Here’s my use case: A family member arrives home. Companion application welcomes the family member home with a notification containing a button to open the garage door. Once the button has been pressed the garage door opens and the family member receives a notification confirming this. Grateful for advice on the best way to achieve this. I want to do this within a single automation and avoid having a separate automation for each member of the family. I’m thinking a service template may be required but I’d welcome comments before I dig any deeper.
Probably something like this-
trigger:
- platform: state
entity_id: person.your_name
from: not_home
to: home
condition: []
action:
- variables:
action_open: >-
{{ 'OPEN_' ~ context.id }}
action_decline: >-
{{ 'DECLINE_' ~ context.id }}
phone_name: >-
{{ 'notify.mobile_app_' ~ trigger.entity_id.split('.')[1] }}
- service: >-
{{ phone_name }}
data:
message: >-
Welcome home {{ trigger.to_state.attributes.friendly_name.split(' ')[0] }}. Do you want to open garage door?
data:
actions:
- action: >-
{{ action_open }}
title: 'Yes'
- action: >-
{{ action_decline }}
title: 'No'
- wait_for_trigger:
- platform: event
event_type: mobile_app_notification_action
event_data:
action: >-
{{ action_open }}
- platform: event
event_type: mobile_app_notification_action
event_data:
action: >-
{{ action_decline }}
timeout: '60'
continue_on_timeout: false
- choose:
- conditions:
- condition: template
value_template: >-
{{ wait.trigger.event.data.action == action_open }}
sequence:
- service: switch.turn_on
target:
entity_id: switch.your_garage
default: []
mode: restart
This is based on the official documentation that you can found below-
Many thanks however I’m getting the following error message when pasting the code to perform the actions in the automation:
Message malformed: must contain at least one of service, service_template. @ data['action'][0]
Ok - i’ve copied the Yaml again, no further errors however upon changing the person state from ‘not_home’ to ‘home’ nothing happens.
Check the automation trace and see what’s stopping the automation to be triggered. Try checking the entity_id as well as the condition (try removing it).
Executed: 25 August 2021, 18:31:56
Error: Template rendered invalid service: notify.mobile_app_['person', 'cergon']
It’s getting stuck here:
service: '{{ phone_name }}'
data:
message: Welcome Home. Do you want to open garage door?
data:
actions:
- action: '{{ action_open }}'
title: 'Yes'
- action: '{{ action_decline }}'
title: 'No'
Sorry, yesterday I am on mobile phone so the "
or '
breaks. Can you try once again using the revised code above?
Next, go to Developer Tools → Services → In the service, type notify.mobile_app
→ Copy here the complete service name
After that, go to Developer Tools → States → Type person.cergon
→ Copy here the full state and attribute
Ardy - many thanks for your help on this - I’ve finally cracked it. Could not have done it without your input.
I have renamed the phone variable and use trigger.to_state.object_id
to reference my notification groups which mirror the object_id of the person entity. This means all devices linked to a particular family member receive the notifications which fits my use case.
For anyone interested, see below for the final Yaml. You’ll likely need to change the notification_service variable and the entity id associated with the garage door.
alias: Garage - Ask to open door when arriving home
description: ''
trigger:
- platform: state
entity_id: person.husband
from: not_home
to: home
- platform: state
entity_id: person.wife
from: not_home
to: home
condition:
- condition: state
entity_id: binary_sensor.garage_door_status
state: 'off'
action:
- variables:
action_open: '{{ ''OPEN_'' ~ context.id }}'
action_decline: '{{ ''DECLINE_'' ~ context.id }}'
notification_service: '{{ "notify." ~ trigger.to_state.object_id }}'
- service: '{{ notification_service }}'
data:
title: Welcome Home {{ trigger.to_state.attributes.friendly_name }}
message: Do you want to open garage door?
data:
actions:
- action: '{{ action_open }}'
title: 'Yes'
- action: '{{ action_decline }}'
title: 'No'
- wait_for_trigger:
- platform: event
event_type: mobile_app_notification_action
event_data:
action: '{{ action_open }}'
- platform: event
event_type: mobile_app_notification_action
event_data:
action: '{{ action_decline }}'
timeout: '120'
continue_on_timeout: false
- choose:
- conditions:
- condition: template
value_template: '{{ wait.trigger.event.data.action == action_open }}'
sequence:
- service: switch.turn_on
target:
entity_id: switch.garage_door
- service: '{{ notification_service }}'
data:
message: The garage door has been opened.
- conditions:
- condition: template
value_template: '{{ wait.trigger.event.data.action == action_decline }}'
sequence:
- service: '{{ notification_service }}'
data:
message: The garage door has not been opened.
default: []
mode: single