Using actionable notification event as condition in automation

I use AppDaemon for any automation, but with all the nice enhancements in the last few years, I was a bit curious, and wanted to see how far the automations in Hass have come since last I used them.

But I think I need a bit of help to get started.

I’m trying to make an automation that:

  1. Triggers on sensor state change
  2. Sends an actionable notification
  3. Waits 5 minutes
  4. Calls a service, but only if the actionable notification was NOT replied with one specific event/reply.

And it’s this last part I don’t really know how to do.

In AppDaemon I’d set up a listen_state for the state change, and a listen_event for a custom event, that I’d add in the actionable notification, set up a run_in linked to a timer to run the service call, but have the event (returned by the actionable notification) cancel that timer.

**How I would do it in AppDaemon, if that makes it more clear**
import hassapi as hass

class Alarm(hass.Hass, globals.Entities):

    def initialize(self):

        self.listen_state(self.alarm, "binary_sensor.occupancy", state="off")
        self.listen_event(self.disable_alarm, event='mobile_app_notification_action', action='disable_alarm')
        self.timer = None

    def alarm(self, entity, attribute, old, new, kwargs):

        self.notify(new_state)
        self.timer = self.run_in(self.set_alarm, 600)

    def set_alarm(self):

        self.call_service("alarm_control_panel/alarm_arm_away")

    def notify(self, new_state):

        title = "Alarm Arming in 5 Minutes"
        message = "The house is empty, I will arm the alarm in 5 minutes"
        data = {
            "actions": [
                {
                    "action": "ignore",
                    "title": "OK"
                },
                {
                    "action": "disable_alarm",
                    "title": "Don't Arm",
                }
            ]
        }

        self.call_service("notify/mobile_app_some_phone", title=title, message=message, data=data)

    def disable_alarm(self, event_name, data, kwargs):

        try:
            self.cancel_timer(self.timer)
        except:
            self.call_service("alarm_control_panel/alarm_disarm")

Is there a way to do this with the Automations panel in the UI? It doesn’t seem like you can switch to yaml mode for automation as there is for e.g. Developer ToolsServices.

You should be able to switch to YAML mode on any automation by clicking the 3 dots at the top right of the automation. Also, just to be clear, are you wanting your action to be setup like this:

  • When initial state is reached you are actually calling both a notification action and a timer-based action.
  • The timer-based action will happen unless the notification action is used to stop the pending timer-based action.

Does that sound right? You are using the Notification Action as a way to stop an in-progress action instead of juat starting a new action?

If I’m following you correctly, then I think you might want to use a binary helper(toggle) as a way to track a state. You can use the binary helper(toggle) in your actionable notification to have the handling of the action be to set the state to off. Then you can use the if-then Action after your wait time, then check the state of the binary helper in that if-then Action and if it’s off don’t perform the action you are waiting for otherwise if the binary helper is on go ahead and call the service.

So, if I’m not mistaken you should be able to handle this with 2 automations.

1 Like

Oh, I forget to mention that in the if-then when the binary helper is in the off state your then Action will be to turn that binary helper back to the on state so that it is ready for the next time your main automation it triggered.

1 Like

That makes sense, I guess I was thinking pretty much “inside the box”, not even considering making multiple automation and an additional (helper) entity.

Thanks, I’ll give it a go.

1 Like