Automation Condition - Check if any state.light or state.switch is on

Hello! I’m trying to set up an automation that does that following:

Trigger: I leave home
Condition: Check if any light/switch is on
Action: Notify me via push notification/action to turn everything off

I’m stuck on what i can use in condition, template to check and proceed if any light/switch is on? Anyone able to steer me in the right direction? Thanks!

1 Like
condition: template
value_template: "{{ states.light|selectattr('state','equalto','on')|list|length > 0 }}

And if you want to know which ones in your message:

data:
  message: "These lights are on, {{ states.light|selectattr('state','equalto','on')|map(attribute='name')|list|join(', ')  }}"

The same thing can be done for the switch domain. Though using both requires a bit of glue logic:

conditions:
  - condition: or
    conditions:
      - condition: template
        value_template: "{{ states.light|selectattr('state','equalto','on')|list|length > 0 }}"
      - condition: template
        value_template: "{{ states.switch|selectattr('state','equalto','on')|list|length > 0 }}"

Or using the shorthand notation:

conditions:
  - condition: or
    conditions:
      - "{{ states.light|selectattr('state','equalto','on')|list|length > 0 }}"
      - "{{ states.switch|selectattr('state','equalto','on')|list|length > 0 }}"

And your message:

data:
  message: >
    {% if states.light|selectattr('state','equalto','on')|list|length > 0 %}
      These lights are on, {{ states.light|selectattr('state','equalto','on')|map(attribute='name')|list|join(', ')  }}. 
    {% else %}
      No lights are on.
    {% endif %}

    {% if states.switch|selectattr('state','equalto','on')|list|length > 0 %}
      These switches are on, {{ states.switch|selectattr('state','equalto','on')|map(attribute='name')|list|join(', ')  }}. 
    {% else %}
      No switches are on.
    {% endif %}
1 Like

Hey Tom! Thank you so much for your detailed response! I tried the following but get this bounce back (Heres my code)

condition: template
value_template: "{{ states.light|selectattr(''state'',''equalto'',''on'')|list|length > 0 }}"

Invalid condition configuration

invalid template (TemplateSyntaxError: expected token ‘,’, got ‘state’) for dictionary value @ data[‘value_template’]

conditions:
  - condition: or
    conditions:
      - condition: template
        value_template: '{{ states.light|selectattr(''state'',''equalto'',''on'')|list|length > 0 }}'
      - condition: template
        value_template: '{{ states.switch|selectattr(''state'',''equalto'',''on'')|list|length > 0 }}'

Invalid condition configuration

Unexpected value for condition: ‘None’. Expected and, device, not, numeric_state, or, state, sun, template, time, trigger, zone

Any clues?

Yeah it’s the automations UI editor mangling it.

It works in YAML.

No idea how to fix it sorry, I don’t use the UI automations editor. Maybe:

conditions:
  - condition: or
    conditions:
      - condition: template
        value_template: '{{ states.light|selectattr("state","equalto","on")|list|length > 0 }}'
      - condition: template
        value_template: '{{ states.switch|selectattr("state","equalto","on")|list|length > 0 }}'

Or this:

conditions:
  - condition: or
    conditions:
      - condition: template
        value_template: >
          {{ states.light|selectattr('state','equalto','on')|list|length > 0 }}
      - condition: template
        value_template: >
          {{ states.switch|selectattr('state','equalto','on')|list|length > 0 }}

Thanks Tom_I for the nice example, got everything running but I want to go even further and turn off the specific light that was left on. (as the best system is the one that no action needs to be done manually)

my automaton looks like this

if:
  - condition: template
    value_template: "{{ states.light|selectattr('state','equalto','on')|list|length > 0 }}"
  - condition: state
    entity_id: input_boolean.someone_home
    state: "off"
then:
  - service: notify.notify_marius
    data:
      title: Home Assistant
      message: >-
        The following lights where left on and they where turned off: {{
        states.light|selectattr('state','equalto','on')|map(attribute='name')|list|join(',
        ')  }}
  - service: light.turn_off
    data: {}
    target:
      entity_id: {{ states.light|selectattr('state','equalto','on')|map(attribute='entity_id')|list|join('- ')  }}

my issue is at the formatting of the call service lights.off, I did test it in different ways but as I do not know Jinja2 I’m useless at this

  - service: light.turn_off
    data: {}
    target:
      entity_id: {{ states.light|selectattr('state','equalto','on')|map(attribute='entity_id')|list|join('- ')  }}

what about if multiple lights are left on ?

  - service: light.turn_off
    data: {}
    target:
      entity_id: "{{ states.light|selectattr('state','equalto','on')|map(attribute='entity_id')|list }}"

Reference: Important Template Rules

unfortunately I already try this and is not working


  1. The service call in the screenshot you posted isn’t indented like the example I posted.
  2. How exactly did you test the automation? Did you allow it to trigger normally or did you use some other method?

EDIT

In the Automation Editor, when you use Visual Mode for a single action there’s no need to use a leading hyphen.

service: light.turn_off
data: {}
  target:
    entity_id: "{{ states.light|selectattr('state','equalto','on')|map(attribute='entity_id')|list }}"

BTW, the last line of your second screenshot shows that the template produced a correct result. It’s a list containing the entity_id of the one light that is on.

  1. ok
  2. manually triggered from run actions in the automation visual editor

The following formatting works with one or multiple entities, thanks

service: light.turn_off
data: {}
target:
  entity_id: {{ states.light|selectattr('state','equalto','on')|map(attribute='entity_id')|list }}
enabled: true

Except what you posted above isn’t what I had suggested. Your version of the template is missing outer double-quotes (which makes the configuration invalid).

Hi @123 ,
I’m looking for something to turn off all the lights that are only on. Zwave takes too long to turn everthing off with a group. Can I also exclude things via your code? For example, light.group_all.

Or skip one light, example i’m going to bed, all the lights need to turn off but not the light.bedroom

service: light.turn_off
target:
  entity_id: >
    {{ states.light
      | selectattr('state', 'eq', 'on')
      | rejectattr('entity_id', 'in', ['light.group_all', 'light.another_one', 'light.etc'] )
      | map(attribute='entity_id') | list }}

Perfect! Thank you very much.

These are all examples for lights. My lights are all on smart switches and I replaced light with switch in the code. It works but it also picks up every switch (physical or virtual). Is there a way to do all of this with a specific switch group? I have the same situation as the original post where I want to determine if everything in a group is off when I leave. This group is a combination of smart switches but not all switch entities in HA. Can I do the same where I’m confining the status and notification to only those within the defined group?


    {{ states.switch
      | selectattr('entity_id', 'in', state_attr('group.my_switches', 'entity_id') if state_attr('group.my_switches', 'entity_id') )
      | selectattr('state','eq','on')
      | map(attribute='entity_id') | list }}


Or directly use your switch group:


    {{ state_attr('group.my_switches', 'entity_id')
      | select('is_state', 'on' )
      | list }}

Thank you.

hi… what if i have switch groups within switch groups? how do i list the ultimate underlying switches?

i.e. living room switch group → dining room switch group → actual switches

thank you