This object is in archive! 

How to create subroutines

David Pritchard shared this idea 8 years ago
Under Consideration

I know that some of you know this, but for the benefit of those that don't...


I've often thought it would be handy to package a series of actions into a subroutine which can be invoked from different rules as needed. After some experimentation, I've found that this can work reliably using a virtual switch as the trigger.


1. Add a virtual switch, e.g. Sub_Trigger_Switch.

2. Create a subroutine rule that is triggered when this switch is set to "on".

3. Inside the subroutine, turn the trigger switch back off (otherwise the rule will never be invoked again). Because of the bug in the Zipabox that prevents us from making several changes to a switch state in rapid succession, you need to add a wait of 2 seconds before turning the switch off (bearing in mind that the switch will have just been turned on to trigger the subroutine). To be absolutely sure that the switch is turned off, I usually add another instruction at the end that turns it off, optionally with another wait of 2 seconds. The alternative to this is to turn it off in the calling routine before turning it on.

4. Set the switch to "on" in any rule where you want to invoke the subroutine.


In my example, the invoking rule (rule 10), which is itself a subroutine, invokes many subroutines, including rule 50, which simply sets up the "weekday" switch.


Another advantage of this is that it makes rules very easier to trigger for testing purposes. Just flip the trigger switch!


It's worth noting that, if you run several subroutines in succession, the Zipabox won't wait for the first to finish before triggering the second. It's just flipping switch states. So you might to chain together subroutines that depend on each other, or insert wait times, whatever works best.

Replies (5)

photo
1

I discovered another useful benefit of this approach today: you can use scenes to trigger rules.

photo
1

This is a great way to save duplicating the same code in multiple rules. I read another post where you created a subroutine to play announcements on several Sonos players you have scattered around your house. I'm planning a similar setup for my house. Thanks for sharing!

photo
1

That is something I never though of sharing, I guess it would be useful for many. I use the same approach for subroutines I like to trigger automatically and manually, also I have combined scenes that trigger a few switches that trigger rules (subroutines).


Another approach I use for Sonos and Local voice on Zipatile is using virtual meters and different values, for example 1 to 100, that way with one meter you can trigger many different rules or subroutines. This last method can even be used to interlock Zipatile and Zipabox without a cluster, you just setup a meter on both and change their values using HTTP POST requests.


From my Zipatile I sent touch events to zipabox and from zipabox I send local voice and local push messages to Zipatile.

photo
1

Now all my subroutines are triggered by toggling the virtual switch, rather than setting it to "on", which is kind of obvious and much more reliable.


Sometimes apparently obvious things are worth sharing because they may not have occurred to someone else.


Almost all my rules are subroutines.


Alberto, could you elaborate on what you do with the virtual meters, or post an example?

photo
1

Sure David,


This is the principle, I have one virtual meter on my Zipatile called "Command_Intlks" which is basically written by the Zipatile Bezel Buttons and touch events, I asign a number to each action and send it using a POST HTTP request, for example; by swiping down with two fingers I want all the lights on my first floor to turn OFF, so from the zipatile I have a subroutine called "ToZipabox2FingerInlks" find attached rule which basically sends a different numeric number per every "Two finger" touch event, another subroutine for "Three finger" and so on. On the Zipabox side I have also one subroutine each (attached) called "CasaM&A_2FingerInlks" and "CasaM&A_3FingerInlks" and these check wether the meter is within certain values to trigger the rule and then based on the number to do a specific action and then go back to zero.


Also find attached my chart for assigning numeric event to my zipatile touch events. I do the same from my zipabox to my zipatile for local voice and local push notifications of several events at home, for example, a welcome message from the zipatile when you succesfully unlocked the door (and disarmed the system with the same event). If you have further questions please let me know, I will be happy to help.

photo
1

Very nice. At some point I really may consider the Zipatile, it looks very convenient.

Do you have a light sensor for the levels of natural light, or do you just work it out from whether it's day/night?

photo
photo
1

Thanks for the tip David. I had considered resetting my virtual switches to "off" each day at 2am to clear any glitches, but your suggestion to trigger when a switch toggles is much cleaner.

photo
1

Yep. I used to do the same thing, but unfortunately the switches were not always reliably set to "off" from within the rules themselves. Until I thought: why don't I just toggle them?!

photo
Leave a Comment
 
Attach a file