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 %}
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