Template return more than one attribute

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 %}
{% for so in 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') %}
  {% set name = so.name 
            | replace(' Battery', '')
            | replace('[', '')
            | replace(']', '')
            | replace("'", '')
            | replace(' Percentage', '')
            | replace(' percentage', '')
            | replace(' Level', '')
            | replace(",","<br>")
            | replace("\x27","")
            | replace("\x22","") %}
  {% set state = so.state %}
  {{ name }} {{ state }}
{% endfor %}

As a sidebar, this could be optimized seeing that you need to use a for loop anyways.

{% for so in states.sensor
            | selectattr('attributes.device_class', 'defined')
            | selectattr('attributes.device_class', 'eq', 'battery') if so.name is search('iPhone|iPad|MacBook') %}
  {% set state = so.state | int(100) %}
  {% if state < 50 %}
    {% set name = so.name 
            | replace(' Battery', '')
            | replace('[', '')
            | replace(']', '')
            | replace("'", '')
            | replace(' Percentage', '')
            | replace(' percentage', '')
            | replace(' Level', '')
            | replace(",","<br>")
            | replace("\x27","")
            | replace("\x22","") %}
    {{ name }} {{ state }}
  {% endif %}
{% endfor %}
1 Like

you could help yourself and add a ‘battery’ label to those entities you are interested in, and simply use label_entities('battery').
you’d only need to use the name replacer template in that case

hehe, this is what I have for those:

              |map(attribute='name')|sort|join(', ')
              |replace(' sensor Battery','')
              |replace(' draadloze dimmer','')
              |replace(' hygro temp Battery','')
              |replace(' bewegingssensor','')
              |replace(' afstandsbediening','')
              |replace(' door: Battery level','')
              |replace(' Battery Level','')
              |replace(' flood sensor: Battery level','')
              |replace(' dimmer switch battery level','')
              |replace(' dimmer switch Battery','')
              |replace(' Internal','')
              |replace(' Battery','')
              |replace(' Batterij','')}}

which ofc is ridiculous…

            {% set alert_level = states('input_number.battery_alert_level')|int %}
            {%- set ns = namespace(items=[]) -%}

            {%- for s in label_entities('batterij')
              if is_number(states(s)) and states(s)|int < alert_level
              or not has_value(s) -%}
            {%- set ns.items =
                ns.items + [{'name': states[s].name, 'perc': states[s].state|int(-1)}] -%}
                {% else %} Alle batterijen boven {{alert_level}} %
            {%- endfor -%}
            {% if  ns.items|count > 0 %}
            Batterijen: {{ns.items|count}} {{'- '}}
            {%- for i in ns.items|sort(attribute='perc') %}
            {{- i.name}}: {{i.perc}}%
            {%- if not loop.last %}, {% endif -%}
            {% endfor %}
            {% endif %}

if you care to play around a bit more

1 Like

Thanks Petro. Your second one, for some reason, was leaving multiple line breaks when I tested it in DevTools but, no problem, the first one works perfectly, thank you.

Thanks Marius. Labels are something I’ve not yet used so I’ll certainly look more into that.