Reference new additional entity in notification by appending to trigger.entity_id

As per the title, I’ve got a trigger.entity_id that I want to append text to, to reference a related entity in a notification.

I’ve got a automation that triggers on a low battery event, and want to include information in the notification from another entity (battery type) that is related to the notification. To include that I need to append “_type” to the trigger.entity to include that.

The two entity in question for testing are:

image

I don’t want to hardcore entities as this automation is used by a half-a-dozen battery-powered devices.

This is the current notification yaml I have

service: notify.pushover
metadata: {}
data:
  title: Battery level Notification {{trigger.to_state.name}}
  message: >-
    Battery alert for {{trigger.to_state.name}} reporting a currnet level of
    {{trigger.to_state.state}} % Consider charging or replacing batteries
    {{trigger.entity_id ~ '_type'}} soon.
  target: Mobile-Phone

I have tried to create and test the concatenated based on testing it in “states” which yielded positive results

{{ states('sensor.pir_sensor_battery' ~ '_type') }}

image

But the the life on me I can can’t get the notification to show " 3x AAA" as per the testing.

Based on the above code I get the below, which is sort of correct, but showing the entity and not the entity state

image

My suggestion, IE what I have found very helpful with troubleshooting…
Add a variable statement, and create variables for all those, especially the created values. This, 1, makes the logic code MUCH cleaner, and 2, When you look at the trace for the automation all the variables are listed and you can see exactly what is created as it runs.

Thanks for the advice @Sir_Goodenough tried and somehow still have the same challenge

Updated automation to create a variable as part of the action section:

action:
  - variables:
      type_msg: "{{trigger.entity_id ~ '_type'}}"

This does “create” the right entity when triggered

context:
  id: 01J11PGW52Y3BQGKZB2812CQHY
  parent_id: 01J11PGW512GNE5HHA8NBRK8SP
  user_id: null
type_msg: sensor.pir_sensor_battery_type

Then to reference the created variable “type_msg” in the message:

service: notify.pushover
metadata: {}
data:
  title: Battery level Notification {{ trigger.to_state.name }}
  message: >-
    Battery alert for {{ trigger.to_state.name }} reporting a currnet level of
    {{ trigger.to_state.state }} % Consider charging or replacing batteries {{
    type_msg }} soon.

But no dice, I still get the entity in the message:

Executed: June 23, 2024 at 4:43:56 PM
Result:
params:
  domain: notify
  service: pushover
  service_data:
    title: Battery level Notification PIR Sensor Battery
    message: >-
      Battery alert for PIR Sensor Battery reporting a currnet level of 19.0 %
      Consider charging or replacing batteries sensor.pir_sensor_battery_type
      soon.
    target: Mobile-Phone

equals this:

correct? What do you want in there?

I personally would all variables to all the trigger.xxxx things then look in the trace to see what is what. Personal choice, but helps me in troubleshooting.

variables:
  to_state: '{{ trigger.to_state.state | lower }}'
  from_state: '{{ trigger.from_state.state | lower }}'

condition:
  - alias: Make sure there even was a move
    condition: template
    value_template: '{{ to_state != from_state }}'

In the trace along with everything else I use to make decisions:

to_state: home
from_state: home

from: HA_Blueprints/Automations/Person_Alert_Blueprint.yaml at 2b12188d3388cd5dddfadbf4098980804cc24de1 · SirGoodenough/HA_Blueprints · GitHub.

To get the entity’s state value, use the states() function.

service: notify.pushover
metadata: {}
data:
  title: Battery level Notification {{ trigger.to_state.name }}
  message: >-
    Battery alert for {{ trigger.to_state.name }} reporting a current level of
    {{ trigger.to_state.state }} % Consider charging or replacing batteries {{
    states(type_msg) }} soon.

That is the magic, after reading many posts and docs, I did try it with {{ states.type_msg }} but you nailed it for me

Thank you now getting the state of the entity in the message

  domain: notify
  service: pushover
  service_data:
    title: Battery level Notification PIR Sensor Battery
    message: >-
      Battery alert for PIR Sensor Battery reporting a current level of 17.0 %
      prepare to charge or replace the batteries 3× AAA soon.

1 Like

I wanted to see the state of that entity as that will tell me which type of batteries.

All the other references was working it is just this one that I’m concatenating to which gave me issues

Thank you for assisting @Sir_Goodenough

1 Like