Toggle switch on/off n times in an hour?

I suddenly have a need to do something like this:

if motion turn on/off switch 10 times space over the course of the next hour. Say, on for 2 minutes, off for 8. Something like that. The actual intervals can be configurable.

If I were using time.sleep() this would be simple, but I’m not quite sure how to do it using appdaemon run_in scheduling. Any ideas?

Thx

You could store the state changes with timestamps in a sensor entity (as an attribute dict) and check that data during each callback and do something if the criteria you’re looking for matches.

I was thinking something like this:

   self.thing_on = self.run_every(self.turn_on(thing), "now" , 600)
   self.thing_off = self.run_every(self.turn_off(thing), "now+120", 600)

but I keep get an error and it doesn’t seem to work:

INFO AppDaemon: Adding thread 0
2022-02-13 12:45:52.423695 CRITICAL AppDaemon: Thread thread-0 has died
2022-02-13 12:45:52.424232 CRITICAL AppDaemon: Pinned apps were: [‘thing’]
2022-02-13 12:45:52.424640 CRITICAL AppDaemon: Thread will be restarted

It works like this cuz it’s a callback not a function. I don’t need to pass “thing” into the callback, I just get it from the args directly there.

    self.thing_on = self.run_every( self.its_on_c, "now", 10 * 60 )
    self.thing_off = self.run_every( self.its_off_c, "now+120", 10 * 60 )

Ah ok, yeah if you’re looking to run every hour that is fine. I thought maybe you wanted to check when the event happened if $x amount has happened within the last hour. As long as this works for you. :+1:t2:

I suppose this isn’t actually my whole solution. I’m using another virtual switch that gets turned on with motion in the room. This code only runs when that other virtual switch is on. It turns the device on for 2 minutes, then off for 8 minutes, as long as that other switch is on, aka the room is occupied. the 10 * 60 is straight from the docs. I don’t know why it’s put that way in the docs and not just 600 seconds.

I also still need to parameterize that interval and the now+interval.