Light switch on based on daylight help

Hi,

I’m stuck with a rule - maybe someone could help me pointing to the error why this rule isn’t firing.

I’d like to :

  • Measure the light with my HUE motion sensor and trigger below 11000
  • check if it is day (I’ve got a switch for this state)
  • see if the lamp is on already (WZL)
  • check if someone’s home (Presence)
  • check if the family is away (Abwesend)
    if all of the above is true, switch on two lights (WZL and GF_Stairwell_Surface3)

and in the “else” part:
if all of the above is true and the light is >= 11000, switch off the lamp.

rule "Light on, if too less daylight"

when
        Item GF_Stairwell_Sensor_Licht changed

then
        if((GF_Stairwell_Sensor_Licht.state <= 11000) && (Presence.state == ON) && (Abwesend.state == OFF) && (Tag.state == ON)){
        logInfo("GF_Stairwell_Sensor_Licht", "Check if enough daylight: " +GF_Stairwell_Sensor_Licht.state +Presence.state +Abwesend.state +Tag.state)
        logInfo("Tag", "check if day: " +Tag.state)
        sendCommand(WZL, ON)
        sendCommand(GF_Stairwell_Surface3, 80)
        logInfo("Light goes on " + WZL.state)
        }else{
        if((GF_Stairwell_Sensor_Licht.state >= 11000) && (Presence.state == ON) && (Abwesend.state == OFF) && (Tag.state == ON) && (WZL.state == ON))
       logInfo("GF_Stairwell_Sensor_Licht", "Check if more daylight" +GF_Stairwell_Sensor_Licht.state)
        logInfo("Tag", "Check if day " +Tag.state +WZL.state +Presence.state +Abwesend.state)
 
        {
        sendCommand(GF_Stairwell_Surface3, 0)
      logInfo("more daylight, lamp off" + WZL.state)
      sendCommand(WZL, OFF)
        }
}
end

thanks upfront for your help,
Kurt

1 Like

I can only guess, but I think you need to convert the state of illumination item to a number ( add: “as Decimal”) in order to make it comparable to the number.
I would also put the two logInfo in the else part into the curly brackets.

Hi,

thanks a lot for your hint! I wasn’t aware that sting item values are not directly comparable to numbers.
this one is still driving me nuts and a bit more of your help would be appreciated :slight_smile:

I’ve tried this:

        var LichtEG = (GF_Stairwell_Sensor_Licht.state as Decimal)

        if((LichtEG <= 11000) && (Presence.state == ON) && (Abwesend.state == OFF) && (Tag.state == ON)){

and it’s throwing the error below:

Rule 'Light on, if too less daylight': void

if I use

        var LichtEG = (GF_Stairwell_Sensor_Licht.state as DecimalType)

        if((LichtEG <= 11000) && (Presence.state == ON) && (Abwesend.state == OFF) && (Tag.state == ON)){

I get the below error:

[ntime.internal.engine.RuleEngineImpl] - Rule 'Light on, if too less daylight': org.eclipse.smarthome.core.library.types.DecimalType

:thinking: Kurt

Please take a look at this slightly changed rule:

rule "Light on, if too less daylight"
when
    Item GF_Stairwell_Sensor_Licht changed
then
    val hue = (GF_Stairwell_Sensor_Licht.state as DecimalType).intValue 
    logInfo("Lights_rule","Presence: {} Abwesend: {} Tag: {}",Presence.state, Abwesend.state, Tag.state)
    if(Presence.state == ON && Abwesend.state == OFF && Tag.state == ON) {
        logInfo("Lights_rule","GF_Stairwell_Sensor_Licht: {}", hue)
        if(hue <= 11000) {
            WZL.sendCommand(ON)
            GF_Stairwell_Surface3.sendCommand(80)
            logInfo("Lights_rule","Light goes on. WZL: {}" + WZL.state)
        }	
        else if(hue > 11000 && WZL.state == ON) {
            GF_Stairwell_Surface3.sendCommand(0)
            WZL.sendCommand(OFF)
            logInfo("Lights_rule","more daylight, lamp off. WZL: {}", WZL.state)
        }
    }
end

You had several bugs in your rule, curly brackets at the wrong place and wrong usage of logInfo(). Although it’s possible to build a string and simply log this, you would have to use Item.state.toString. You would have to use additional +" "+ spaces to get a nice output. logInfo() is defined as logInfo(String,String) where the former is the logger name and the latter the text to log. If using "Log Message {}",var there is no need to convert var to string (aside from special vars like dateTime)

It’s more appropriate to split the if clause to the general (presence,abwesend,tag) and the more special two clauses (Sensor_Licht and WZL).

It’s better to use the method item.sendCommand(value) instead of using the action sendCommand(item,value), see http://docs.openhab.org/configuration/rules-dsl.html#sendcommand-method-vs-action for details.

Is the item GF_Stairwell_Sensor_Licht defined as Number? If not, you would have to use

val hue = new Number(GF_Stairwell_Sensor_Licht.state.toString)

to ensure that the value is of type number.

Hi!

Thanks a lot for your valuable help on the rule but most importantly, thanks for the guidance on the wrong usage of sendCommand & logInfo - this is extreemly helpfull!

Kurt