Getting friendly name from trigger, not working

What’s wrong with this automation? The value for “message” doesn’t seem to be rendering. If I replace message with “abc” I get the expected results.


- alias: Sensor Low Battery
  trigger: 
    platform: state
    entity_id: binary_sensor.mba_bath_sensor_low_battery
    to: 'on'
  action:
    - service: script.system_notify
      data_template:
        title: "Low Battery"
        message: "{{trigger.to_state.attributes.friendly_name}}"

try

{{ state_attr(trigger.entity_id, 'friendly_name') }}

Not sure if it will make a difference, but try

  action:
    - service: script.system_notify
      data_template:
        title: "Low Battery"
        message: >
          {{ trigger.to_state.attributes.friendly_name }}

That won’t make any difference. If his code is not working, then your code won’t either. The only difference is that he has a single line template and you’re specifying a multiline template.

Noted. Could it be that the binary sensor is missing the friendly name altogether? If this is a template sensor, the friendly_name is completely optional.

there are many possibilities. Hard to say when we don’t have all the pieces. If the template that I provided returns undefined or None, then he doesn’t have the attribute. In that case he can use this template to get the auto generated name…

{% set domain, object_id = trigger.entity_id.split('.') %}
{{ states[domain][object_id].name }}

… or just:

{{ trigger.to_state.name }}

yep, that’d work if it’s a state trigger. I was trying to be generic for any trigger.

But not all triggers have entity_id. If you check, all the ones that have entity_id also have to_state. :smiley:

Here is how I define the binary sensor. I tried all of the suggestions but none of them will report the defined friendly name.

    mba_bath_sensor_low_battery:
      value_template: "{{ is_state('binary_sensor.1st_bathroom_sensor_low_bat', 'on') }}"
      friendly_name: "MBA motion sensor battery"
      device_class: battery

Please elaborate on what you mean by ‘doesn’t seem to be rendering’. The action is passing two variables to a script. One of the variables is called message and should contain the binary_sensor’s friendly_name. You’re saying it doesn’t so what does message contain? Some other value or nothing at all?

FWIW, I tested this simple automation and it works. The notification contained the input_boolean’s friendly_name.

- alias: 'test 44'
  trigger: 
    platform: state
    entity_id: input_boolean.guest
  action:
    service: persistent_notification.create
    data_template:
      title: test 44
      message: "Triggered by {{trigger.to_state.attributes.friendly_name}}"

So I tried this and it’s still not working.


- alias: Sensor Low Battery
  trigger: 
    platform: state
    entity_id: binary_sensor.mba_bath_sensor_low_battery
    to: 'on'
  action:
    - service: script.system_notify
      data_template:
        title: "Low Battery"
        message: "Triggered by {{trigger.to_state.attributes.friendly_name}}"

When I look at the states for the binary_sensor it shows

friendly_name: MBA motion sensor battery
device_class: battery

To be clear, those are the binary_sensor’s attributes:

  • friendly_name
  • device_class

Out of curiosity, what is the binary_sensor’s current state? It should be on to have triggered the automation.

I don’t think the problem is in the automation or the definition of the binary sensor. It must be in the script, which you haven’t shared yet. Please post the script.

Or possibly are you manually triggering the automation? If so, then the trigger variable will not exist, and that will cause it to not work as expected. You must trigger the automation normally, meaning you have to get binary_sensor.mba_bath_sensor_low_battery to change to 'on'. If that isn’t happening on its own, and you want to test the automation & script, then go to the States page and use that to force the state of binary_sensor.mba_bath_sensor_low_battery to change (to 'off', and then to 'on'.)

That’s exactly what I was doing. Your suggestion on changing the state worked!

1 Like

This is the umpteenth time this week someone has manually triggered an automation then reported the automation doesn’t work correctly.

@FutureTense
As the umpteenth person to have done this, can you suggest where in the documentation this important tidbit of information can be plastered so others can avoid making the same mistake?

Currently it is found in Automation > Troubleshooting > Testing Your Automation.

Please note that if you click on Trigger of an automation in the frontend, only the action part will be executed by Home Assistant. That means you can’t test your trigger or condition part that way. It also means that if your automation uses some data from triggers, it won’t work properly as well just because trigger is not defined in this scenario.

Also, how did you manually trigger it? From the Lovelace UI (trigger button in the automation’s More-Info box) or by using the Developer Tools > Services page?