Here’s a sample dimmer rule, using a Zwave dimmer, Astro events, and a cron rule. It turns the dimmer light ON and dim (starts either at 0 or at the current value), when it starts getting dark outside. It then slowly raises the dimmer value up to full brightness (2% per minute, adjust as necessary to make it faster/slower).
I used the civilDusk event, which fires earlier than other sunset events since it gets darker quicker inside:
var Number Light_FF_Living_Sconces_Value = 0
rule "Sunset Indoor Functions"
when
Channel 'astro:sun:local:civilDusk#event' triggered START
then
/*Start Living Room Sconces*/
sendCommand(Light_FF_Living_Sconces_Dim, ON)
logInfo("Lights", "Living Sconces Dim Sequence Start")
end
rule "Minute Functions"
when
Time cron "0 * * * * ?"
then
/*Bring up sconces progressively from dim to bright*/
if (Light_FF_Living_Sconces_Value < 100)
{
if (Light_FF_Living_Sconces.state > 0)
{
Light_FF_Living_Sconces_Value = Light_FF_Living_Sconces.state as PercentType
}
else
{
Light_FF_Living_Sconces_Value = 0
}
if (Light_FF_Living_Sconces_Dim.state == ON)
{
Light_FF_Living_Sconces_Value = Light_FF_Living_Sconces_Value + 2
sendCommand(Light_FF_Living_Sconces, new PercentType(Light_FF_Living_Sconces_Value.toString))
logInfo("Lights", "Sconces at " + Light_FF_Living_Sconces_Value.intValue)
}
}
else
{
if (Light_FF_Living_Sconces_Dim.state != OFF)
{
sendCommand(Light_FF_Living_Sconces_Dim, OFF)
}
}
end
When I start OpenHAB, with this rule enabled, I get the following message:
2016-01-28 14:23:15.029 [ERROR] [.o.m.r.i.engine.ExecuteRuleJob] - Error during the execution of rule Minute Functions
java.lang.ClassCastException: Cannot cast org.openhab.core.library.types.PercentType to void
is Light_FF_Living_Sconces link to a real dimmer?
the binding in the exemple poll the dimmer every 60 seconds. if you remove that, the dimmer initial value is undefined and the rules will not work.
try moving the dimmer in the Web app before firing up the rule. if that work. then that’s your problem.
@sam2b, it may be much simpler but it is really bad practice.
Your rule is holding on one of the 5 threads available
You should do that with a timer:
Much safer
val int timeoutMills = 3 * 600 // 60000 divided by 100 dim levels
var int dimLevel = 1
var Timer timer = null
rule "Slowly Dim Up"
when
Item Outlet2_Switch changed to ON
then
if (timer === null) {
timer = createTimer(now.plusSeconds(0), [ | //Starts immediately
Light1_Dimmer.sendCommand(dimLevel)
if (dimLevel == 100) {
timer = null // cancel timer
} else {
dimLevel = dimLevel + 1
timer.reschedule(now.plusMillis(timeoutMills)) // reschedule timer is 3 * 600 mills
}
])
}
end
Hi, @vzorglub. Thanks very much for your help on this. (by the way, the code above needs a curly brace after time.reschedule() ).
Originally, I tried using a timer, but I went about it the wrong way. I just used your code and works just a smoothly. This was a good lesson.
Hi,
should this rule still work in oh4.2?
I’m trying to make it work, to dim down instead of dim up, and it partially works (keeps dimming down every minute as I specified) but it disregards the second if (where it should check if something changed to stop the dimming down loop and cancel the timer
it works with Thread::sleep(300) before the second if…