Trigger.entity_id sample

I am trying to create an automation where notification is sent when the alarm is tripped. In the notification, I want to include which entity triggers the alarm. Referring to Automation Templating, this is what I put in my automation…

- alias: 'Alarm tripped'
  trigger:
    - platform: state
      entity_id: sensor.frontdoor
      state: 'Open'
    - platform: state
      entity_id: sensor.entrancemotion
      state: 'Active'
  condition:
    - condition: state
      entity_id: alarm_control_panel.zone_a
      state: armed_away
  action:
    service: notify.notify
    data:
      message: "Alarm tripped at {{ trigger.entity_id }}"
      title: ""

But it is not working. Can anyone tell me what is the correct way to use {{ trigger.entity_id }} ? Thanks.

2 Likes

I am guessing it may be a problem with your state for the condition here. State should be a text, e.g., ‘not_home’.

Nothing is wrong with that. This is the new syntax introduced in version 0.19. More at https://home-assistant.io/getting-started/scripts-conditions/

Anyway, I know why it is not working. I need to change the data under action to data_template

Now I will receive a notification whenever the alarm is tripped. For example, “Alarm tripped at sensor.frontdoor”

However, I wish I know how to use the friendly name of the sensors in the notification. Any pointer?

1 Like

Haven’t tried trigger data yet, but with templating…maybe adding .attributes.friendly_name to the template?
Something like :

message: "Alarm tripped at {{ trigger.entity_id.attributes.friendly_name }}"

thanks. but it is not working.

It seems attributes are not passed over to data_template. A temporary workaround for me is to manipulate the string. Example…

- alias: 'Alarm tripped'
  trigger:
    - platform: state
      entity_id: sensor.front_door
      state: 'Open'
    - platform: state
      entity_id: sensor.entrance_motion
      state: 'Active'
  condition:
    - condition: state
      entity_id: alarm_control_panel.zone_a
      state: armed_away
  action:
    service: notify.notify
    data_template:
      message: "Alarm tripped at {{ trigger.entity_id|replace('sensor.', '')|replace('_', ' ') }}"
      title: ""
2 Likes

This is a pretty cool method - I see how you got your nym.

Hey,
I know it’s a bit late but I just had the exact same issue and I did it with a quick loop over all states of the domain sensor.

action:
  - service: notify.xxx
    data_template:
      title: "[Alert][Server] - "
      message: >-
        {%- for state in states.sensor if state.entity_id == trigger.entity_id -%}
          {{ state.attributes.friendly_name }} is down.
        {%- endfor -%} 
8 Likes

Nice! This might be an answer for an Alexa intent I was working on - thanks!

1 Like

Do you care to elaborate on this? I am currently working on alexa integration myself. Althogh I have to do the “Alexa” device myself as well, as it’s not available in Germany right now. But it’s always good to hear what others are doing to get some ideas :smiley:

I have an intent set up to activate scenes and another to check on the status of the front door lock.

What I’d like to do is set the manual alarm in HA, but have Alexa tell me if one or more of my sensors are currently on and if so, which ones. If all sensors are reported off, then Alexa should set the alarm in away mode and tell me I have XX amount of seconds before it’s armed.

I’m kind of hoping some of the answers I got from @aimc as well as your new piece of info will let me do this. Even just querying the state of all the sensors and returning the names of the ones that are on would be good enough for me.

Is the Alexa Voice Service available in Germany? I’ve seen a lot of DIY RasPi Echo setup tutorials. Perhaps you could roll your own?

What you trying to do should be perfectly doable. If you need any help with your setup I’d be glad to help you out. What maybe does help you is making a template sensor called something like “alexa_response”

alexa_response:
  value_template: >-
    {{ states("sensor.alexa_response") }}

You can just call a script in your intent set the state of the “sensor.alexa_response” to what you want alexa to say after the script and add that to your alexa like this

speech:
  type: plaintext
  text: >
    {{ states("sensor.alexa_response") }}

That’s how I implemented my custom kodi commands which rely on an external python script. (For playing latest episode of x or playing season y episode z of x)

The Alexa voice service is available here, at least the developer console is.
And I already have a RaspberryPi set up using the AlexaPi github repo and even added voice activation over in my fork/branch https://github.com/PhyberApex/AlexaPi/tree/voice-activated for anyone wondering.

1 Like

Thanks much! I may take you up on that.

I’m doing like 4 things at once so I think I need to re-read this post when I am in dev mode. :smiley:

I’m trying to clean house and get things sorted for a new couch being delivered and at the same time struggling with an Aeotec Multisensor 6 and trying to get it set up proper for HA. I probably should take a break…

But I’m going to come back to this - and I wanted to take the time to thank you for your guidance and your offer of help. Cheers!

1 Like

No problem! Cheers!

Dredging up an old topic! But could you clarify how you’re setting the state of a sensor? I was under the impression that they were read-only. Or are you using a command-line sensor and manipulating things that way? I’d love to find a way to store small bits of text.

wow…geezus I just now have seen that I did not answer that. Really sorry! I set the state of this sensor over the REST API from a python script. I think there really should be a way to write sensor data within HA tho.

~Cheers

This is exactly what I was looking for! I wanted my google home to welcome home whoever just came home, instead of a generic welcome home message.

alias: Home Routine
trigger:
  - platform: state
    entity_id: group.jordan
    from: not_home
    to: home
  - platform: state
    entity_id: group.lee
    from: not_home
    to: home
  - platform: state
    entity_id: group.alexia
    from: not_home
    to: home

action:
  - service: media_player.turn_off
    entity_id: media_player.living_room_home
  - service: media_player.turn_on
    entity_id: media_player.living_room_home
  - service: switch.turn_on
    entity_id: switch.thecave
  - service: cover.open_cover
    entity_id: cover.garage_door
  - delay: '00:02:00'
  - service: tts.google_say
    entity_id: media_player.living_room_home
    # data:
    #   message: "Welcome home!"
    data_template:
      message: Welcome home, {{ trigger.entity_id|replace('group.', '') }}!

I’ll test this out when I get home.

You can simplify it to this…

alias: Home Routine
trigger:
  - platform: state
    entity_id: group.jordan, group.lee, group.alexia
    from: not_home
    to: home

action:
  - service: media_player.turn_off
    entity_id: media_player.living_room_home
  - service: media_player.turn_on
    entity_id: media_player.living_room_home
  - service: switch.turn_on
    entity_id: switch.thecave
  - service: cover.open_cover
    entity_id: cover.garage_door
  - delay: '00:02:00'
  - service: tts.google_say
    entity_id: media_player.living_room_home
    # data:
    #   message: "Welcome home!"
    data_template:
      message: Welcome home, {{ trigger.to_state.attributes.friendly_name }}!
5 Likes

What was your trigger on this automation?