Introduction
I have a question as to how to work my idea out. I am using my KlikAanKlikUit poweroutlet switches with OpenHab and I love it. When my phone battery is reaching the charged capacity of 95%, Tasker sends an OFF command to OpenHab and the switch (and thus the phone charger) switches off. Good for the battery, great for preventing fires due to overcharging.
question
Now, most devices I charge don´t have the luxury of telling OpenHab what to do, e.g. a Nintendo 3DS. In order to prevent overcharging, I was expirimenting with a user-set timer. The timer would be set in the sitemap. This timer would run while the device is charging and switch off the charger when the timer runs out, preventing the device from charging any further.
Unfortunately, I did not have any luck getting my idea to work. I used a setpoint in my sitemap to set the time, the problem that rose was that I did not know how to ´transform´ this setpoint to a useable variabele.
used information
Searching the forum I came across the following information:
and
Does anyone have any idea or leads for me how to work this idea out? Thanks in advance!
var Timer timer = null //Global variable
rule "MySwitch turned ON"
when
Item MySwitch changed to ON
then
val timerDuration = (MySwitchTimer.state as Number).intValue // transforms the Timer item value into an integer as timers only accept integers as arguments
if (timer === null) { // If the timer is not running already
timer = createTimer(now.plusMinutes(timerDuration), [ | // Creates a routine that will run a the time of now plus timerDuration minutes
// Timer routine starts
mySwitch.sendCommand(OFF) // Turns the switch off
timer = null // cancel the timer
// Timer routine ends
])
}
end
I use the expire binding to set a timer in some of my rules. Here is an example that turns off a light switch 3 minutes after last motion is detected. New motion resets the timer.
Item:
Switch BasementTimer { expire="3m,command=OFF" }
Rules
rule "Basement Light On"
when
Item basement_motion_1 changed from OFF to ON or Item basement_motion_2 chan
ged from OFF to ON
then
basement_light.sendCommand(ON)
// start or reset Timer
BasementTimer.sendCommand(ON)
end
rule "Basement Light Off"
when
Item BasementTimer changed from ON to OFF
then
basement_light.sendCommand(OFF)
end
Thank you for the code. I implemented it but unfortunatley I get two errors.
The ChargerSwitchTimer.state gives the following error: ¨The method or field State is undefined for the type NumberItem¨
The timer inside the if statement gives the same error ¨the method or field timer is undefinded¨.
Do you happen to know what could resolve these errors?
My (euhrm…your…hehe:relaxed:) rule:
rule "ChargerSwitchTimer"
when
Item MySwitch changed to ON
then
val timerDuration = (ChargerSwitchTimer.State as Number).intValue
if (timer === null) {
timer = createTimer(now.plusMinutes(timerDuration), [ |
ChargerSwitch.sendCommand(OFF)
timer = null
])
}
end
You are welcome.
Just make sure you read the code that you copied and pasted and understand it
This use of timers is very, very useful and you will need it again.
Anything not clear just ask.
I have added comments to the code