Thanks for this. Hacky or not, it is very useful.
Though I did opt for now.plusMinutes
to save a few processes.
I now use it with a selection menu, where the value of the selection is used as the timer.
I signed up to the crowdsourced wifi service fon which allows you to use any Fon hotspot around the world for free. Providing you share a little bit of yours. They give you a little router to plug into your own. All secured from your local network.
I explicitly asked about where the traffic (the external ip) from someone using my hotspot would appear to be coming from. I was told it would be linked to the account of the member using it with no link to my ip. I was doubtful that it was even possible to route to another IP and I questioned it several times to be sure.
However when I plugged in and tested it, the external IP is the same IP as if I was using the local network.
When I contacted to complain, they said not to worry, the IP is of my ISP not my computer, the internal IPs are safe. I said that I would happily write 192.168.1.123 on the wall because its useless. The external IP is the one that is the problem. Tisk.
So now I only share at random times every now and then to still keep the service.
items
Number Fon_Timer_Selector "Timer duration"
Number Fon_Timer_Remaining "Remaining minutes [%s]"
// additional items so I can see the start/stop times on the sitemap
DateTime Fon_Timer_DateTime_Start "Last Start/Update [%1$tA, %1$tm/%1$td, %1$tI:%1$tM %1$tp]"
DateTime Fon_Timer_DateTime_Stop "Last Stopped [%1$tA, %1$tm/%1$td, %1$tI:%1$tM %1$tp]"
sitemap
Frame label="Fon Switches" {
Selection item=Fon_Timer_Selector icon="clock" mappings=[0 = "Off",15 = "15 minutes",30 = "30 minutes",60 = "60 minutes",120 = "120 minutes"]
Text item=Fon_Timer_Remaining visibility=[Fon_Timer_Selector>0] icon="clock"
Switch item=Socket_02 label="Fon power switch" icon="power" labelcolor=[Socket_02==ON="red"]
}
Frame label="Fon Info" {
Text item=Fon_Timer_Remaining
Text item=Fon_Timer_DateTime_Start
Text item=Fon_Timer_DateTime_Stop
}
rules
import org.openhab.core.library.types.*
import org.openhab.model.script.actions.*
import org.joda.time.*
var Timer timerFon = null
// As a precaution in case I restarted the Pi or there is a power cut, an OFF command is send at startup to the RFXcom switch
// This is because the timer would not exist after a restart or power cut and the switch would never receive the intended OFF command
rule "Initialize Fon switches on startup"
when
System started
then
sendCommand(Socket_02, OFF)
postUpdate(Fon_Timer_Selector,0)
end
// Start a timer using the value of the selection item in the UI
rule "Turn Fon on for selected time"
when
Item Fon_Timer_Selector changed
then
logInfo("Fon_Timer_Selector", "changed")
val org.eclipse.xtext.xbase.lib.Functions$Function2 makeTimer = [
int myDelay,
org.eclipse.xtext.xbase.lib.Functions$Function2 makeTimer2 |
if(myDelay>0) {
postUpdate(Fon_Timer_Remaining, myDelay)
// to save processes I only update UI once a minute
timerFon=createTimer(now.plusMinutes(1)) [|
postUpdate(Fon_Timer_Remaining, myDelay-1)
makeTimer2.apply(myDelay-1, makeTimer2)
]
}else {
// timer reach zero
postUpdate(Fon_Timer_Selector,0)
// what I want to happen when the timer stops
sendCommand(Socket_02, OFF)
postUpdate(Fon_Timer_DateTime_Stop, new DateTimeType())
}
]
// if the timer already exists, it will be canceled and replaced with a new one
if(timerFon!=null) {
timerFon.cancel
timerFon=null
postUpdate(Fon_Timer_Remaining,0)
}
// extracts the value of the selection menu so you can set different times through the UI
var int int_Fon_Selection = (Fon_Timer_Selector.state as DecimalType).intValue
// updates the sitemap item straight away otherwise you have to wait 1 minute
postUpdate(Fon_Timer_Remaining, int_Fon_Selection)
if(Fon_Timer_Selector.state>=15){
// start the timer if Fon_Timer_Selector is greater than 15 - could be any number greater than 0
logInfo("Fon_Timer_Selector", "state = " + Fon_Timer_Selector.state.toString())
makeTimer.apply(int_Fon_Selection,makeTimer)
// what I want to happen at the start of the timer
sendCommand(Socket_02, ON)
postUpdate(Fon_Timer_DateTime_Start, new DateTimeType())
}
end