Creating a custom string from a group of items

I have a group with the following items:

ibbq_probe_fault:

  • binary_sensor.ibbq_probe_1_fault
  • binary_sensor.ibbq_probe_2_fault
  • binary_sensor.ibbq_probe_3_fault
  • binary_sensor.ibbq_probe_4_fault

I want to create a string that takes all the items in that group that have a state = off
and give me the following:

lets say binary_sensor.ibbq_probe_3_fault = on

P1: sensor.ibbq_probe_1 (the value from this entity)
P2: sensor.ibbq_probe_2
P4: sensor.ibbq_probe_4

lets say binary_sensor.ibbq_probe_1_fault = on

P2: sensor.ibbq_probe_2 (the value from this entity)
P3: sensor.ibbq_probe_3
P4: sensor.ibbq_probe_4

and so onā€¦
I want to then use this string as an output on my lovelace view

Copy-paste the following template into the Template Editor and confirm it reports the correct information.

If it works correctly, but the line spacing isnā€™t what you want, add or remove the hyphens in {%- and {-%} to see how they control whitespace and line breaks. Adjust them to suit your requirements and then use the template in a Markdown card.

{%- set probes_all = ['binary_sensor.ibbq_probe_1_fault', 'binary_sensor.ibbq_probe_2_fault', 
  'binary_sensor.ibbq_probe_3_fault', 'binary_sensor.ibbq_probe_4_fault'] -%}
{%- set probes_off = expand(probes_all) | selectattr('state', 'ne', 'on') | map (attribute='entity_id') | list -%}
{% for p in probes_off -%}
P{{p.split('_')[3]}}: {{p[:-6]}}
{% endfor -%}

EDIT

Correction. Wrong variable name. Replaced probes with probes_off.

I had to fix this since I believe probes in your code is actually probes_all but that gives:

Template editor

Result type: string

P1: binary_sensor.ibbq_probe_1
P2: binary_sensor.ibbq_probe_2
P3: binary_sensor.ibbq_probe_3
P4: binary_sensor.ibbq_probe_4

All were ā€œoffā€ except P3 which was ā€œonā€ yet it lists all. Also I wanted the actual state of binary_sensor.ibbq_probe_1, binary_sensor.ibbq_probe_2, binary_sensor.ibbq_probe_3, binary_sensor.ibbq_probe_4 not the nameā€¦

Try the corrected version that uses two variables whose names are probes_all and probes_off.

You can also try this version in the Template Editor which displays more information about the templateā€™s inner workings.

{%- set probes_all = ['binary_sensor.ibbq_probe_1_fault', 'binary_sensor.ibbq_probe_2_fault', 
  'binary_sensor.ibbq_probe_3_fault', 'binary_sensor.ibbq_probe_4_fault'] -%}
{{ probes_all }}

{{ expand(probes_all) | map('state') | list }}

{%- set probes_off = expand(probes_all) | selectattr('state', 'ne', 'on') | map (attribute='entity_id') | list -%}
{{ probes_off }}

{% for p in probes_off -%}
{{ p }}
P{{p.split('_')[3]}}: {{p[:-6]}}
{% endfor -%}

Wonā€™t that simply be off for all of them given that itā€™s what you want the template to extract (only the entities that are not on)? If thatā€™s what you want then you can just use this:

{%- set probes_all = ['binary_sensor.ibbq_probe_1_fault', 'binary_sensor.ibbq_probe_2_fault', 
  'binary_sensor.ibbq_probe_3_fault', 'binary_sensor.ibbq_probe_4_fault'] -%}
{%- set probes_off = expand(probes_all) | selectattr('state', 'eq', 'off') | map (attribute='entity_id') | list -%}
{% for p in probes_off -%}
P{{p.split('_')[3]}}: {{p[:-6]}} off
{% endfor -%}

If you donā€™t want the name at all just remove {{p[:-6]}}.

Each probe (there are 4 of them) has 2 entities:
binary_sensor.ibbq_probe_1_fault = on/off
sensor.ibbq_probe_1 = temperature from a sensor

So the output should be for example:

P1: 45 (the actual temp from sensor.ibbq_probe_1)
P2: 55
P3: 65
P4: 23

Thanks for the clarification. I think I now understand what you want.

{%- set probes_all = ['binary_sensor.ibbq_probe_1_fault', 'binary_sensor.ibbq_probe_2_fault', 
  'binary_sensor.ibbq_probe_3_fault', 'binary_sensor.ibbq_probe_4_fault'] -%}
{%- set probes_off = expand(probes_all) | selectattr('state', 'eq', 'off') | map(attribute='object_id') | list -%}
{% for p in probes_off -%}
P{{p.split('_')[2]}}: {{states('sensor.' ~ p[:-6])}}
{% endfor -%}

Nice! It worked, just need to figure out how to make it P1, P2, P3 rather than P3,P2,P1ā€¦

Result type: string

P3: 73.7
P2: 71.8
P1: 74.9

Tried to add sort(attribute=ā€˜stateā€™, reverse=true) but not working

Got it:

{% for p in probes_off | reverse() -%}
P{{p.split('_')[2]}}: {{states('sensor.' ~ p[:-6])}}
{% endfor -%}
1 Like

The reverse ordering is a byproduct of using the expand function. Add sort to the template that calculates probes_off as shown below:

{%- set probes_off = expand(probes_all) | selectattr('state', 'eq', 'off') | map(attribute='object_id') | sort | list -%}

EDIT

Or the way you did it. Same difference; undo the reverse ordering caused by expand.

1 Like

That worked as well

Funny thing in frontend it shows it in one line rather than one under the orherā€¦

image

I tried adding {{- ā€˜\nā€™ -}} but did not fix it.

{% for p in probes_off -%}
P{{p.split('_')[2]}}: {{states('sensor.' ~ p[:-6])}}{{- '\n' -}}
{% endfor -%}

Ok found it:


                    - type: markdown
                      card_mod:
                        style: |
                          ha-card {
                            font-size: 12px !important;
                            font-weight: 400;
                          }
                      content: '{{ states(''sensor.ibbq_probe_all_temps'')|replace(",","\n") }}'