Markdown - output heading only if at least one query applies

Hello,
I have search here in the community and in the documation for an oppertunity. But I don´t find anything.

For example I have the following Code with 5 if is_state queries

# Bewegung
{% if is_state('binary_sensor.flur_bewegungssensor_motion', 'on') %} ## Bewegung OG Flur erkannt {% else %}{% endif %}
{% if is_state('binary_sensor.bad_bewegungssensor_motion', 'on') %} ## Bewegung OG Bad erkannt {% else %}{% endif %}
{% if is_state('binary_sensor.kuche_bewegunssensor_motion', 'on') %} ## Bewegung OG Küche erkannt {% else %}{% endif %}
{% if is_state('binary_sensor.schlafzimmer_bewegungssensor_motion', 'on') %} ## Bewegung OG Schlafzimmer erkannt {% else %}{% endif %}
{% if is_state('binary_sensor.homematicip_bewegungsmelder_innen', 'on') %} ## Bewegung EG Flur erkannt {% else %}{% endif %}

I would like that the headline #Bewegung is only displayed if at least one if is_state query applies

Can me helf anyone how get the right result?

you can use a conditional card in conjunction with the markdown card.

Here is an example of mine:

      - type: conditional
        conditions:
          - entity: sensor.nws_alerts
            state_not: '0'
        card:
          type: markdown
          content: >
            ---
            
            # NWS First Active Alert
              {% if state_attr('sensor.nws_alerts', 'title') != None and state_attr('sensor.nws_alerts', 'title').split(' - ')[0] is defined  %}
                
            ## {{ states.sensor.nws_alerts.attributes.title.split(' - ')[0] }}

              {{ state_attr('sensor.nws_alerts', 'display_desc').split('\n\n-\n\n')[0] }}

              {% else %}
                none
              {% endif %}

it only displays the card if the condition is met.

Try this:

{% set x = expand('binary_sensor.flur_bewegungssensor_motion', 'binary_sensor.bad_bewegungssensor_motion', 'binary_sensor.kuche_bewegunssensor_motion', 'binary_sensor.schlafzimmer_bewegungssensor_motion', 'binary_sensor.homematicip_bewegungsmelder_innen' )
   | selectattr('state', 'eq', 'on')
   | map(attribute='name') | list %}
{{ 'Bewegung ' ~ x | join(', ') if x | length > 0 else '' }}

If that works the way you want, consider creating a group containing the five motion sensors. Then the template can be reduced to this:

{% set x = expand('group.bewegungssensor_motion' )
   | selectattr('state', 'eq', 'on')
   | map(attribute='name') | list %}
{{ 'Bewegung ' ~ x | join(', ') if x | length > 0 else '' }}

Thanks this works.

But I have one another question.

If I want to put a
***
in front ob the headline Bewegung.

I have try this Code:

{{ '<br> *** <br> # Bewegung' if x | length > 0 else '' }}

But this code get out

***
# Bewegung

What I must change?

And one question.
What I must change in this part of the code
| selectattr('state', 'eq', 'on')

If i want not check if is on but i whant check if the state is > 21. ?

The <br> creates a newline so remove the one between *** and # Bewegung.

You cannot simply do this:

states.sensor | selectattr('state', 'gt', 21)

because an entity’s state value is a string.

Sorry, but this two codes don`t work.

If I use this Code:

{% set x = expand(
'sensor.netatmo_haus_buro_og_wohnzimmer_og_co2',
'sensor.netatmo_haus_buro_og_schlafzimmer_og_co2',
'sensor.netatmo_haus_buro_og_bad_og_co2',
'sensor.netatmo_haus_buro_og_co2',
'sensor.netatmo_haus_buro_anbau_oben_co2',
'sensor.netatmo_haus_buro_wohnzimmer_co2',
'sensor.netatmo_haus_buro_schlafzimmer_co2',
'sensor.netatmo_haus_buro_co2'
)
   | selectattr('state', 'gt', 1000)
   | map(attribute='name') | list %}
{{  '# Luftqualität'  if x | length > 0 else '' }}

I got an error:
TypeError: ‘>’ not supported between instances of ‘str’ and ‘int’

I think my mistake is here with the > 0:
{{ '# Luftqualität' if x | length > 0 else '' }}

But I dont´t know how to change ist.

And if I use this code for the break and line:
{{ '<br> *** # Bewegung' if x | length > 0 else '' }}

The output is:
*** # Bewegung
the *** and # is in the output.

Sorry if I understand some thinks wrong.

I know they don’t work. I explained you cannot use that.

Sorry for the Missunderstanding.

So they is no way to do what I want?

Which one of your questions are you referring to?

I believe I have answered your original question which was to display nothing in the Markdown Card if none of the binary_sensors is on.

I think I also answered your question about how to display *** # Bewegung all on one line and only if at least one binary_sensor is on.

Are you referring to your third question concerning how to display sensors whose value is greater than 21?

If you are, the try this:

{% set x = expand(
'sensor.netatmo_haus_buro_og_wohnzimmer_og_co2',
'sensor.netatmo_haus_buro_og_schlafzimmer_og_co2',
'sensor.netatmo_haus_buro_og_bad_og_co2',
'sensor.netatmo_haus_buro_og_co2',
'sensor.netatmo_haus_buro_anbau_oben_co2',
'sensor.netatmo_haus_buro_wohnzimmer_co2',
'sensor.netatmo_haus_buro_schlafzimmer_co2',
'sensor.netatmo_haus_buro_co2'
)
   | map(attribute='state')
   | map('float')
   | select('gt', 1000) | list %}
{{  '# Luftqualität'  if x | length > 0 else '' }}

However, the variable x will not contain the names of the sensors, only their values.

The Question with display nothing in the Markdown Card if none of the binary_sensors is on. – > is fine and works

The question display *** # Bewegung:
I would linke to display *** # Bewegung only if at least one binary-sensor is on.

Maybe I have s solution, not fine, but it works.

I put the same code a secound time (but this time replace I the # Bewegung into ***

`{% set x = expand(
‘binary_sensor.flur_bewegungssensor_motion’,
‘binary_sensor.bad_bewegungssensor_motion’,
‘binary_sensor.kuche_bewegunssensor_motion’, ‘binary_sensor.schlafzimmer_bewegungssensor_motion’, ‘binary_sensor.homematicip_bewegungsmelder_innen’
)
| selectattr(‘state’, ‘eq’, ‘on’)
| map(attribute=‘name’) | list %}
{{ ’ *** ’ if x | length > 0 else ‘’ }}

{% set x = expand(
‘binary_sensor.flur_bewegungssensor_motion’,
‘binary_sensor.bad_bewegungssensor_motion’,
‘binary_sensor.kuche_bewegunssensor_motion’, ‘binary_sensor.schlafzimmer_bewegungssensor_motion’, ‘binary_sensor.homematicip_bewegungsmelder_innen’
)
| selectattr(‘state’, ‘eq’, ‘on’)
| map(attribute=‘name’) | list %}
{{ ‘# Bewegung’ if x | length > 0 else ‘’ }}`

The question display sensor whose value is greater than I don`t know how to do

I hope know my questions are clearer.

Sorry for my bad english.

Sorry maybe you edit you post or I over read the solution.

I Think the greater than works with this code.

That’s what this does:

{{ '<br> *** # Bewegung' if x | length > 0 else '' }}

If the length of the variable x is greater than zero it will display *** # Bewegung on a new line, otherwise it will display nothing in the Markdown Card. If you want *** on one line and #Bewugung on a second line, use this:

{{ '<br> *** <br> # Bewegung' if x | length > 0 else '' }}

Here’s a screenshot of the result:
Screenshot from 2021-08-16 16-38-30

I provided an example of how to do it in my previous post.

As you can see in your screenshot, the *** # is displayed with this code. Normally in Markdown a line is displayed with *** and with # the word behind it is printed in large letters.

For Example

Am I getting something wrong with your code?

The question diplay sensor whose value is greater wirth your example works well.

But now I have to this Code one last question.

I have a mixed were I need for some sensors

| select('lt', 10) | list %}
and for some other sensors
| selectattr('state', 'eq', 'on')

And I one sensor with lt 10 or one sensor eq on is treu the Headline should be display.

How can i combine this two selects?

To get that result, you have to put a literal newline after *** not a <br> and the *** and # must appear in the template without any leading spaces.

That’s good news. Please consider marking my post above with the Solution tag. It will automatically place a checkmark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

{% set x = expand(
    'binary_sensor.flur_bewegungssensor_motion',
    'binary_sensor.bad_bewegungssensor_motion',
    'binary_sensor.kuche_bewegunssensor_motion',
    'binary_sensor.schlafzimmer_bewegungssensor_motion',
    'binary_sensor.homematicip_bewegungsmelder_innen'
    )
    | selectattr('state', 'eq', 'on')
    | map(attribute='name') | list %}
{% if x | length > 0 %}
***
# Bewegung
{% else %}
  {% set x = expand(
    'sensor.netatmo_haus_buro_og_wohnzimmer_og_co2',
    'sensor.netatmo_haus_buro_og_schlafzimmer_og_co2',
    'sensor.netatmo_haus_buro_og_bad_og_co2',
    'sensor.netatmo_haus_buro_og_co2',
    'sensor.netatmo_haus_buro_anbau_oben_co2',
    'sensor.netatmo_haus_buro_wohnzimmer_co2',
    'sensor.netatmo_haus_buro_schlafzimmer_co2',
    'sensor.netatmo_haus_buro_co2'
    )
    | selectattr('state', 'gt', 1000)
    | map(attribute='name') | list %}
    {% if x | length > 0 %}
***
# Luftqualität
    {% endif %}
{% endif %}

The Code for the line and lager letters works.

But the Code wich I take for combine two selects don’t work.

I try this Code:

{% set x = expand(
    'binary_sensor.homematicip_alarmsirene_battery',
    'binary_sensor.homematicip_schlusselbundfernbedienung_alarm_battery',
    'binary_sensor.homematicip_heizkorperthermostat_battery',
    'binary_sensor.homematicip_fenster_battery',
    'binary_sensor.homematicip_fenster_battery_3',
    'binary_sensor.homematicip_heizkorperthermostat_battery_2',
    'binary_sensor.homematicip_fenster_und_turkontakt_mit_magnet_battery_2',
    'binary_sensor.homematicip_heizkorperthermostat_battery_3',
    'binary_sensor.homematicip_fenster_battery_2',
    'binary_sensor.homematicip_heizkorperthermostat_battery_5',
    'binary_sensor.homematicip_fenster_und_turkontakt_mit_magnet_battery_3',
    'binary_sensor.homematicip_heizkorperthermostat_battery_4',
    'binary_sensor.homematicip_fenster_und_turkontakt_mit_magnet_battery_4',
    'binary_sensor.homematicip_fenster_und_turkontakt_mit_magnet_battery',
    'binary_sensor.homematicip_fenster_und_turkontakt_mit_magnet_battery_5',
    'binary_sensor.homematicip_fenster_und_turkontakt_mit_magnet_battery_6',
    'binary_sensor.homematicip_tur_battery',
    'binary_sensor.homematicip_bewegungsmelder_innen_battery',
    'binary_sensor.homematicip_wassersensor_battery',
    'binary_sensor.homematicip_wassersensor_battery_2'
    )
    | selectattr('state', 'eq', 'on')
    | map(attribute='name') | list %}
{% if x | length > 0 %}
***
# Batterie
{% else %}
  {% set x = expand(
    'sensor.aarlo_battery_level_eg',
    'sensor.aarlo_battery_level_og',
    'sensor.gang_battery',
    'sensor.netatmo_haus_buro_og_wohnzimmer_og_battery_percent',
    'sensor.netatmo_haus_buro_og_schlafzimmer_og_battery_percent', 
    'sensor.netatmo_haus_buro_og_bad_og_battery_percent', 
    'sensor.netatmo_haus_buro_anbau_oben_battery_percent',
    'sensor.netatmo_haus_buro_og_aussen_og_battery_percent',
    'sensor.netatmo_haus_buro_wohnzimmer_battery_percent',
    'sensor.netatmo_haus_buro_schlafzimmer_battery_percent',
    'sensor.netatmo_haus_buro_aussen_battery_percent',
    'sensor.netatmo_haus_buro_og_regensensor_battery_percent' 
    )
    | selectattr('state', 'lt', 10)
    | map(attribute='name') | list %}
    {% if x | length > 0 %}
***
# Batterie
    {% endif %}
{% endif %}

But with this Code I get this error:
TypeError: ‘<’ not supported between instances of ‘str’ and ‘int’

But I don’t know what are the mistake is.

I have mark the post from you with the solution als solution.

Try this version:

{% set x = expand(
    'binary_sensor.homematicip_alarmsirene_battery',
    'binary_sensor.homematicip_schlusselbundfernbedienung_alarm_battery',
    'binary_sensor.homematicip_heizkorperthermostat_battery',
    'binary_sensor.homematicip_fenster_battery',
    'binary_sensor.homematicip_fenster_battery_3',
    'binary_sensor.homematicip_heizkorperthermostat_battery_2',
    'binary_sensor.homematicip_fenster_und_turkontakt_mit_magnet_battery_2',
    'binary_sensor.homematicip_heizkorperthermostat_battery_3',
    'binary_sensor.homematicip_fenster_battery_2',
    'binary_sensor.homematicip_heizkorperthermostat_battery_5',
    'binary_sensor.homematicip_fenster_und_turkontakt_mit_magnet_battery_3',
    'binary_sensor.homematicip_heizkorperthermostat_battery_4',
    'binary_sensor.homematicip_fenster_und_turkontakt_mit_magnet_battery_4',
    'binary_sensor.homematicip_fenster_und_turkontakt_mit_magnet_battery',
    'binary_sensor.homematicip_fenster_und_turkontakt_mit_magnet_battery_5',
    'binary_sensor.homematicip_fenster_und_turkontakt_mit_magnet_battery_6',
    'binary_sensor.homematicip_tur_battery',
    'binary_sensor.homematicip_bewegungsmelder_innen_battery',
    'binary_sensor.homematicip_wassersensor_battery',
    'binary_sensor.homematicip_wassersensor_battery_2'
    )
    | selectattr('state', 'eq', 'on')
    | map(attribute='name') | list %}
{% if x | length > 0 %}
***
# Batterie
{% else %}
  {% set x = expand(
    'sensor.aarlo_battery_level_eg',
    'sensor.aarlo_battery_level_og',
    'sensor.gang_battery',
    'sensor.netatmo_haus_buro_og_wohnzimmer_og_battery_percent',
    'sensor.netatmo_haus_buro_og_schlafzimmer_og_battery_percent', 
    'sensor.netatmo_haus_buro_og_bad_og_battery_percent', 
    'sensor.netatmo_haus_buro_anbau_oben_battery_percent',
    'sensor.netatmo_haus_buro_og_aussen_og_battery_percent',
    'sensor.netatmo_haus_buro_wohnzimmer_battery_percent',
    'sensor.netatmo_haus_buro_schlafzimmer_battery_percent',
    'sensor.netatmo_haus_buro_aussen_battery_percent',
    'sensor.netatmo_haus_buro_og_regensensor_battery_percent' 
    )
    | map(attribute='state')
    | map('float')
    | select('lt', 10) | list %}
    {% if x | length > 0 %}
***
# Batterie
    {% endif %}
{% endif %}
1 Like

Thank you this works.

1 Like