Xiaomi battery level making a "sensor" template for binary sensor

Hi. Having some challenges with adding a xiaomi binary sensor to my Battery level group.
I know that for sensors it work, but i get an error when i use the same logic for binary sensors:
I use HA 0.60.1 and python 3

  • platform: template
    sensors:
    koekken_sensor_battery:
    value_template: >-
    {%- if states.sensor.humidity_158d0001e798bb -%}
    {{ states.sensor.humidity_158d0001e798bb.attributes.battery_level }}
    {%- else -%}
    n/a
    {%- endif -%}
    unit_of_measurement: ‘%’
    entity_id: sensor.humidity_158d0001e798bb

  • platform: template
    binary_sensors:
    door_switch_battery:
    value_template: >-
    {%- if states.binary_sensor.door_window_sensor_158d0001a21a0a -%}
    {{ states.binary_sensor.door_window_sensor_158d0001a21a0a.battery_level }}
    {%- else -%}
    n/a
    {%- endif -%}
    unit_of_measurement: ‘%’
    entity_id: binary_sensor.door_window_sensor_158d0001a21a0a

Error i get:
Failed config
sensor.template:
platform: template
binary_sensor: [source /home/homeassistant/.homeassistant/configuration.yaml:232]
door_switch_battery: [source /home/homeassistant/.homeassistant/configuration.yaml:233]
entity_id: binary_sensor.door_window_sensor_158d0001a21a0a
unit_of_measurement: %
value_template: {%- if states.binary_sensor.door_window_sensor_158d0001a21a0a -%}
{{ states.binary_sensor.door_window_sensor_158d0001a21a0a.battery_level }}
{%- else -%}
n/a
{%- endif -%}

Regards, Kenneth

Hi @kalpcc

Isn’t supposed to be “sensors” also for binary_sensor? Like this:

- platform: template 
  sensors:

Also, use the </> markings when posting code.

I tried that, an got an error.

battery_level is an attribute (look at your regular sensor)

Anyway, some comments:

  • it’s difficult to understand the code without indentation (HA is sensitive in this regard); use the </> in the edit window;
  • what is your intent? binary_ sensor has 2 states (on/off; open/closed; etc);

I use this code

  - platform: template
    sensors:
      battery_bedroom_switch:
        friendly_name: 'Bedroom Switch'
        value_template: "{{ states.binary_sensor.switch_158d000110356c.attributes.battery_level|default(-1)|int if states.binary_sensor.switch_158d000110356c is not none }}"
        unit_of_measurement: '%'
        icon_template: '{% if states.binary_sensor.switch_158d000110356c.attributes.battery_level == 100 %}
                           mdi:battery
                        {% elif states.binary_sensor.switch_158d000110356c.attributes.battery_level >= 90 %}
                           mdi:battery-90
                        {% elif states.binary_sensor.switch_158d000110356c.attributes.battery_level >= 80 %}
                           mdi:battery-80
                        {% elif states.binary_sensor.switch_158d000110356c.attributes.battery_level >= 70 %}
                           mdi:battery-70
                        {% elif states.binary_sensor.switch_158d000110356c.attributes.battery_level >= 60 %}
                           mdi:battery-60
                        {% elif states.binary_sensor.switch_158d000110356c.attributes.battery_level >= 50 %}
                           mdi:battery-50
                        {% elif states.binary_sensor.switch_158d000110356c.attributes.battery_level >= 40 %}
                           mdi:battery-40
                        {% elif states.binary_sensor.switch_158d000110356c.attributes.battery_level >= 30 %}
                           mdi:battery-30
                        {% elif states.binary_sensor.switch_158d000110356c.attributes.battery_level >= 20 %}
                           mdi:battery-20
                        {% elif states.binary_sensor.switch_158d000110356c.attributes.battery_level >= 10 %}
                           mdi:battery-10
                        {% else %}
                           battery-outline
                        {% endif %}'

grgergrgssgd

3 Likes

Try this (check in dev-state that it is the correct entity). You can add more sensors of the same type. I assume you have split your configuration.yaml so you need to use proper indentation. I think it catches all errors (if state is unknown it will show 110% otherwise the charge percentage should be fine).

- platform: template
  sensors:
    x_od1bat:
      friendly_name: Xiaomi OD1 Battery Level
      value_template: >
        {% if is_state('binary_sensor.door_window_sensor_158d0001a21a0a', 'unknown') %}
          110
        {% else %}
          {{ states.binary_sensor.door_window_sensor_158d0001a21a0a.attributes["battery_level"] | float}}
        {% endif %}
      icon_template: > 
        {% set battery_level_xod1 = states.binary_sensor.door_window_sensor_158d0001a21a0a.attributes.battery_level|default(0)|int %}
        {% set battery_round_xod1 = (battery_level_xod1 / 10) |int * 10 %}
        {% if battery_round_xod1 >= 100 or is_state('binary_sensor.door_window_sensor_158d0001a21a0a', 'unknown') %}
          mdi:battery
        {% elif battery_round_xod1 > 0 %}
          mdi:battery-{{ battery_round_xod1 }}
        {% else %}
          mdi:battery-charging-wireless-outline
        {% endif %}
      unit_of_measurement: '%'

Tanks :slightly_smiling_face: That did the trick. And Sorry for NOT using </> I AM a newbee :grimacing:

Regards,
Kenneth

You’re welcome

I’m trying to implement the same, but I walways get the error:

"2018-03-25 20:06:25 ERROR (MainThread) [homeassistant.config] Invalid config for [binary_sensor.template]: [Bateria_Porta] is an invalid option for [binary_sensor.template]. Check: binary_sensor.template->sensors->Bateria_Porta. (See ?, line ?). Please check the docs at https://home-assistant.io/components/binary_sensor.template/
"

I have the following configuration:

binary_sensor:
  - platform: template
    sensors:
      Bateria_Porta:
        friendly_name: Bateria Porta
        value_template: >
         {% if is_state('binary_sensor.door_window_sensor_158d0001211b69', 'unknown') %}
          110
         {% else %}
          {{ states.binary_sensor.door_window_sensor_158d0001211b69.attributes["battery_level"] | float}}
         {% endif %}
      icon_template: > 
        {% set battery_level_xod1 = states.binary_sensor.door_window_sensor_158d0001211b69.attributes.battery_level|default(0)|int %}
        {% set battery_round_xod1 = (battery_level_xod1 / 10) |int * 10 %}
        {% if battery_round_xod1 >= 100 or is_state('binary_sensor.door_window_sensor_158d0001211b69', 'unknown') %}
          mdi:battery
        {% elif battery_round_xod1 > 0 %}
          mdi:battery-{{ battery_round_xod1 }}
        {% else %}
          mdi:battery-charging-wireless-outline
        {% endif %}
      unit_of_measurement: '%'

Can anyone help me?

Thanks in advance

Hi,

You need to have a look at the spacing as YAML is very sensitive. Cannot post from phone but all lines starting with icon_template down to end need two additional spaces (not tabs).

I checked in an Online Parser (http://yaml-online-parser.appspot.com/) and seems all ok… I don’t understand what I’m doing wrong.

This device I’m trying to get the battery info it’s a door sensor. I have a quetion still: Should be “binary_sensor:” or “sensor:”?

Add two spaces in front of all lines starting icon_template

binary_sensor:
  - platform: template
    sensors:
      Bateria_Porta:
        friendly_name: Bateria Porta
        value_template: >
          {% if is_state('binary_sensor.door_window_sensor_158d0001211b69', 'unknown') %}
            110
          {% else %}
            {{ states.binary_sensor.door_window_sensor_158d0001211b69.attributes["battery_level"] | float}}
          {% endif %}
        icon_template: > 
          {% set battery_level_xod1 = states.binary_sensor.door_window_sensor_158d0001211b69.attributes.battery_level|default(0)|int %}
          {% set battery_round_xod1 = (battery_level_xod1 / 10) |int * 10 %}
          {% if battery_round_xod1 >= 100 or is_state('binary_sensor.door_window_sensor_158d0001211b69', 'unknown') %}
            mdi:battery
          {% elif battery_round_xod1 > 0 %}
            mdi:battery-{{ battery_round_xod1 }}
          {% else %}
            mdi:battery-charging-wireless-outline
          {% endif %}
        unit_of_measurement: '%'
1 Like

Hi

Thanks for the code, but it didn’t work out of the box.
I read the documentation for ‘Template Binary Sensor’ and ist says: “The state of a Template Binary Sensor can only be on or off”.
Next I used in ‘sensor’ section and it works perfectly. Now I see the correct value of ‘battery_level’ and symbol.

1 Like

My bad. Phone is not ideal for posting formated text.

It’s working now!!! Had to change from binary_sensor to sensor, having the correct formated YAML and remove the Capital letters as well.

This is what I have now:

sensor:
  - platform: yr

  - platform: template
    sensors:
      bateria_porta:
        friendly_name: Bateria Porta
        value_template: >
          {% if is_state('binary_sensor.door_window_sensor_158d0001211b69', 'unknown') %}
            110
          {% else %}
            {{ states.binary_sensor.door_window_sensor_158d0001211b69.attributes["battery_level"] | float}}
          {% endif %}
        icon_template: >
          {% set battery_level_xod1 = states.binary_sensor.door_window_sensor_158d0001211b69.attributes.battery_level|default(0)|int %}
          {% set battery_round_xod1 = (battery_level_xod1 / 10) |int * 10 %}
          {% if battery_round_xod1 >= 100 or is_state('binary_sensor.door_window_sensor_158d0001211b69', 'unknown') %}
            mdi:battery
          {% elif battery_round_xod1 > 0 %}
            mdi:battery-{{ battery_round_xod1 }}
          {% else %}
            mdi:battery-charging-wireless-outline
          {% endif %}
        unit_of_measurement: '%'

Thanks @ReneR and @Petrica for the help :wink:

2 Likes

It works. I added a new file batteries.yaml and load it with

!include batteries.yaml