Here’s my rule to control my landing light. I want to vary the brightness depending on the time of day or light level. I’ve clearly not understood who to use variables correctly as I get this error each time. I can’t seem to find a similar example on the forum so need a little pointer.
2019-10-20 11:16:03.749 [WARN ] [rthome.model.script.actions.BusEvent] - Cannot convert '-1' to a command type which item 'First_Floor_Landing_Light_Dimmer' accepts: [PercentType, OnOffType, IncreaseDecreaseType, RefreshType].
rule "Landing Ceiling Light"
when
Item Landing_CeilingLight_vSwitch changed
then
var Number dimmingLevel = -1
if(Porch_ST815_Lux_Level.state < 200) {
dimmingLevel = 100
}
switch Time_Of_Day.state {
case "EVENING": {
dimmingLevel = 65
}
case "NIGHT": {
dimmingLevel = 25
}
case "BED": {
dimmingLevel = 15
}
case "MORNING": {
dimmingLevel = 35
}
}
if(dimmingLevel < 1) Landing_CeilingLight_vSwitch.sendCommand(OFF)
if(Landing_CeilingLight_vSwitch.state == ON) {
if(First_Floor_Landing_Light_Dimmer.state < 1) {
First_Floor_Landing_Light_Dimmer.sendCommand(dimmingLevel)
}
}
if(Landing_CeilingLight_vSwitch.state == OFF) {
if(First_Floor_Landing_Light_Dimmer.state > 0) {
First_Floor_Landing_Light_Switch.sendCommand(OFF)
}
}
logInfo("org.openhab.rules", "Lights: Landing Ceiling Light {}.", Landing_CeilingLight_vSwitch.state)
end
The Time_Of_Day.state is fine, it’s the trapping of the -1 from dimmingLevel that’s causing the error. If I remove the -1 value from the variable then I don’t get an error and the light comes on at the level I request.
You may want some “else” in your if() logic.
The first if() will execute an OFF if dimmingLevel -1 … but that doesn’t stop it going on to the following if()s
You may been expecting Landing_CeilingLight_vSwitch to have state OFF if you just sent it command OFF?
It doesn’t.
Commands (and postUpdates) are asynchronous, the rule does not stop and wait for them to be carried out before continuing.
will switch Landing_CeilingLight_vSwitch to OFF, the next line is executed long time before Landing_CeilingLight_vSwitch gets its new state, so even if the vSwitch is switched off, the next block is executed, if the vSwitch was on before. So better check the dimmlevel in addition to the switch state.