Need help with Automation triggered by device tracker battery level

With the help of others on here I have been able to trouble shoot and get working an ESP-01s with v1 relay (desoldering a resistor was required) but I’m now facing another problem.

I’ve had a search thought the docs and through the posts here (but I’m not sure how to word what I am looking for) with limited success.

my aim:

to have an automation turn a switch on or off depending on the percentage battery level of my phone.

the battery level can be obtained from my device tracker as an atribute

device_tracker.robin 

source_type: gps 
battery_level: 85

but how do I trigger an automation based on an atribute?

here is what I have tried without success…

- id: '0000000000029'
  alias: Charge Robins Phone on
  initial_state: 'true'
  trigger:
    platform: numeric_state
    entity_id: device_tracker.robin
    value_template: "{{ device_tracker.robin.battery_level }}"
    below: 95
  action:
    - service: switch.turn_on
      entity_id: switch.robins_charger

- id: '0000000000030'
  alias: Charge Robins Phone off
  initial_state: 'true'
  trigger:
    platform: numeric_state
    entity_id: device_tracker.robin
    value_template: "{{ device_tracker.robin.battery_level }}"
    above: 99
  action:
    - service: switch.turn_off
      entity_id: switch.robins_charger

if I manually trigger the automation it works, so I know its my trigger thats the problem… but I have no idea what I am doing and honestly I dont understand.

can anyone help me? even if its just helping me to know what I am looking for…

thank you for your help

Based on the example provided for Numeric State Trigger, change this:

    value_template: "{{ device_tracker.robin.battery_level }}"

to this:

    value_template: "{{ state.attributes.battery_level }}"

thank you,

thats where I was going wrong I thought I had to replace my details into the example…

thank you

1 Like

I like this, and a better solution:

value_template: “{{ state_attr(‘device_to_motion’, ‘battery_level’) }}”

not?