How to share sensor status in message to app

Hi,
I am trying to send a message to my smartphone with the information which sensor is open when I leave home, but the code does not work. Can someone help me fix the code?

action:
  - if:
      - condition: or
        conditions:
          - condition: state
            entity_id: binary_sensor.lumi_lumi_sensor_magnet_opening_3
            state: "on"
          - condition: state
            entity_id: binary_sensor.lumi_lumi_sensor_magnet_opening_5
            state: "on"
          - condition: state
            entity_id: binary_sensor.lumi_lumi_sensor_magnet_opening
            state: "on"
          - condition: state
            entity_id: binary_sensor.lumi_lumi_sensor_magnet_opening_2
            state: "on"
          - condition: state
            entity_id: binary_sensor.kellertur_opening
            state: "on"
          - condition: state
            entity_id: binary_sensor.lumi_lumi_sensor_magnet_opening_4
            state: "on"
    then:
      - device_id: a2dc67a453c0cc3606fc49806c3a3eed
        domain: mobile_app
        type: notify
        message: "Folgende Sensoren sind geöffnet: {% if is_state('binary_sensor.lumi_lumi_sensor_magnet_opening_3', 'on') %} Sensor 1, {% endif %}{% if is_state('binary_sensor.lumi_lumi_sensor_magnet_opening_5', 'on') %} Sensor 2, {% endif %}{% if is_state('binary_sensor.lumi_lumi_sensor_magnet_opening', 'on') %} Sensor 3, {% endif %}{% if is_state('binary_sensor.lumi_lumi_sensor_magnet_opening_2', 'on') %} Sensor 4, {% endif %}{% if is_state('binary_sensor.kellertur_opening', 'on') %} Sensor 5, {% endif %}{% if is_state('binary_sensor.lumi_lumi_sensor_magnet_opening_4', 'on') %} Sensor 6{% endif %}"
      - service: notify.alexa_media_kups_wohnzimmer
        data:
          message: "Folgende Sensoren sind geöffnet: {% if is_state('binary_sensor.lumi_lumi_sensor_magnet_opening_3', 'on') %} Sensor 1, {% endif %}{% if is_state('binary_sensor.lumi_lumi_sensor_magnet_opening_5', 'on') %} Sensor 2, {% endif %}{% if is_state('binary_sensor.lumi_lumi_sensor_magnet_opening', 'on') %} Sensor 3, {% endif %}{% if is_state('binary_sensor.lumi_lumi_sensor_magnet_opening_2', 'on') %} Sensor 4, {% endif %}{% if is_state('binary_sensor.kellertur_opening', 'on') %} Sensor 5, {% endif %}{% if is_state('binary_sensor.lumi_lumi_sensor_magnet_opening_4', 'on') %} Sensor 6{% endif %}"

I would make use of variables.


automation:

- id: '202210092041'
  alias: Fenster/Türen vergessen

  variables:
    ## checks if any door or window is still open
    ft: |-
      {{ states.binary_sensor
        |select('search', 'opening|door|window')
        |selectattr('state', 'eq', 'on')
        |map(attribute='name')
        |list }}

    nachricht: |-
      {%- if ft |count > 0 %} 
      {{' und '.join((ft|join(', ')).rsplit(', ', 1)) }}{{ ' sind' if ft |count > 1 else ' ist' }} noch offen!
      {%- else %} 
      {% endif %}

  trigger:
  - …

  condition:
    - '{{ ft |count > 0 }}'

    ## alternatively create a group
    - condition: state
      entity_id: group.fenster_tueren
      state: 'on'

  action:
  - service: notify.mobile_app_usw…
    data:
      title: 'SICHERHEITSWARNUNG [{{ now().strftime('%T') }}]'
      message: '{{ nachricht }}'

  - service: notify.alexa_media_kups_wohnzimmer
    data:
      message: '{{ nachricht }}'


Another option:

trigger: []
condition: []
action:
  - if:
      - condition: state
        entity_id: 
          - binary_sensor.lumi_lumi_sensor_magnet_opening_3
          - binary_sensor.lumi_lumi_sensor_magnet_opening_5
          - binary_sensor.lumi_lumi_sensor_magnet_opening
          - binary_sensor.lumi_lumi_sensor_magnet_opening_2
          - binary_sensor.kellertur_opening
          - binary_sensor.lumi_lumi_sensor_magnet_opening_4
        state: "on"
        match: any
    then:
      - variables:
          message: |
            {% set mapper = { 
            "Sensor 1": is_state('binary_sensor.lumi_lumi_sensor_magnet_opening_3', 'on'),
            "Sensor 2": is_state('binary_sensor.lumi_lumi_sensor_magnet_opening_5', 'on'),
            "Sensor 3": is_state('binary_sensor.lumi_lumi_sensor_magnet_opening', 'on'),
            "Sensor 4": is_state('binary_sensor.lumi_lumi_sensor_magnet_opening_2', 'on'),
            "Sensor 5": is_state('binary_sensor.kellertur_opening', 'on'),
            "Sensor 6": is_state('binary_sensor.lumi_lumi_sensor_magnet_opening_4', 'on')
            } %}
            {% set x = mapper.items() %}
            Folgende Sensoren sind geöffnet: {{ x|reject('search', 'False')|map(attribute=0)|join(', ') }}
      - device_id: a2dc67a453c0cc3606fc49806c3a3eed
        domain: mobile_app
        type: notify
        message: "{{ message }}"
      - service: notify.alexa_media_kups_wohnzimmer
        data:
          message: "{{ message }}"

Thank you for your help, but I am still not able to get it running. Let me share the whole code:

alias: New Automation
description: ""
trigger:
  - platform: state
    entity_id:
      - person.xxx
    to: not_home
action:
  - if:
      - condition: state
        entity_id:
          - binary_sensor.lumi_lumi_sensor_magnet_opening_3
          - binary_sensor.lumi_lumi_sensor_magnet_opening_5
          - binary_sensor.lumi_lumi_sensor_magnet_opening
          - binary_sensor.lumi_lumi_sensor_magnet_opening_2
          - binary_sensor.kellertur_opening
          - binary_sensor.lumi_lumi_sensor_magnet_opening_4
        state: "on"
        match: any
    then:
      - variables:
          message: |
            {% set mapper = { 
              "Sensor 1": is_state('binary_sensor.lumi_lumi_sensor_magnet_opening_3', 'on'),
              "Sensor 2": is_state('binary_sensor.lumi_lumi_sensor_magnet_opening_5', 'on'),
              "Sensor 3": is_state('binary_sensor.lumi_lumi_sensor_magnet_opening', 'on'),
              "Sensor 4": is_state('binary_sensor.lumi_lumi_sensor_magnet_opening_2', 'on'),
              "Sensor 5": is_state('binary_sensor.kellertur_opening', 'on'),
              "Sensor 6": is_state('binary_sensor.lumi_lumi_sensor_magnet_opening_4', 'on')
            } %}
      - device_id: a2dc67a453c0cc3606fc49806c3a3eed
        domain: mobile_app
        type: notify
        message: "{{ message }}"

I am not receiving any message on the mobile app when running the script, however, this part is correct since I am using it for other notifications already:

      - device_id: a2dc67a453c0cc3606fc49806c3a3eed
        domain: mobile_app
        type: notify
        message: Irgendwas ist offen

Which is why I am assuming that something is wrong in the variables section.
@ pedolsky: I tried the your idea with the variables unsuccessfully, too. I never worked with groups so far, therefore I havent tried that.

The group option is meant as an alternative and example; the automation doesn’t work if no such group exists (condition will fail). Use the example without it


automation:

- id: '202210092041'
  alias: Fenster/Türen vergessen

  variables:
    ## checks if any door or window is still open
    ft: |-
      {{ states.binary_sensor
        |select('search', 'opening|door|window')
        |selectattr('state', 'eq', 'on')
        |map(attribute='name')
        |list }}

    nachricht: |-
      {%- if ft |count > 0 %} 
      {{' und '.join((ft|join(', ')).rsplit(', ', 1)) }}{{ ' sind' if ft |count > 1 else ' ist' }} noch offen!
      {%- else %} 
      {% endif %}

  trigger:
  - …

  condition:
    - '{{ ft |count > 0 }}'

  action:
  - service: notify.mobile_app_usw…
    data:
      title: 'SICHERHEITSWARNUNG [{{ now().strftime('%T') }}]'
      message: '{{ nachricht }}'

  - service: notify.alexa_media_kups_wohnzimmer
    data:
      message: '{{ nachricht }}'

You used Drew’s example not completely, this part is missing:


            {% set x = mapper.items() %}
            Folgende Sensoren sind geöffnet: {{ x|reject('search', 'False')|map(attribute=0)|join(', ') }}

Yes, you left out the two lines that define and “print” the value…

Yes, you are right both. With these two lines I get: Message malformed: expected a dictionary for dictionary value @ data[‘action’][0][‘then’][0][‘variables’]