Automation when state is NOT "home", for a list of entities?

Hi,
I’m using this working automation :

- alias:  Always-on devices dead
  initial_state: 'on'
  trigger:
    platform: state
    entity_id:
      - device_tracker.plug_dishwasher
      - device_tracker.plug_washing
      [...more devices...]
    to: 'not_home'
    for:
      minutes: 60
  action:
    service: notify.ha_telegram
    data_template:
      message: "{{ trigger.to_state.attributes.friendly_name }} is now {{ trigger.to_state.state }} since {{ trigger.for }} minutes"

How could I replace “looking for <not_home>” to "looking for anything but " ?

I’ve seen people using in another topic

value_template: "{{ not is_state('climate.panasonic_aircon', 'idle') }}"

but it doesn’t work for a list of entities I think

This is not an easy feat when you want to treat every device tracker as a separate trigger. Your best option would be to create a template trigger for each device tracker:

trigger:
- platform: template
  value_template: "{{ not is_state('device_tracker.xxx', 'home') }}"
  for:
    minutes: 60

Then create one for each device tracker.

You can use the template designer to write it for you, and shrink it down to 3 lines by changing your for.

  trigger:
{%- set trackers = [
      'device_tracker.plug_dishwasher',
      'device_tracker.plug_washing',
      '... add more trackers here in quotes followed by comma, like this row ...',
  ] %}
{%- for tracker in trackers %}
  - platform: template
    value_template: "{% raw %}{{ not is_state('{% endraw %}{{ tracker }}{% raw %}', 'home') }}{% endraw %}"
    for: "00:60:00"
{%- endfor %}

thanks Pedro I’ll give it a try

should I also test the “not” condition as documented on https://www.home-assistant.io/docs/scripts/conditions/#not-condition ?
But I would need a trigger which triggers all the time, then which will be restricted by this condition, I think.

I’ve tried

- alias:  Always-on devices dead 2
  initial_state: 'on'
  trigger:
{%- set trackers = [
      'device_tracker.plug_dishwasher',
      'device_tracker.plug_washing',
  ] %}
{%- for tracker in trackers %}
    platform: template
    value_template: "{% raw %}{{ not is_state('{% endraw %}{{ tracker }}{% raw %}', 'home') }}{% endraw %}"
    for: "00:60:00"
  action:
    service: notify.ha_telegram
    data_template:
      message: "{{ trigger.to_state.attributes.friendly_name }} is now {{ trigger.to_state.state }} since {{ trigger.for }} minutes - 2"
{%- endfor %}

but I get

Failed config
  General Errors:
    - Error loading /config/configuration.yaml: while scanning for the next token
found character '%' that cannot start any token
  in "/config/automation.yaml", line 109, column 2

Line 109 being {%- set trackers = [

Is there a documentation about for loops against arrays in HA ?

You missed the part where I said paste that into the template editor and copy the result into your automation.

1 Like

Thanks @petro - I got it now :+1:

1 Like