Trying to have a message sent to my phone and announcement via Alexa in the same automation. I get the push notification, but it says closed for both opening and closing the gate. Not sure what I am missing as I have something similar working fine for tracking devices in and out of zones. Any suggestions?
alias: Gate Notification
description: ''
trigger:
- type: opened
platform: device
device_id: xxxxxx
entity_id: binary_sensor.back_yard_gate
domain: binary_sensor
- type: not_opened
platform: device
device_id: xxxxx
entity_id: binary_sensor.back_yard_gate
domain: binary_sensor
condition: []
action:
- device_id: xxxxx
domain: mobile_app
type: notify
title: ''
message: >-
{% if trigger.event == "opened" %} The gate has been opened. {% else %}
The back yard gate is now closed. {% endif %}
- service: notify.alexa_media
data:
message: >-
{% if trigger.event == "opened" %} 'The gate has been opened.' {% else
%} 'The back yard gate is now closed.' {% endif %}
data:
type: tts
target: media_player.echo_living_room
mode: queued
max: 10
You’re using device triggers. I don’t think they even create a trigger object, but if they do they certainly won’t have a .event method as there’s no event key in the trigger.
Yes - but that’s not the event you’re triggering from, so it’s not part of the trigger object.
Put simply, device triggers/conditions/actions are for simple automations created with the ui only. If you want to do anything even slightly powerful in homeassistant (like use templates) you need to avoid them and use ‘proper’ triggers, conditions and actions.
You’ll also find that the code becomes much neater and easier to read.
alias: Gate Notification
trigger:
platform: state
entity_id: binary_sensor.back_yard_gate
to:
- 'on'
- 'off'
action:
- service: notify.mobile_app_XXXXX
message: "The gate is {{ 'opened' if trigger.to_state.state == 'on' else 'closed' }}."
- service: notify.alexa_media
data:
message: "The gate is {{ 'opened' if trigger.to_state.state == 'on' else 'closed' }}."
target: media_player.echo_living_room
data:
type: tts
It’s the ‘only’ difference, but it’s a very big difference. Zone triggers create a trigger object, which you need for the template in your action.
You can do. If so, paste it like the others, so it will have a - before the word alias and then everything below needs it’s indentation preserved so it looks identical to the above, just another 2 spaces to the right each so it lines up with your other automations.