I have the below template sensor which lists the friendly name but I also want to show the state alongside. How can I do this?
(state needs to show as %)
{{ states.sensor
| selectattr('attributes.device_class', 'defined')
| selectattr('attributes.device_class', 'eq', 'battery')
| selectattr('state', 'lt', '50')
| rejectattr('state', 'in', '100')
| rejectattr('name', 'search', 'iPhone')
| rejectattr('name', 'search', 'iPad')
| rejectattr('name', 'search', 'MacBook')
| map(attribute='name')
| list
| replace(' Battery', '')
| replace('[', '')
| replace(']', '')
| replace("'", '')
| replace(' Percentage', '')
| replace(' percentage', '')
| replace(' Level', '')
| replace(",","<br>")
| replace("\x27","")
| replace("\x22","")
}}
I do actually have an alternative template which returns both name and state but I can’t figure out how to include the replace
from the above template, so an amendment to either of these two templates is acceptable for what I require.
{% set batteries = states.sensor
| selectattr('attributes.device_class', 'defined')
| selectattr('attributes.device_class', 'eq', 'battery')
| rejectattr('state', 'in', ['unknown', 'unavailable'])
| rejectattr('name', 'search', 'iPad')
| selectattr('state', 'ne', '100')
| selectattr('state', 'le', '50')
| selectattr('state', 'ge', '0')
%}
{% for battery in batteries -%}
{{ state_attr(battery.entity_id, 'friendly_name') }} {{ states(battery.entity_id)}}%
{% endfor %}