Template condition to check state for multiple entities?

Hi,

I need a template or state condition (in a script) that will test if any of the lights in a list is off. The list is supplied as a variable lights which may contain one or more lights.

Say I supply something like

variables:
  lights: 
    - light.one
    - light.two

Then I would to condition the script to run only if all the lights are off. So I need something like

condition: "{{is_state(lights,'off'}}"

that will work with a list of one or more lights. I can use some kind for loop, but how do I combine the True/Falses I get as output?

I tried something like the below, but it doesn’t work, even though it doesn’t give a syntax error:

conditions:>
{% for l in light %}
 - {{is_state(l,'on')}}
{%-endfor %}

Thanks for any help!

If you create a group containing all of the lights then the Template Condition to check if all are off is simply this:

condition: "{{ is_state('group.my_lights', 'off') }}"

If you, for some reason, don’t want to use a group but prefer to define them as a list in a variable, the template is a bit longer.

condition: "{{ expand(lights) | selectattr('state', 'eq', 'on') | list | count == 0 }}"
3 Likes

That’s great. Thanks a lot Taras.

The list is dynamically generated by some other sensors so I didn’t want to have a fixed group. A lot of new things to unpack in the template.

1 Like

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic is resolved. This helps other users find answers to similar questions. For more information, refer to guideline 21 in the FAQ.

I tried this solution with a blueprint and I am always getting false. I will continue to research, but I have no solution at this moment.

This blueprint is my motion sensing automation. If the motion is detected, it turns the lights on to a color temperature based on the time of day. If the motion has been off for a while, it will turn the lights to half brightness for a bit (to warn people in the room) and then turn them off.
I am using the template statement to check if the lights are already off before telling them to go to half brightness.

mode: restart
max_exceeded: silent

trigger:
  platform: state
  entity_id: !input motion_entity

variables:
  lights_template: !input light_target

action:
  - if:
    - condition: template
      value_template: "{{trigger.to_state.state == 'on'}}"
    then:
    - service: light.turn_on
      data:
        brightness: 255
        color_temp: "{{states('sensor.v_timeofday_mireds')}}"
      target: !input light_target
    else:
      - if: # Check to see if all the lights are off
        - condition: template
          value_template: "{{ expand(lights_template) | selectattr('state', 'eq', 'on') | list | count > 0 }}"
        then:
          - delay: !input no_motion_wait
          - service: light.turn_on
            data:
              brightness: 127
            target: !input light_target
          - delay: !input auto_off_time
          - service: light.turn_off
            target: !input light_target

I found a solution. There is a duplicate post here with a lot of detail:

Here the solution is made better:

I was able to get my blueprint to work.

1 Like