Different states for entity list in message template

I want to create alert with problematic entities list.
Everything works fine when needed entities status are same, but I don’t know how to make it work with different states.

Here is what I have, but I need to include items, i.e. binary_sensor.garage_nvr_disk_error, which have state ON when problem.
Thank in advance.

        message: >-
          {%- if is_state("binary_sensor.video_problem", "off") %}
            Video ok.
          {% else %}
            Video problem: 
            {%- set entities = [states.binary_sensor.garage_nvr_connection, states.binary_sensor.home_nvr_connection, states.binary_sensor.garage_cam_outdoor_connection, states.binary_sensor.garage_cam_indoor_connection, states.binary_sensor.home_cam_entrance_connection, states.binary_sensor.home_cam_outdoor_connection] -%}
            {%- for entity in entities -%}
              {%- if entity.state == 'off' %}
            - {{ entity.name }}
              {%- endif %}
            {%- endfor -%}
          {%- endif %}

Are we talking about the alert integration or an automation?

Copy the following into Developer Tools —> Template and report if it is working for you (long variables names and paragraphs only for clarification):


message: |-
  {% set problem_when_on = ['binary_sensor.1', 'binary_sensor.2']
  |select('is_state', 'on') |list %}
  
  {% set problem_when_off = ['binary_sensor.a', 'binary_sensor.b'] 
  |select('is_state', 'off') |list %}
  
  {% set all_problems = problem_when_on + problem_when_off %}
  
  {{ 'Video ok.' if all_problems |count == 0 else 'Video problem: ' ~ all_problems }}

2 Likes

Nice, its works, but instead of user-friendly entity name, it provide system name of entity.

I use that template for Alert.

Ah, overlooked that.

The following computes a formatted name list:


  message: |-
    {% set problem_when_on = ['binary_sensor.tageslicht', 'binary_sensor.loggiatur']
    |select('is_state', 'on') |list %}
  
    {% set problem_when_off = ['binary_sensor.a', 'binary_sensor.b'] 
    |select('is_state', 'off') |list %}
  
    {% set all_problems = expand(problem_when_on + problem_when_off) |map(attribute='name') |list %}
  
    {{ 'Video ok.' if all_problems |count == 0 else 'Video problem: ' }}
    - {{ all_problems |join('\n- ') }}

Flawless. Thank you!