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.
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
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?
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.
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.