[SOLVED] Rules on/off with a switch?


Can I activate / deactivate a rule with a switch?

Thanks for hints

You can use the state of the switch, to determine if the rule should run the rest of the code, or if it should just skip it.

Could be like

if(rule_toggle.state == ON)
	rest of the rule here

thanks, it works.

rule “Lights corridor ON 8:00 SAT”
Time cron “0 0 8 ? * SAT *”
if(light_corridor.state == ON)
logInfo(“info”,“lights corridor ON”)

1 Like

Sorry but,
Is the " light_corridor " ( if(light_corridor.state == ON) ) an
item and sitemap switch or ?
if yes if openhab / pi restarts what happens then ?

It must be an item
What happens when oh2restarts depends if there any other rules that do something on startup or whether persistence is defined with restore on startup. If the latter the item state will simply take again the last state it had before it was oh2 was shutting down. The former depends obviously on the rule that runs on startup
If none of the above the item state will be UNDEF until it is updated.

You can deal with UNDEF in rules like that, by carefully considering if-conditions.
For example, to run some code if switch is ON or NULL (not yet initialised) or UNDEF (maybe a device communication failure)

if (mySwitch.state != OFF { ...