Now, obviously an action (I really preferred the old service call nomenclature instead of the overloaded use of “action” but I suppose that boat has sailed) can be performed on a collection of entities. However in this case I want to perform the action only if the state of the entity is X. There seems to be no clear way to do this, but hopefully I’m missing something. Maybe some template magic? Just to complicate things this is for use in a script where ideally the list of entities needs to be a field, if that’s possible.
#Loop over entities calling an action
1 messages · Page 1 of 1 (latest)
Yes you can do that with a template. If you have a list of entity_id’s in a variable called entities then it would be
{% set entity_dict = zip(entities, entities | map('states')) %}
{% set filtered_entities = entity_dict | selectattr(1, '==', 'the_state_i_want') | map(attribute=0) | list %}
{{ filtered_entities }}
If you need more help with this you should make a post over in #1284966664357810196
Can be done without templates as well
Oh wait, not if the list of entities is provided as a field
Anyway, you could do a repeat
repeat:
for_each: "{{ entity_list_provided_by_field }}"
sequence:
- condition: template
value_template: "{{ is_state(repeat.item, 'X') }}"
- action: whatever
target:
entity_id: "{{ repeat.item }}"
If the state should be exactly a set value, so not above or below, you don't need to create a zip
{{ entities | select('is_state', 'wanted_state') | list }}
I assume there is not_state? Since it’s actually the reverse. Looks good, will have a play! Thanks both of you. Will report outcome.
You can use reject() instead of select()