What am i missing trigger.entity_id

Got my notify Working :slight_smile:

{
“message”: “Side Door Open”
}

I want to get smarter

base on what tigger the automation

thought his would work BUT not
{
“message”: " {{ trigger.entity_id }} Open"
}

think I missing Something
What Manual should i be reading

I used this a while back but changed to something different, but I think it’s supposed to be

trigger.entity_id.attributes.friendly_name

thanks

added this
{
“message”: " {{ trigger.entity_id.attributes.friendly_name }} Open"
}

nop

log saying
File “/usr/lib/python3.6/site-packages/jinja2/sandbox.py”, line 385, in getattr
value = getattr(obj, attribute)
jinja2.exceptions.UndefinedError: ‘trigger’ is undefined

so more reading

are you manually running the automation or actually triggering it with real hardware?

you need to have all the possible triggers listed in order for it to work, ie

  trigger:
    - platform: state
      entity_id: switch.one, switch.two, switch.three
      from: 'not_home'
      to: 'home'
      for:
        seconds: 30

this way the automation gets triggered and will only spit out the message depending on which item triggered it.

also It should be

trigger.to_state.name

found it on someone elses automation. I hadn’t used it in months and forgot the proper syntax.

thank

Ill try it when I get home

I under Stand What you have Written
one of my problems when I read automation which is build from the Front end
it look like its in the wrong Order think that a python thing LOL
Hay what about when we build a automation form the front could the engine put some
Hash -----------------------------------
at the Begining before script/ automation
and
Hash ----------------------------------- at the End
so just us new bee and see what happen and so we can break Break it

Trying to get it to send a message when the Side Door is Open

if i have
{
“message”: “Side Door Open”
}

Works

BUT when I Try this

{
“message”: “{{ trigger.to_state.name }} Open”
}

its does not Work :frowning:

Just trying to get it working smarter

ooo well I keep reading

Thanks again for your inputs I leaning something one day the light will come on LOL

Could you please post your entire automation, with proper formating? (See the blue box at the top of this page)

I suspect that your issue is that you need to use a data_template, not just data.

Dolores here automation

- action:
  - data:
      message: A  Open was Open
    service: notify.stephan_phone
  - data:
      data:
        displaytime: 20000
        icon: warning
      message: A Door Opened!
      title: Home Assistant
    service: notify.main_kodi_message
  alias: To Stephan Phone
  condition: []
  id: '1513489971216'
  trigger:
  - entity_id: sensor.garage_side_door
    from: Closed
    platform: state
    to: Open
  - entity_id: sensor.garage_door
    from: Closed
    platform: state
    to: Open

I want to change the
message: A Open was Open

to
what trigger open

eg if the garage_door was open then

garage door open

thanks

well Im getting some where

look like a have to change the data to data_template

{{ trigger.to_state.state }}

tells me the state :slight_smile:

now work out how to display the name of it

- action:
  - data_template:
      message: " {{ trigger.entity_id }} was {{ trigger.to_state.state }} "
    service: notify.stephan_phone

the trigger.entity_id give me the ID would like it to display the friendly_name

im getting so close

trigger.entity_id.name
or
trigger.entity_id.friendly_name

give me the nothing
message just say

was Open

well ill keep reading

I think what you need is {{ trigger.to_state.attributes.friendly_name }}

Edit: or actually!
{{ trigger.entity_id.attributes.friendly_name }}

3 Likes

THANK YOU
It Working now

" {{ trigger.to_state.attributes.friendly_name }} was {{ trigger.to_state.state }} "

that was the only pattern that i didn’t try

now im seeing the light

thanks again

2 Likes

This isn’t actually a thing, right? I’ve been trying to figure out how to get the friendly names of things for automating messaging like:

“Front door sensor was closed and now is open!”
“Floor of water heater sensor was dry and now is wet!”
“Garage motion sensor was clear and is now detected!”

Seems like a good data template for this would be:
{{ trigger.entity_id.attributes.friendly_name }} was {{ trigger.from_state.attributes.friendly_name }} and now is {{ trigger.to_state.attributes_friendly_name }}!

Lots of people have recommended using {{ trigger.entity_id.attributes.friendly_name }} but on my setup that appears to result in error messages like this:

Error rendering data template: UndefinedError: 'str object' has no attribute 'attributes'

This tells me that entity_id is a string and to my surprise trigger.from_state.attributes.friendly_name is actually the entity_id’s friendly_name and has nothing to do with the state at all.

In my case trigger.from_state.state (or to_state) is either ‘off’ or ‘on’. Is there any way to have it process the type of Binary Sensor so that the state is actually meaningful to humans? Especially for things like a lock – ‘on’ means unlocked and ‘off’ means locked.

{{ trigger.entity_id.attributes.friendly_name }}

you need a to_state or from_state

This tells me that entity_id is a string and to my surprise trigger.from_state.attributes.friendly_name is actually the entity_id’s friendly_name and has nothing to do with the state at all.

It has nothing to do with current state. It has to do with state changes. “TO” a new state “FROM” and old state.

With a binary sensor I’d just set up an if else and be done with it.

ie (pseudo code)

if state = on then
    door is open
else
    door is closed
end if

With a binary sensor there’s no real need for to and from… you just need an automation that fires on a change…

Hope this helps

Thanks for the response @forsquirel. I understand how the automation triggering works but I’m trying to figure out how to make the automation send a message using a data_template with the result being the custom state value based on the type of binary sensor. I currently have this:

- id: make_sonos_talk
  alias: Make Sonos Talk
  trigger:
    - platform: state
      entity_id: binary_sensor.front_door
      to: 'on'
    - platform: state
      entity_id: binary_sensor.basement_family_room_door
      to: 'on'
    - platform: state
      entity_id: binary_sensor.drum_room_exterior_door
      to: 'on'
    - platform: state
      entity_id: binary_sensor.garage_front_man_door
      to: 'on'
    - platform: state
      entity_id: binary_sensor.floor_near_gas_water_heater
      to: 'on'
    - platform: state
      entity_id: binary_sensor.floor_near_electric_water_heater
      to: 'on'
    - platform: state
      entity_id: binary_sensor.motion_in_garage
      to: 'on'
    - platform: state
      entity_id: binary_sensor.motion_in_drum_room
      to: 'on'
  action:
    - service: script.sonos_say
      data_template:
        sonos_entity: media_player.office_sonos
        volume: 0.5
        message: >
          {{ trigger.to_state.attributes.friendly_name }} is now {{ trigger.to_state.state }}!
        delay: '00:00:05'

… and it results in:
“Front door is now on!”
“Floor near gas water heater is now on!”
“Motion in garage is now on!”
etc…

what I want is:
“Front door is now open!”
“Floor near gas water heater is now wet!”
“Motion in garage is now detected!”
etc…

Is that possible?

According to binary sensors you need to assign a device class. I don’t use them so I can’t say first hand, but I think that’s what you need.

1 Like

Yeah, I’m using the device class with the sensors so the “friendly name” is applied to the lovelace card:
image

But, the automation messaging state tokens still don’t seem to have a way to get to the “friendly name” of the state, it’s still reported as ‘on’ or ‘off’.