Hi Alex,
I also couldn’t create a sensor with the battery value of the hive via ZHA.
Seems, the function zha.read_zigbee_cluster_attribute
isn’t implemented yet (see also topics here in the forum and on Github)
A possible solution is to install the HACS-integration zha-toolkit with its function zha_toolkit.attr_read
.
An automation reads the battery level periodically, writes the value into a helper (input_number), which is used as base for the battery-sensor (helper/template-sensor):
- Add helper “number” (range 0-200):
input_number.hive_battery_measure
- Add helper “template” (sensor):
sensor.hive_battery
with state template:
{{ (states('input_number.hive_battery_measure') | int / 2 ) | round(0) }}
- Add automation:
alias: Hive Battery
description: ""
trigger:
- platform: time_pattern
hours: /1
condition:
- alias: Test if available
condition: not
conditions:
- condition: or
conditions:
- condition: state
entity_id: light.hive
state: unknown
- condition: state
entity_id: light.hive
state: unavailable
action:
- service: zha_toolkit.attr_read
data:
ieee: light.hive
endpoint: 1
cluster: 1
attribute: 33
state_id: input_number.hive_battery_measure
mode: single
The automation reads the battery value once an hour, what seems enough for me and doesn’t drain the battery too much. To prevent errors, a condition checks whether the lamp is (not) available. The attribute 33
is the decimal equivalent for hex 0x0021
, the id of Hives cluster attribute battery_percentage_remaining
.
In the template, the measured battery value is converted into an integer percentage value (division by 2).
Apparently the battery level changes in steps of 20, the sensor accordingly by 10%.