Help with an automation where the trigger is not passing in the battery_level attribute

I’m close to having my automation figured out for sending notifications when a battery is getting low on a device. The one thing I am unable to figure out is how to get the battery_level to get passed into the data_template. The friendly_name is available, so some attributes are there but for some reason this one is not.

Here is the automation:

- id: low_battery_notification
  alias: low battery notification
  trigger:
    platform: numeric_state
    entity_id:
      - sensor.battery_back_bedroom_motion_sensor
      - sensor.battery_entry_motion_sensor
      - sensor.battery_doorbell
      - sensor.battery_front_bedroom_motion_sensor
      - sensor.battery_front_door
      - sensor.battery_garage_entry_door
      - sensor.battery_garage_motion_sensor
      - sensor.battery_kitchen_door
      - sensor.battery_nursery_motion_sensor
      - sensor.battery_rooftop_door
      - sensor.battery_rooftop_motion_sensor
      - sensor.battery_thermostat
      - sensor.battery_deadbolt
      - sensor.battery_garage_deadbolt
    below: 51
  condition: []
  action:
    service: notify.dave_sms
    data_template:
      title: "Low battery notification"
      message: "{{ trigger.to_state.attributes.friendly_name }} is at {{ trigger.to_state.attributes.battery_level | default(0) }}%"

The notification I receive says “doorbell battery is at 0%” (when I set the value to 49 or 50).

When I cast the value as an integer like this:

message: "{{ trigger.to_state.attributes.friendly_name }} is at {{ trigger.to_state.attributes.battery_level | int }}%"

I get the following error when the automation is triggered:

Error rendering data template: UndefinedError: 'mappingproxy object' has no attribute 'battery_level'

Hope this helps a bit.

Thanks!!

SOLVED!! I realized my template sensor was setting the battery_level to state so I just changed to this and it’s working:

     message: "{{ trigger.to_state.attributes.friendly_name }} is at {{ trigger.to_state.state }}%"

Here’s the template sensor:

  - platform: template
    sensors:
     battery_doorbell:
        friendly_name: 'doorbell battery'
        unit_of_measurement: '%'
        value_template: >-
          {{ states.binary_sensor.doorbell_89.attributes.battery_level }}
        icon_template: mdi:battery

Did you see this Howto create battery alert without creating a template for every device

1 Like

How do these sensors interface with Home Assistant?

try

“{{ trigger.to_state.attributes.friendly_name }} is at {{ trigger.to_state.state }}%”

my understanding

as the automation been trigged there for we should just have to read the trigger state
i think