question:
I want to check the percentage of all the batteries.
so, with state.names() I get all entities. But I would like t get all the entites who have a device_class “battery”. I would rather not test on the names of the entities, but get entities with device_class “battery”.
Any idea?
update: I came up with this: (I still need to add the check for the minumum percentage, but, the main thing here is to search for entities who contain battery data)
@service
def check_batteries(min_perc=None):
sensors=state.names("sensor")
for sensor in sensors:
sensor_attr=state.getattr(sensor)
if "device_class" in sensor_attr and sensor_attr["device_class"]=="battery":
battery_level=state.get(sensor)
notify.persistent_notification(message=f'found battery level {sensor} {battery_level}',title="test notify title")
I was wondering, that, if there’s shorter way.