Timer - delete object, restart, cancel?

  • Platform information:

    • Hardware: OrangePi PC
    • OS: ARMBIAN
    • Java Runtime Environment: 1.8
    • openHAB version: 2
  • Issue of the topic: Good afternoon friends!
    Today I wanted to turn the fan on and off by choosing the time on the slider.
    I turn on the fan when a change occurs + create a timer. But if I decided to change the time during operation (increase or decrease), I reassigned the timer to a new off time, but my timer is not reassigned. Please tell me where there is good information about how to stop the timer, start, delete the timer object so that I can work well.

  • Items configuration related to the issue

Dimmer Fan_kitchen "Timer [%d min.]"

  • Sitemap configuration related to the issue
Slider item=Fan_kitchen minValue=0 maxValue=6 step=1 icon="fan"
  • Rules code related to the issue
import org.openhab.model.script.actions.*
import org.openhab.model.script.actions.Timer

var Timer fantimer = null

rule "Fan timer received"

when 
	Item Fan_kitchen received command
then    

		var fantime=(Fan_kitchen.state as DecimalType).intValue  
	
	if(fantime < 1) { 
		
			fantimer.cancel
			fantimer = null 
		
			if(Fan_kitchen.state!="OFF"){  SonoffBasic_fankitchen.sendCommand(OFF)  }
					 
		}	
		else {
				if(fantimer===null) {		

							SonoffBasic_fankitchen.sendCommand(ON)		
							fantimer = createTimer(now.plusMinutes(fantime), [|
									SonoffBasic_fankitchen.sendCommand(OFF) 
									Fan_kitchen.sendCommand(0) 
				
							])		
					}
					
					else {

						fantimer.reschedule(now.plusMinutes(fantime), [|
								SonoffBasic_fankitchen.sendCommand(OFF) 
								Fan_kitchen.sendCommand(0)			
						])
					
					
					}
			}					
end

rule "Item reset"
when 
	Item SonoffBasic_fankitchen received command OFF
then  
	Fan_kitchen.sendCommand(0)
end 
  • If logs where generated please post these here using code fences:
18:20:34.989 [INFO ] [arthome.event.ItemStatePredictedEvent] - SonoffBasic_fankitchen predicted to become OFF
18:20:35.010 [INFO ] [smarthome.event.ItemCommandEvent     ] - Item 'Fan_kitchen' received command 0
18:20:35.028 [ERROR] [untime.internal.engine.RuleEngineImpl] - Rule 'Fan timer received': cannot invoke method public abstract boolean org.eclipse.smarthome.model.script.actions.Timer.cancel() on null
18:20:44.529 [INFO ] [del.core.internal.ModelRepositoryImpl] - Validation issues found in configuration model 'fan.rules', using it anyway:
The use of wildcard imports is deprecated.

You need to test if the fan timer is null -> fantimer?.cancel

Imports are not needed, neither are the brackets.

You only need to reschedule, the timer does know what to do -> else {fantimer.reschedule(now.plusMinutes(fantime) } This is enough.

1 Like

Using “?” we are checking? or is this condition necessary?

We are checking for null. See this post: Help, I'm ready to give up . The ? operator can be used almost everywhere, where a null state can happen.

It is necessary, why: simple if you send command 0 to Fan_kitchen if you just save the rule or if you’re previous command was also 0, the timer wil be null.
null can’t be canceled (obviously), so that’s why you have to check. That’s why you get the error in your log.

Many thanks! I will correct and verify everything and I will definitely write here

1 Like