How to create subroutines
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.
I discovered another useful benefit of this approach today: you can use scenes to trigger rules.
I discovered another useful benefit of this approach today: you can use scenes to trigger rules.
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!
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!
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.
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.
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?
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?
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.
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.
Replies have been locked on this page!