[HELP] Variables Not Working As Expected

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

You probably want to logInfo out the Time_Of_Day.state

Then you may want to add a default case to your switch statement.

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.

But the time of day switch-case is supposed to result in dimmingLevel not being -1 anymore, no?

Yes it does, however I have nothing for DAY. So will always receive a -1 and therefore not switch the light on.

Right, got it now.

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.

I guess you are not aware of the fact, that openHAB does its work asynchronous. So while the first line of this code:

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)
    }
}

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.