brianlay
(Brian G Lay)
April 4, 2025, 1:12am
1
OH 4.2.3 running on RPi 4
I’m trying to add a timer to close my garage door after 5 minutes, only if a user did not already close it.
Could I build everything in Blockly?
My other rules start with a trigger outside Blockly and then run a Blockly script that includes a timer.
Like this
Is there some way to create the whole thing in Blockly?
Johnno
(John)
April 4, 2025, 1:34am
2
When you say “everything”, do you mean without a trigger? The script doesn’t continuously loop, so it will need a trigger.
Instead of triggering on specifically an OPEN state, you can trigger on any state.
In terms of the logic, you can check the item state within the script as part of an if/else statement.
If CLOSED to OPEN, then create a 5 min timer with a keyname, after which send a CLOSE command to the item.
If OPEN to CLOSED, cancel the timer with the unique keyname.
You might need an additonal check to ensure you don’t have an infinite trigger and script action loop.
Blockly has all these functions as premade blocks.
brianlay
(Brian G Lay)
April 4, 2025, 1:39am
3
I was thinking maybe the trigger could be in Blockly, but after re-reading the docs a rule always starts with a trigger.
So maybe a stupid question…
brianlay
(Brian G Lay)
April 4, 2025, 1:42am
4
So my solution is when the door becomes OPEN
Start a 5 minute timer
After 5 minutes, check door state and if it’s still OPEN, then send a command to close it.
Johnno
(John)
April 4, 2025, 1:48am
5
Sounds like that would work.
To double check your code, think about every possible action at each state of automation.
For example, what if someone closes and reopens the door in the 5 min period. Blockly has a function for resetting/starting the timer too.
ubeaut
(Greg)
April 4, 2025, 3:38am
7
This is what I use in javascript:
cachename=ctx.ruleUID
console.log("cachename : " + cachename)
//turn the light off after X minutes
var zmtimer=10
if (cache.private.exists('cachename') === false || cache.private.get('cachename').hasTerminated()) {
items.getItem("zigbee_switch_4_gang_switch_2").sendCommand("ON")
console.log('The script ran top level command ON');
cache.private.put('cachename', actions.ScriptExecution.createTimer('cachename', time.ZonedDateTime.now().plusMinutes(zmtimer), function () {
items.getItem("zigbee_switch_4_gang_switch_2").sendCommand("OFF")
console.log('The script ran 2nd level command OFF');
}));
} else {
cache.private.get('cachename').reschedule(time.ZonedDateTime.now().plusMinutes(zmtimer));
items.getItem("zigbee_switch_4_gang_switch_2").sendCommand("ON")
console.log('The script rescheduled the command ON');
};
Just change the item name to your name and change the time.
Its a bit rough but it works.
There is probably a neater way to do it.
Johnno
(John)
April 4, 2025, 9:11pm
8
Keep in mind if you find any strange behaviour, such as your 5 min timer expiring immediately, it may be a bug in OH or the operating system.
I’ve been dealing with intermittent issues in timers or cron based rules once the pi has been off overnight. It’s fixed by an immediate restart of the system, or saving / reinitialising the affected rules.
brianlay
(Brian G Lay)
April 5, 2025, 12:37am
9
Thanks it’s been working as expected so far
My Pi is on a UPS and never turned off or restarted.