Lets continue this series on the Rule system. Today we are going to create some more advanced rules, and explain how it works under the hood.
Control multiple IO in sync
In this example we have two light in a room. And we want that when we press the switch both light goes ON simultaneously.
A basic approach of doing the rule would be:
That will work when both lights are synced, but the problem that need to be solved is that when lights are controlled directly (from calaos_home or calaos_mobile), and then pressing the switch, they could be out of sync. (first is ON and the second is OFF). The only way to put both lights in sync again is to do it manually. No so smart!
As you saw in the action popup, you can choose an action for the light from a predefined static list. Possible actions are for ex: toggle, true, false, … But there is a button right under the action, Dynamic value. This is where you can choose a value from an other IO. In concrete terms, after clicking the button you will be able to choose an IO, and when the action is executed, the value will be extracted from the selected IO.
Lets do that for the Light 2. So now in our example the first action is to toggle the state of the first light, and then assign that new state to the second light.
Now with such a rule, if the lights are not in sync, the second light will automatically be set at the same state. Of course this example can be repeated with other type of IO, like shutters. It’s extremely powerful when controlling multiple shutters with just one button.
There is another way to sync both lights, by using a second rule. This time we will use a rule to toggle the state of the first light:
And a second rule with a condition that checks for the Light 1 state change. If the state changes, we update the state of the second light from the first light.
The fundamental difference between the two solutions exposed here is that the sync is done everytime the state of Light 1 changes, even when you control the light from the user interfaces. In the first solution that’s not the case, the second light is only synced when you press the switch.
The second approach is good when you need to link multiple outputs. You only need to do an action on the first IO, and the second will be updated automatically.