That almost worked for meā¦ I had to add āunknownā to the rejectattr and to get rid of a few straggling battery_state sensors that would return āchargingā, ādischargingā, etc I switched the device_class-based selecattr for:
{% set x = (states.sensor
| rejectattr('attributes.attribution', 'eq', 'Data provided by Apple iCloud')
| rejectattr('state', 'in', ['unavailable', 'unknown'])
| selectattr('entity_id', 'search', 'battery_level') ) %}
{% set lowest_battery = namespace(entity=0, value=1000) %}
{% for y in x %}
{% if (y.state | int) < lowest_battery.value %}
{% set lowest_battery.value = y.state | int %}
{% set lowest_battery.entity = y %}
{% endif %}
{% endfor %}
{{ lowest_battery.entity.state }}
You seem to know this stuff very well. Are there any write ups or tutorials or is it a matter of trial and error?
I assume this searches for entities with entity id which includes battery_level. Is there a change that an entity is missed? Probably the same could be said for selecting entities which belongs to the device class battery. I found one entity which is of the device class battery and has entity id sensor.tradfri_remote_control.
By the way, just curious, what does the ā-ā in state: > vs state: >- ?
Just like you, Iām no expert and Iām still learningā¦ there are members on this forum who know this stuff way better than me. I just try to help out where I can.
Yes, there is a chance that an entity or two are missed with this template, or any other template. There are a lot of devices that work with Home Assistant, but not all integrations expose device information in the same format. If you have an entity that doesnāt create its own battery_level sensor, there are a couple methods that can be used to have that entity work with this template. I would just create a battery_level sensor using a template sensor.
The difference between ā>ā and ā>-ā can be found here. Either one will work for Home Assistant templates, I use ā>-ā out of habit more than necessity.
Very much appreciated. I learned a lot from going through this exercise.
I still prefer to filter on the device class because if the device class is battery, I can assume state is the battery level. After some polishing I can up with this.
template:
- sensor:
- unique_id: lowest_battery_level
name: >-
{# get list of battery entities #}
{% set battery_entities = (states.sensor | rejectattr('attributes.attribution', 'eq', 'Data provided by Apple iCloud') | rejectattr('state', 'in', ['unavailable', 'unknown']) | selectattr('attributes.device_class', 'eq', 'battery') ) %}
{# get entity with lowest battery state #}
{% set lowest_battery = namespace(entity=0, value=101) %}
{% for x in battery_entities %}
{% if (x.state | int) < lowest_battery.value %}
{% set lowest_battery.value = x.state | int %}
{% set lowest_battery.entity = x %}
{% endif %}
{% endfor %}
{% if lowest_battery.entity %}
{{ lowest_battery.entity.name }}
{% else %}
{{ "unknown" }}
{% endif %}
state: >-
{# get list of battery entities #}
{% set battery_entities = (states.sensor | rejectattr('attributes.attribution', 'eq', 'Data provided by Apple iCloud') | rejectattr('state', 'in', ['unavailable', 'unknown']) | selectattr('attributes.device_class', 'eq', 'battery') ) %}
{# get entity with lowest battery state #}
{% set lowest_battery = namespace(entity=0, value=101) %}
{% for x in battery_entities %}
{% if (x.state | int) < lowest_battery.value %}
{% set lowest_battery.value = x.state | int %}
{% set lowest_battery.entity = x %}
{% endif %}
{% endfor %}
{% if lowest_battery.entity %}
{{ lowest_battery.entity.state }}
{% else %}
{{ "unknown" }}
{% endif %}
unit_of_measurement: "%"
icon: >-
{% set battery_level = states.sensor.lowest_battery_level.state|default(0)|int %}
{% set battery_round = (battery_level / 10) |int * 10 %}
{% if battery_round >= 100 %}
mdi:battery
{% elif battery_round > 0 %}
mdi:battery-{{ battery_round }}
{% else %}
mdi:battery-alert
{% endif %}
The only thing I am not have with is that I need to filter and iterate through the entities multiple times and wish there is a way to do that once and refer to the resulting entity multiple times.
There are a few feature request posts to have the ability to use variables added to Template Sensorsā¦ but until that happens youāre only other option is:
I have a follow up question regarding trigger based sensors. The documentation for trigger sensors states: trigger list (optional) Define an automation trigger to update the entities. Optional. If omitted will update based on referenced entities.
What would be the difference between