i am running openhab2 on a orange pi with armbian.
i have a rule with timer that works half way and also gives errors in openhab log and i dont have enough knowledge to figure it out.
its hacked together by what i found on different forums around. its not something i can create myself, i am a begginer.
this is the rule:
import org.openhab.model.script.actions.Timer
rule “Kuhinja”
when
Item mqtt_topic_kuhinja_occupancy received update
then
var Timer kuhinja_timer
var String kuhinja = mqtt_topic_kuhinja_occupancy.state
switch (kuhinja){
case “true”: {
if (kuhinja_timer !== null) {
kuhinja_timer.cancel()
Timer = null}
sendCommand(mqtt_homie300_test_air_output1,“ON”)
}
case “false”:{
kuhinja_timer = createTimer(now.plusMinutes(3), [
sendCommand(mqtt_homie300_test_air_output1,“OFF”)
Timer = null])}
}
end
basicaly what happens is timer runs when motion sensor doesnt detect anyone and turns off the light, but it should cancel timer if someone returns to kitchen and it doesnt. so the light go of and then back on in a few seconds. also when every timer runs out it fills log with an error
Please use ‘code fences’ to post code in this forum.
You don’t need that import in OH2; you must have looked at some quite old OH1 examples.
This creates a new variable to hold the timer handle every time the rule runs.
If you want a second run of the rule to do something about a timer started by an earlier run of the rule, don’t do that.
If you look at examples, you will see the timer handle variable being declared outside of any rule, so that it has an existence between repeat rule runs.
See " Simple Example: Timers Version "
Rules DSL is a loosely typed language. kuhinja won’t be a string, it will be a state type object.
If you want a string, it is easily got -
var kuhinja = mqtt_topic_kuhinja_occupancy.state.toString
If you use the sendCommand() Action like that, it requires you to supply two strings.
thanks for your help, i will need a day or two to try and repair timer with what you wrote. i didnt have a problem with sending commands, it was working and i beleive i had trouble with the way you wrote, it didnt work for some reason, but will look into it as soon as i figure out timer. ill get back to you with updates.
i tried to fix the rule, its kinda working. it does restart timer, but once timer runs out and light is off it wont turn the light on again when someone enters. ill upload a pic of the error
var Timer kuhinja_timer
rule "Kuhinja"
when
Item mqtt_topic_kuhinja_occupancy received update
then
var kuhinja = mqtt_topic_kuhinja_occupancy.state.toString
switch (kuhinja){
case "true": {
if (kuhinja_timer !== null) {
kuhinja_timer.cancel()
Timer = null}
sendCommand("mqtt_homie300_test_air_output1","ON")
}
case "false":{
kuhinja_timer = createTimer(now.plusMinutes(2), [
sendCommand("mqtt_homie300_test_air_output1","OFF")
Timer = null])}
}
end
also do i need IF line where it look if timer !== null ? can it reset/cancel timer every time state changes to ON?
thanks for help and sorry if i did code fences wrong, not sure of what i am doing
after a short test this seems to work, thanks again. i owe you a beer
var Timer kuhinja_timer
rule "Kuhinja"
when
Item mqtt_topic_kuhinja_occupancy received update
then
var kuhinja = mqtt_topic_kuhinja_occupancy.state.toString
switch (kuhinja){
case "true": {
if (kuhinja_timer !== null) {
kuhinja_timer.cancel()
kuhinja_timer = null}
sendCommand("mqtt_homie300_test_air_output1","ON")
}
case "false":{
kuhinja_timer = createTimer(now.plusMinutes(4), [
sendCommand("mqtt_homie300_test_air_output1","OFF")
Timer = null])}
}
end