[Solved] OHv2: I thought I have a basic idea of timers by now :(

OHv2.5

I have this rule:

rule "NLO KDL window shade position"
	when
		Item TX433_Blind_StdShade changed to ON
	then
		// {mqtt=">[mymosquitto:ArgyleCourt/Shed/RF433TX/Command:command:ON:13]", expire="2s,command=OFF"}
    	// 12 = Blind 1 = NLO_Window; 2 = down
        publish("mymosquitto", "ArgyleCourt/Shed/RF433TX/Command", "12")

		if (NLO_Window_Timer === null || NLO_Window_Timer.hasTerminated())
		{
			NLO_Window_Timer = createTimer(now.plusSeconds(8), [|
        		// 13 = Blind 1 = NLO_Window; 3 = stop
        		publish("mymosquitto", "ArgyleCourt/Shed/RF433TX/Command", "13")
				TX433_Blind_StdShade.sendCommand(OFF)
				NLO_Window_Timer.cancel()
			])
		}
		/*
		else
		{
			NLO_Window_Timer.cancel()
        	// 13 = Blind 1 = NLO_Window; 3 = stop
			publish("mymosquitto", "ArgyleCourt/Shed/RF433TX/Command", "13")
		}
		*/
end

… without the else part it works.
With the else part it briefly starts the blind and stops it. Why?
The timer is null, hence, the else part should not run!

I also noticed that I cannot check the status of a time in karaf:

openhab> smarthome:status NLO_Window_Timer
Error: Item 'NLO_Window_Timer' does not exist.

Any hints appreciated.

We’ll also want to see events.log to see what TX433_Blind_StdShade is doing. The rule is triggering again before the timer expires.

Well… OH seems to work just fine… it seems the blind motor needs to be controlled differently.

Here the events.log

--> without else
2021-08-11 18:42:44.621 [ome.event.ItemCommandEvent] - Item 'TX433_Blind_StdShade' received command ON
2021-08-11 18:42:44.627 [vent.ItemStateChangedEvent] - TX433_Blind_StdShade changed from OFF to ON
2021-08-11 18:42:52.643 [ome.event.ItemCommandEvent] - Item 'TX433_Blind_StdShade' received command OFF
2021-08-11 18:42:52.649 [vent.ItemStateChangedEvent] - TX433_Blind_StdShade changed from ON to OFF
--> with else
2021-08-11 18:43:47.725 [ome.event.ItemCommandEvent] - Item 'TX433_Blind_StdShade' received command ON
2021-08-11 18:43:47.732 [vent.ItemStateChangedEvent] - TX433_Blind_StdShade changed from OFF to ON
2021-08-11 18:43:55.781 [ome.event.ItemCommandEvent] - Item 'TX433_Blind_StdShade' received command OFF
2021-08-11 18:43:55.813 [vent.ItemStateChangedEvent] - TX433_Blind_StdShade changed from ON to OFF

Thanks for looking into this.

This motor an AM25 tube motor only needs three commands, up, down, stop.
I have to revisit why there is an after-down|-up command that doesn’t seem to be required. Maybe it is… I have to investigate.

For now this post is marked resolved, as it is not a rule issue. Thanks. :slight_smile:

Well, no. Variables only exist inside the rules framework. If you want to know what it is, you must log it out in your rule. (Timer handle variables look like gobbledygook when you do that, that’s fine, you’re interested if null or not)

There’s no point putting timer cancel() at end of its own code block - it’s already done by then. Maybe you meant to set its own handle to null, that’s a common technique.

Thanks… proves I still don’t get this timer business :slight_smile:

The (I hope) proper timer code below:

rule "NLO KDL window standard shade position"
	when
		Item TX433_Blind_StdShade changed to ON
	then
    	// 12 = Blind 1 = NLO_Window; 2 = down
        publish("mymosquitto", "ArgyleCourt/Shed/RF433TX/Command", "12")

		if ((NLO_Window_Timer === null) || (NLO_Window_Timer.hasTerminated()))
		{
			NLO_Window_Timer = createTimer(now.plusSeconds(8), [|
        		// 13 = Blind 1 = NLO_Window; 3 = stop
        		publish("mymosquitto", "ArgyleCourt/Shed/RF433TX/Command", "13")
				TX433_Blind_StdShade.sendCommand(OFF)
				NLO_Window_Timer = null
			])
		}
		else
		{
			// the case when standard shade button is pressed again within the 8s
			NLO_Window_Timer.cancel()
			NLO_Window_Timer = null
        	// 13 = Blind 1 = NLO_Window; 3 = stop
			publish("mymosquitto", "ArgyleCourt/Shed/RF433TX/Command", "13")
		}
end