Template for binary sensor sonoff zigbee device

Hi All,

I have connected my Sonoff zigbee bridege to my home assitant using this component: https://github.com/AlexxIT/SonoffLAN
I add on the ewlink app one door sensor, and all looks working ok.doorsensor1
Also diging in the properties of the new sensor I found this on the development page:
doorsensor2

So i want to create a sensor/template that shows the battery level of this.
I have try the templates with no results, my last attempt is this one:

sensor 17:
  - platform: template
    sensors:
      bat5:
        friendly_name: "bat_5"
        unit_of_measurement: '%'
        value_template: '{{ states.sensor.binary_sensor.sonoff_a4400008d0.attributes.battery_level }}'
        icon_template: mdi:battery

I have change the following line in 2 different ways:

value_template: '{{ states.sensor.binary_sensor.sonoff_a4400008d0.attributes.battery_level }}'
value_template: '{{ states.sensor.binary_sensor.sonoff_a4400008d0.attributes.battery }}'

And i got this after restarting HA with any of the two previews options:
doorsensor3

I haven’t try any templates before, I have that code pasted in my config.yaml file.
If more information is needed let me know.
I just want a sensor that shows the battery level of this component.

I have my HA running in a virtual machine on proxmox from the official image.

Check out the templating docs.

You should avoid usibg this notation states.binary_sensor.xxx and rather use this notation states(‘binary_sensor.xx’) or state_attr(‘binary_sensor.xxx’, ‘attribute’) in this case, as mentioned in the warning here.

You’re mistake is that you use statea.sensor.xxx instead of states.binary_sensor.xxx, you want the info from a binary_sensor.

Try this:

value_template: "{{ state_attr('binary_sensor.sonoff_a4400008d0', 'battery') }}"

You can always test your templates from the sidebar -> Developer Tools -> Templates.

Thanks, that makes the trick. Was not sure how to use that part of the documentation, as all the examples I found on theb log have the other notation.

Thanks for your fast response.