Hello,
I want to set a timer for a group of MQTT lights in Openhab.
If I turn the Fence lights ON after 5 hours it will turn off automatically.
Can anyone help me do that?
Thank you!
Sure. Create a Number Item and put in your UI with a setpoint or similar. Think about how you want to initialize that at system startup. Use its state in your rule - tip, you need the integer value for the plusHours()
import org.joda.time.DateTime
var Timer myTimer = null
rule "my auto off rule"
when
Item Fence changed
then
if (Fence.state==OFF) {
myTimer.cancel
myTimer = null
}
else if (Fence.state==ON) {
if (myTimer!=null) {
myTimer.cancel
myTimer = null
}
myTimer = createTimer(now.plusSeconds(myHours.state as DecimalType)) [|
Fence.sendCommand(OFF)
]
}
end
Come on, give us something to work with. What happens that you don’t expect, what doesn’t happen that you do?
When developing rules, always check openhab.log. If there’s nothing there, say so. If wondering if something happened or not, check events.log, tell us what you see.
Okay, so I gave a very old example because it was simple
remove this line from your rules, this was needed in OH1 but not wanted in OH2
I use Openhab2
13:56:19.269 [ERROR] [untime.internal.engine.RuleEngineImpl] - Rule ‘my auto off
rule’: cannot invoke method public void java.util.Timer.cancel() on null
It’s right you know; you can’t cancel a timer that doesn’t exist.
The rule tries to cancel the timer everytime the Item changes, even if no timer is null.
Tip - this sneaky small change will cancel the timer if it exists, but not complain if it doesn’t.
i remove the line import org.joda.time.DateTime
my rules:
var Timer myTimer = null
rule "my auto off rule"
when
Item Fence changed
then
if (Fence.state==OFF) {
myTimer?.cancel
myTimer = null
}
else if (Fence.state==ON) {
if (myTimer!=null) {
myTimer.cancel
myTimer = null
}
myTimer = createTimer(now.plusSeconds(myHours.state as DecimalType)) [|
Fence.sendCommand(OFF)
]
}
end
When i press the ON button i receive this:
14:28:39.294 [ERROR] [untime.internal.engine.RuleEngineImpl] - Rule ‘my auto off
rule’: An error occurred during the script execution: Could not invoke method:
org.joda.time.DateTime.plusSeconds(int) on instance: 2019-08-07T14:28:39.292+03:
00
When i press the OFF button i receive this:
14:28:41.974 [ERROR] [untime.internal.engine.RuleEngineImpl] - Rule ‘my auto off
rule’: cannot invoke method public void java.util.Timer.cancel() on null
var Timer myTimer = null
rule "my auto off rule"
when
Item Fence changed
then
if (Fence.state==OFF) {
myTimer?.cancel
myTimer = null
}
else if (Fence.state==ON) {
if (myTimer!=null) {
myTimer.cancel
myTimer = null
}
myTimer = createTimer(now.plusMinutes((myHours.state as DecimalType).intValue)) [|
Fence.sendCommand(OFF)
]
}
end
Does anyone help me with this suggestion?
“For convenience, myHours should be persisted for example with mapdb (strategy = everyChange, restoreOnStartup) so if openHAB is restarted, myHours will be set to the last value.”
How do myHours keep their value even if openhab is reset?
Can you explain further to my meaning?
Thank you
Magic word is persistence, which stores values in a database. The function you are looking at is restoreOnStartup, which uses the database to repopulate selected Items at system boot. Mapdb is the simple one-value database for just this purpose.