Hello,
I am trying to find a real life example of actionnable notification to (for example) switch off light from an actionable notification if it’s turned on for too long.
So far I have this:
service: Notifications: Send a notification via mobile_app_iphone (I use the grahpical automation builder inside HA)
And in data, I have this:
actions:
- action: SERVICE
title: Switch off the light
data:
service: light.toggle
entity_id: light.somerandomid
But that won’t do anything.
Can someone help me please ?
This is the automation that actually does what ever you want to do. In my case in the actions part of this I send an RF code. In your case you would have to set it to turn off the lights.
Thanks for your answers, I thought it would be possible to have all actions embeded in the iOS notification (like in my example).
I will try it with your solutions.
He is also using iPhone, so should be pretty easy to modify to your needs
[I’ve been on this for hours, it became a challenge!]
I’d think it would be something like this:
In theory, this should: trigger (some light has been left on).
send a notification to a phone (fill your device id in the placeholder, remove the [ ])
wait for a response for 5 minutes.
If you click on the notification, should turn off some light (fill in the id)
If no interaction with the notification, it should leave the light on and end the automation run.
This code should go in the actions part of the automation.
action:
- device_id: [deviceID]
domain: mobile_app
type: notify
title: Light on
message: SomeRandomLight was left on!
data:
actions:
- action: TURN_OFF
title: Turn off
- wait_for_trigger:
- platform: event
event_type: mobile_app_notification_action
event_data:
action: TURN_OFF
timeout: '00:05:00'
continue_on_timeout: false
- service: light.turn_off
target:
entity_id: light.somerandomlight
mode: single
Wow, that took quite a while to figure out… By the way everything except for the button can be done from the UI. You can create every action and then edit the notification one in YAML.
As demonstrated above it is possible to have them all in a single automation. I had done this for a few but upon real life usage decided to keep a couple in two separate automations (on to send the notification and a second listening).
The main reason was how long to wait for a response in the single automation before deciding to call it a day - if you’re only going to wait for 5 minutes then a single automation is fine, but if I’m waiting for 5 hours then I felt it was better to split them, for example I’m always tinkering and might have restarted HA thus terminating the long waiting automation, if my wife then triggered a response it wouldn’t be picked up. (in @jcasarini examples he’s using a timeout but there may be cases where this isn’t applicable)
Just food for thought, any number of ways of doing things! The key thing is you should have all the information to make your decision now
That’s the reason I thought it was not possible to do it in a single automation, having one waiting for hours for a trigger didn’t seem too efficient and I didn’t know the “wait for trigger” action.
As you say, the timeout is not extrictly necessary, it’s just an resource management idea.
I rewrote my garage door automation to work in a single one, just as a proof of concept: as you, I tinker a lot too, so restarting HA is a common thing, and for other, the garage door might be left open for hours on purpose, I see no point in having a single automation running, consuming resources, waiting for something that might not happen.
And it works, for the moment I used the 2 automations solution, one for notification and the other one for light toggle.
At first I thought it didn’t worked because the process time is slower tha usual toggleing.
EDIT: Managed to get it working. Sharing the fixed automation YAML in case it is of use to anyone finding this thread. My goal was to create a notification that prompted me to arm the alarm if I forget. I didn’t want to automate arming it automatically as we aren’t all running HA on our phones.
description: ""
trigger:
- platform: state
entity_id:
- sensor.is_someone_home
from: "True"
to: "False"
for:
hours: 0
minutes: 10
seconds: 0
condition:
- condition: device
device_id: 1e5a3b30259fadf726c40428ad5fa336
domain: alarm_control_panel
entity_id: alarm_control_panel.aqara_hub_m2_b6a4_security_system
type: is_disarmed
action:
- alias: Set up variables for the actions
variables:
action_armaway: "{{ 'ARMAWAY_' ~ context.id }}"
- alias: No one is home
service: notify.mobile_app_rd_oneplus
data:
message: No one is home!
data:
actions:
- action: "{{ action_armaway }}"
title: Arm Away
- wait_for_trigger:
- platform: event
event_type: mobile_app_notification_action
event_data:
action: "{{ action_armaway }}"
- platform: event
event_type: mobile_app_notification_cleared
event_data:
action_1_key: "{{ action_armaway }}"
- alias: Perform the action
choose:
- conditions: "{{ wait.trigger.event.data.action == action_armaway }}"
sequence:
- service: alarm_control_panel.alarm_arm_away
data: {}
target:
entity_id: alarm_control_panel.aqara_hub_m2_b6a4_security_system
- conditions: "{{ wait.trigger.event.event_type == mobile_app_notification_cleared }}"
sequence:
- service: persistent_notification.create
data:
title: App notification result
message: The notification was closed
mode: single