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 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:
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 are right both. With these two lines I get: Message malformed: expected a dictionary for dictionary value @ data[‘action’][0][‘then’][0][‘variables’]