Extracting which sensor was triggered from sensor group (for notification purposes)

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.

1 Like

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

trigger:
  platform: state
  entity_id:
    - sensor.front_door
    - sensor.back_door
    - sensor.garage_side_door
    - sensor.garage_overhead_door
action:
  service: notify.notify
  data_template:
    message: >
      {{ trigger.to_state.attributes.friendly_name }} is {{ trigger.to_state.state }}

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:

“Front Door is Open” or “Front Door is Closed”

9 Likes

Makes total sense to use the devices as triggers and is also managable! Many thanks, also for the examples

Once again, reading one of your answers to a post leads to an answer for a problem I was having!

Thanks, @jbardi!

1 Like

Mr. Hass @jbardi killin it like usual! My HA hero!

I’m trying to do this as well, but all I get in my notification is:
“The was changed to on while no one is home - 02:20:38 PM 15Sept2016”

trigger:
  - platform: state
    entity_id:
      - binary_sensor.ecolink_garage_door_sensor_sensor_3
      - binary_sensor.ecobee_occupancy
      - binary_sensor.dining_room_occupancy
    from: 'off'
    to: 'on'
condition:
  condition: state
  entity_id: group.all_devices
  state: 'not_home'
action:
  - service: notify.html5_scott_phone
    data_template:
      message: "The {{ trigger.to_state.attributes.friendly_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) }}"
1 Like

Got it to work with:

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) }}"
1 Like

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.

1 Like

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!”

3 Likes