Turn wifi plug/socket off after x minutes (timer)

Can someone give me an example on how to turn off a wifi plug/socket after x minutes?

I am using openhabian

I want to be able to do the following:

In the GUI on Classic UI or in the iOS/Android app, I want to be able to turn on the wifi plug and it should turn off after the number of minutes set in the GUI

This is currently my approach:

my rule:

var Timer myTimer = null

rule "my auto off rule"
when
    Item WLanSteckdoseBRo_Switch changed
then
    if (WLanSteckdoseBRo_Switch.state==OFF) {
        myTimer.cancel
        myTimer = null
    }
    else if (WLanSteckdoseBRo_Switch.state==ON) {
        if (myTimer!=null) {
            myTimer.cancel
            myTimer = null
        }
        myTimer = createTimer(now.plusMinutes((myMinutesSteckdose.state as DecimalType)).intValue) [| //use now.plusSeconds() for testing
            WLanSteckdoseBRo_Switch.sendCommand(OFF)
        ]
    }
end

This is my entry in my sitemap:

Setpoint item=myMinutesSteckdose step=0.5 minValue=1 maxValue=1000 icon="clock"

and this is my entry in my .items file

Number myMinutesSteckdose "Auscschalten nach [%s]"

Somehow it is not turning off after the amount of time I set. What am i doing wrong?

Just saw this in the log: how do i fix this ?

2017-12-16 18:24:41.903 [ERROR] [.script.engine.ScriptExecutionThread] - Rule ‘my auto off rule’: An error occurred during the script execution: Could not invoke method: org.joda.time.DateTime.plusMinutes(int) on instance: 2017-12-16T18:24:41.901+01:00

I would use the EXPIRE binding…it’s much simpler to implement…

This is how I use it for a momentary switch…change the expire= to the amount of time you want for the switch to turn off after turning on.

Switch FGate "Front Gate" [ "Switchable" ] { channel="zwave:device:xxxxxxx:node4:switch_binary",expire="1s,command=OFF" }

@KidSquid but using the expire he hasn’t got the possibility to use a setpoint item to change the expiration time from a sitemap, correct?

@salexes
I do have something similar. I’ve got two valves for watering my garden. I can enable the irrigation via a switch in the sitemap and by using setpoint items I can configure the time the valves should be open.

My item (sorry for mixing german and Englisch - i need to get rid of that soon :wink: )

Number VentilHochbeet_Delay			"Abschaltung Hochbeet [%d min]"		
Number VentilJohannisbeeren_Delay	"Abschaltung Johannisbeeren [%d min]"

My sitemap:

	Frame label="Bewässerung"
	{
		Switch 		item=Gartenbewaesserung 			label="Gartenbewässerung" 		mappings=[ON="An", OFF="Aus"]
		Text 		item=VentilHochbeet_Switch 			label="Hochbeet"
		Text 		item=VentilJohannisbeeren_Switch 	label="Johannisbeeren"
		Setpoint 	item=VentilHochbeet_Delay 			icon="time" step=10 minValue=10 maxValue=120
		Setpoint 	item=VentilJohannisbeeren_Delay		icon="time" step=10 minValue=10 maxValue=120
	}

The two setpoint items are for defining the time my valves should be closed.
The first Item enables the whole irrigation.

And finally my rule:

rule "Bewaesserung"

when 
    Item Gartenbewaesserung received command	
then	
    if(receivedCommand==ON) {
    	var Ventil1_Delay = VentilHochbeet_Delay.state as DecimalType
    	var Ventil2_Delay = VentilJohannisbeeren_Delay.state as DecimalType
    	
    	logInfo("Gartenbewaesserung","Bewaesserung gestartet")
        
        createTimer(now.plusMinutes(Ventil1_Delay.intValue)) [
        	sendCommand(VentilHochbeet_Switch, OFF)			//item created via PaperUI 
        	logInfo("Gartenbewaesserung","Ventil Hochbeet - Aus")
        ]
        
        createTimer(now.plusMinutes(Ventil2_Delay.intValue)) [
        	sendCommand(VentilJohannisbeeren_Switch, OFF)	//item created via PaperUI 
        	logInfo("Gartenbewaesserung","Ventil Johannisbeeren - Aus")
        ]
    } 
    else if(receivedCommand==OFF) {
    	logInfo("Gartenbewaesserung", "Deaktiviert durch Benutzer")
    	sendCommand(VentilHochbeet_Switch, OFF)
    	sendCommand(VentilJohannisbeeren_Switch, OFF)
    }
end

This was working the whole summer.

The only difference imho is that i’n using additional variables for storing the numbers of the setpoint items in my rule.
Have you set up a persistence service?

If not, myMinutesSteckdose is not initialized after reboot of openHab and therefore you either need to set up a persistence service or you check the value against null in your rule.

1 Like

Perfect thanks alot for your help!