I’ve been looking for hours how to do this… I have various binary sensors for windows & doors. I’ve defined an all_doors and all_windows groups. I am trying to create an automation which will announce (with tts.google_translate_say) which entity triggered it. So if the front door is opened, it will say, “Front door open”. I don’t know how to get the “friendly name” into the yaml and end the phrase with “opened”. From what I’ve cobbled together from others, this is what I have:
alias: Announce when a window or door is opened
description: ''
trigger:
- platform: state
entity_id: group.all_doors
to: open
condition: []
action:
- service: tts.google_translate_say
data_template:
message: |-
{% for entity in trigger.to_state.attributes.entity_id %}
{% if states(entity) == 'open' %}
{{ state_attr(entity, 'friendly_name') }} has been opened for 2 minutes
{% endif %}
{% endfor %}
entity_id: media_player.google_max
mode: queued
max: 10
I get this error: Error: Error rendering data template: UndefinedError: ‘dict object’ has no attribute ‘to_state’. Thanks!!
While i’m not directly answering your question i thought i would share my example of something similar i created recently. I wanted to get pushover notifications when one of my external door or windows were opened while the alarm is armed. It’s using a similar concept i think. Maybe it will help you.
- id: '1618266282308'
alias: 'security: alarm door or window open and notify'
description: If alarm is armed & if motion notifications not paused, when an external
door or window opens send pushover notification.
trigger:
- platform: state
entity_id:
- binary_sensor.doorwindow01
- binary_sensor.doorwindow02
- binary_sensor.doorwindow03
- binary_sensor.doorwindow04
- binary_sensor.doorwindow05
- binary_sensor.doorwindow09
- binary_sensor.doorwindow10
- binary_sensor.doorwindow11
- binary_sensor.doorwindow12
- binary_sensor.doorwindow13
- binary_sensor.doorwindow15
- binary_sensor.doorwindow16
- binary_sensor.doorwindow17
- binary_sensor.doorwindow18
- binary_sensor.doorwindow19
- binary_sensor.doorwindow20
- binary_sensor.sonoff_snzb04_01
- binary_sensor.ikea_motion_01
from: 'off'
to: 'on'
- platform: state
entity_id: cover.sonoffsv_02
from: closed
to: open
condition:
- condition: state
entity_id: alarm_control_panel.home_alarm
state: armed_away
- condition: state
entity_id: input_boolean.motion_notification_paused
state: 'off'
action:
- service: script.notify_pushover_without_attachment
data:
title: 'Security: External door or window opened.'
message: 'The door or window [ {{ trigger.to_state.name }} ] was opened while
the alarm was armed. Check if someone is home.'
- service: alarm_control_panel.alarm_trigger
target:
entity_id: alarm_control_panel.home_alarm
mode: queued
max: 10
Thanks! I used some of your code and got it working. Looks like it’s not possible to use a group how I was trying. I added each sensor individually as you did and it worked.
I was able to make your idea with the group work fine. You were very close but you just needed expand(), I think. Here’s what I did. I did it in the GUI. Edited the last step in YAML.
alias: Doors Unlocked Notification
description: ""
trigger:
- platform: state
entity_id:
- lock.all_locks
to: unlocked
for:
hours: 0
minutes: 10
seconds: 0
condition: []
action:
- repeat:
while:
- condition: state
entity_id: lock.all_locks
state: unlocked
sequence:
- service: notify.alexa_media_everywhere
data:
message: >-
{%- for lock in expand(trigger.to_state.entity_id) -%}
{%- if lock.state != "locked"
and (now().timestamp() - as_timestamp(lock.last_changed)) >= trigger.for.total_seconds() -%}
The {{ lock.name }} has been unlocked for {{ ((now().timestamp() -
as_timestamp(lock.last_changed)) / 60) | int }} minutes.
{% endif %}
{% endfor %}
data:
type: announce
method: all
- delay: "{{ trigger.for.total_seconds() }}"
mode: single
max_exceeded: silent
Alexa announces “The Back Door Lock has been unlocked for 5 minutes.” just tested it.