Trying to run openhab2 timer but i have multiple problems

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

and i have no idea what is exactly wrong here.
thanks for any help! :slight_smile:

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.

sendCommand("mqtt_homie300_test_air_output1", "OFF")

it’s generally preferable to use the method on the Item though

mqtt_homie300_test_air_output1.sendCommand("OFF") 
// or if this is a Switch type Item
mqtt_homie300_test_air_output1.sendCommand(OFF) 

1 Like

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. :slight_smile:

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

this is the rule

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

This creates a variable of name kuhinja_timer and type Timer.

This checks a variable named kuhinja_timer

This sets some other variable that we haven’t seen before, named Timer, to null

I think you’ve just got names muddled up.

1 Like

after a short test this seems to work, thanks again. i owe you a beer :slight_smile:


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

i do still have this error every time timer runs


dont like it because it fills the log but not a big deal in the end.

1 Like

Well, yes - your timer code block includes this -

and there is no variable called Timer. Un-muddle that name as well.

omg i am a blind idiot >.< i was like “whaaat i fixed that” :sweat_smile: