template:
- sensor:
- name: "Lowest Battery Level"
unit_of_measurement: "%"
state_class: measurement
device_class: battery
state: >
{% set x = states.sensor | rejectattr('attributes.attribution', 'eq', 'Data provided by Apple iCloud') | selectattr('attributes.device_class', 'eq', 'battery') | rejectattr('state', 'in', ['unavailable', 'unknown', 'discharging', '100']) | sort( attribute = 'state') | map(attribute='entity_id') | first %}
{{states(x)}}
I am making good progress with the template sensor, but somehow it does not return the sensor information I expected. It returns a sensor with battery 100% while one of the sensors have battery 50%.
STATE: 50.0
ATTRIBUTES:
state_class: measurement
unit_of_measurement: '%'
friendly_name: 'MultiSensor 6: Battery level'
device_class: battery
Any suggestions on how to troubleshoot?
Also, I want to assign the friendly_name of that sensor to the friendly_name of the template sensor. HA complains friendly_name is not a valid option.
state: >
{% set x = states.sensor | rejectattr('attributes.attribution', 'eq', 'Data provided by Apple iCloud') | selectattr('attributes.device_class', 'eq', 'battery') | rejectattr('state', 'in', ['unavailable', 'unknown', 'discharging', '100']) | sort( attribute = 'state') | map(attribute='entity_id') | first %}
{{states(x)}}
friendly_name: >
{% set x = states.sensor | rejectattr('attributes.attribution', 'eq', 'Data provided by Apple iCloud') | selectattr('attributes.device_class', 'eq', 'battery') | rejectattr('state', 'in', ['unavailable', 'unknown', 'discharging', '100']) | sort( attribute = 'state') | map(attribute='entity_id') | first %}
{{state_attr(x, 'friendly_name')}}