Actionnable notification with entity control

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 ?

Thanks :slight_smile:

An example is provided in the documentation.

Hi!

I have one to close the garage door if it’s been opened for more than 5 minutes.

It actually works with two automations:

The first one triggers after the garage door’s been opened for five minutes, that sends a actionable notification to my phone.

This is the YAML for the actions on this automation:

device_id: redactedID
domain: mobile_app
type: notify
title: Portón Garage
message: El portón quedó abierto!
data:
  actions:
    - action: CERRAR_PORTON
      title: Cerrar portón

Then, a second automation is triggered by the “CERRAR_PORTON” event if I click it on my phone.

YAML for the trigger of this automation:

platform: event
event_type: mobile_app_notification_action
event_data:
  action: CERRAR_PORTON

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.

Hope this helps.

Joaquín

1 Like

Hello,

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.

Actually you can, but the programming is bit more involved…

As @zacwest said, there’s an example in the documentation.

That being said, here is a post with actual code from a user: Wait for Trigger & Choose with Value Template… - #6 by woodmj74

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.

1 Like

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 :slight_smile:

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.

1 Like

Thanks a lot for your answers and feedbacks.
I will test possible solutions soon.

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