I have about 8 doorsensors and 10 motion sensors in the house. I have them grouped in two groups (yes indeed, motion & door).
Getting a notification (or any other action) if ANY of eg the doorsensors being tripped is easy. I can do that in one simple automation.
However I would love to get a custome notification based on what SPECIFIC door opened. I can do this by building 8x automations, doable but not nice. Furthermore, if I add or change a sensor I also need to change/add the individual automation.
Is there a simpler way to do this in one automation with a template? So would love to have one automation (leveraging the groups) but a custom notification on what sensor was tripped.
As far as I know, you can’t do this with a group, since the group itself is an entity, therefore it has a friendly_name and state of its own, rather than picking out each entity within the group.
In order to get the name and state of the item that triggered the automation, you use the trigger.to_state variable and list each of your sensors in a list under entity_id like this:
trigger:
platform: state
entity_id:
- sensor.front_door
- sensor.back_door
- sensor.garage_side_door
- sensor.garage_overhead_door
state: 'Open'
action:
service: notify.notify
data_template:
message: >
{{ trigger.to_state.attributes.friendly_name }} was opened
The trigger.to_state is the entity that triggered the action, so if it was the front door sensor, then these are what the variables will contain:
The Variable The Value
trigger.to_state.state on or Open
trigger.to_state.attributes.friendly_name Front Door
And these will change depending on what sensor triggered the action
I prefer not to use the state in the trigger, that way I can send a notification whether the door opens or closes, which allows you to then take advantage of the trigger.to_state.state variable
My window and door sensors are using template sensors so that I can change the state from on to Open and off to Closed, so with the above, my notifications will be either:
message: "The {{ trigger.to_state.name }} was changed to {{ trigger.to_state.state }} while no one is home. - {{ as_timestamp(now) | timestamp_custom('%I:%M:%S %p %d%b%Y', true) }}"
Crap, did I get the variables messed up? Been a while since I setup any message automations with those templates, and I have since moved all of my automations over to AppDaemon, so I kind of screwed the pooch on the variables apparently lol Sorry about that.
A little late to this party, but good for anyone potentially running into this in future
I had a similar issue where i wanted to use the name of the sensor within the group triggering the automation.
I did it as follows:
For the automation, instead of using entity_id: group.motion_cameras, I did entity_id: !include ../zoneminder_cameras.yaml
For the group, instead of listing each binary sensor, i also did entities: !include ../zoneminder_cameras.yaml
Then in the zoneminder_cameras.yaml file i created in the root of my configuration folder, I just added - binary_sensor.front_gate
Defined in one place, used in multiple (Like a group), and correct friendly_name being extracted via:
action:
- service: tts.google_say
data_template:
entity_id: media_player.office_media_player
message: “Motion has been detected on the {{ trigger.to_state.attributes.friendly_name }} camera!”