How to Create Sensors for Batteries from other Entity Attributes

Is it possible to create new battery sensor from an existing attribute?

E.g. from my hive devices

Txs

1 Like

Ok figured it out - here if it helps someone else

sensor:
  - platform: template
    sensors:
      hive_underfloor_battery_level:
        friendly_name: "Hive Underfloor Battery"
        value_template: '{{ states.climate.underfloor_heating.attributes.battery_level|replace("%","")}}'
        device_class: battery
      hive_central_heating_battery_level:
        friendly_name: "Hive Central Heating Battery"
        value_template: '{{ states.climate.central_heating.attributes.battery_level|replace("%","")}}'
        device_class: battery
3 Likes

Wow, so grateful you posted this, solving it yourself! It just saved me a ton of time! :trophy:

Hey all,

Thanks for the solution! Update for current home assistant 2022.8, adding to the statistics by using state_class so you can track how long your batteries last :zap:

template:
  - sensor:
      - name: "My Sensor Battery Level"
        unique_id: my_unique_id_123  # optional; allows more configuration to entity e.g. icon customization to occur via UI
        state: "{{ state_attr('sensor.my_sensor', 'battery_level') }}"
        unit_of_measurement: "%"
        state_class: measurement
        device_class: battery

Note: if using state_attr() in the template, the sensor will be “unknown” until home assistant starts. This can be useful to ensure that incorrect values aren’t reported from an entity until it’s initialized.
Reference

3 Likes