Automation checking an array of sensors

Hi, I would like to create an automation detecting any of my sensors changing state to offline, without having to add every individual sensor as trigger. Is there a way to add a trigger that will either check from a group or an external list of entities and their states (in this case ‘unavailable’)? I have about 20 sensors, all with extensively unfriendly names which is quite tedious to add them all in. I know I can rename them, but that still is a lot of work and I may want to add in other devices to the check.

I have searched various posts on the forums, but cannot seem to find a solution for this scenario.

Help much appreciated!

- trigger:
  - platform: event
    event_type: state_changed
  condition:
  - condition: template
    value_template: >
      {{
        trigger.event.data.entity_id.startswith('sensor.') and
        trigger.event.data.new_state is not none and
        trigger.event.data.new_state.state == 'unavailable' and
        (
          trigger.event.data.old_state is none or
          trigger.event.data.old_state.state != 'unavailable'
        )
      }}
  action:
  - ...
3 Likes

Thanks! Seems to be working spot on! :smiley:

1 Like

This is amazing, is there a way to include the light that has gone offline in a notification message?

Well this looks for sensors, not lights, but obviously you can edit it to check for lights. Yes, just use trigger.event.data.entity_id or maybe trigger.event.data.new_state.name in a template.

Ah sorry yes I amended the above to use for lights as well, should have said that! So in the data template field for the notification action use one of the above templates? I’ve got it working to tell me when a light or sensor has gone off but was wondering if I could get it to tell me which light or sensor has gone off

Yes

...
- service: something
  data_template:
    message: "{{ trigger.event.data.new_state.name }}"
...

The name field will be the friendly name, or if the entity doesn’t have one, then its entity_id.

2 Likes

Amazing thank you!

1 Like

CAn I ask one final question? Thanks for all the help, I was wondering if I can set a duration for the sensor or light to be off before a notification is issued?

- id: 22a9792f-9e19-49dd-8fb9-f7e3091cae9f
  alias: Light offline
  trigger:
  - event_type: state_changed
    platform: event
    for:
      minutes: 10
  condition:
  - condition: template
    value_template: "{{\n  trigger.event.data.entity_id.startswith('light.') and\n\
      \  trigger.event.data.new_state is not none and\n  trigger.event.data.new_state.state\
      \ == 'unavailable' and\n  (\n    trigger.event.data.old_state is none or\n \
      \   trigger.event.data.old_state.state != 'unavailable'\n  )\n}}\n"
  action:
  - data_template:
      message: '{{ trigger.event.data.new_state.name }} is offline, please investigate'
      title: Light Offline
    service: notify.mobile_app_matts_iphone

I’ve tried the above but it keeps erroring with Invalid config for [automation]: [for] is an invalid option for [automation] but looking at the docs, this is what I should be using?

Yikes, are you using the automation UI editor or something?! Wow is that messed up!

Where in the docs does it say you can use a for option with an event trigger? It won’t say that, because you can’t. Think about it. An event is, well, an event. It happens in an instant of time. How can an event, that has no duration, exist for a period of time?

I think you can do what you want, however, but you must be using at least HA version 0.113.

- trigger:
  - platform: event
    event_type: state_changed
  condition:
  - condition: template
    value_template: >
      {{
        trigger.event.data.entity_id.startswith('sensor.') and
        trigger.event.data.new_state is not none and
        trigger.event.data.new_state.state == 'unavailable' and
        (
          trigger.event.data.old_state is none or
          trigger.event.data.old_state.state != 'unavailable'
        )
      }}
  mode: parallel
  action:
  - wait_template: "{{ not is_state(trigger.event.data.entity_id, 'unavailable') }}"
    timeout: "00:10"
    continue_on_timeout: true
  - condition: template
    value_template: "{{ is_state(trigger.event.data.entity_id', 'unavailable') }}"
  - data_template:
      message: '{{ trigger.event.data.new_state.name }} is offline, please investigate'
      title: Light Offline
    service: notify.mobile_app_matts_iphone

Thanks for the reply, I think I started off in the UI but then removed and now I’ve created it in the yaml file. I used the ‘for’ in the numeric state trigger linked here, https://www.home-assistant.io/docs/automation/trigger/ guessing that was wrong. Appreciate all the help.

Just slightly further up that page the event trigger is documented. It doesn’t mention that for can be used with it. Every trigger is different.

No problem. Glad to help. Hope it works for you!

Sorry to come back to you but using the above example, I get this error?

Invalid config for [automation]: invalid template (TemplateSyntaxError: expected token ',', got 'string') for dictionary value @ data['action'][1]['value_template']. Got None. (See /config/configuration.yaml, line 10).

I’ve commented out the automation above and the error disappears so think it is linked to it?

EDIT: Solved the issue! Had an extra ’ in the action value template.

Yep, just saw the discussion on discord. :smiley: