[Share] Dynamically Schedule actions in Homeassistant using app

I managed to get an app working which could execute actions on Homeassistant dynamically based on either a delay or a give date/time.

This app requires the latest REST-API support to be enabled on Appdaemon to work:

DelayScheduler:


import appdaemon.appapi as appapi
import datetime

class Scheduler(appapi.AppDaemon):
   def initialize(self):
       self.log("Hello from AppDaemon Scheduler")

   def api_call(self,data):
       self.log("{}".format(data))
       def execute(kwargs):
           self.log("Executing Dynamic Callback")
           serv=kwargs.pop('service',None)
           self.call_service(serv,**kwargs)
       delay=data.pop("delay",0)
       self.run_in(execute,delay,**data)
       return {"message":"Success"},200

Payload:

{"service":"homeassistant/toggle","entity_id":"switch.fan","delay":2}

Time Based scheduler:

import appdaemon.appapi as appapi
import datetime

class TimeScheduler(appapi.AppDaemon):
   def initialize(self):
       self.log("Hello from AppDaemon Time Scheduler")

   def api_call(self,data):
       self.log("{}".format(data))
       def execute(kwargs):
           self.log("Executing Dynamic Callback")
           serv=kwargs.pop('service',None)
           self.log("{}".format(serv))
           self.call_service(serv,**kwargs)
       time=self.parse_time(data.pop('time'))
       if isinstance(time,datetime.time):
          runtime=datetime.datetime.combine(datetime.date.today(),time)
       self.run_at(execute,runtime,**data)
       return {"message":"Success"},200

Payload:

{"service":"homeassistant/toggle","entity_id":"switch.fan","time":"18:45:00"}

This will enable you to execute any domain service using voice commands using alexa/google home:
Imagine saying things like : turn off tv in 5 minutes or open garage in 2 minutes

6 Likes

i like it.
but do you have an idea how to get alexa put out that kind off payload and get it to AD?

Well, you could use something like API.AI to do the natural language translation to entities which HA/AD understands.

Ok it would be simpler to use IFTTT alexa integration to push the event to the AD

it would be nice if it was possible to use things like:

alexa turn off tv in 5 minutes

but in fact in the moment it is only possible to ask:

alexa ask AD to turn of the tv in 5 minutes

and that needs to build a skill and a specific api reaction to that, which this doesnt provide.
still you can built some commandline functions to activate this.
or webpages that activate those commands.

How do you do to make HA turn off tv in 5 minutes through alexa?

by creating an intent