Rule: Multiple conditions to toggle two lights

I am trying to set 2 lights with multiple conditions and would like to ask if you would do the same way?

items:

Switch	Kuchenlicht "Küchenlicht" ["Lighting"] { channel="hue:0100:xxxxxxxxxxx:2:brightness" }
Number   SunElevation  "Elevation"  <sun>  (Astro) { channel="astro:sun:home:position#elevation" }

rules:

rule "KucheLichtAuto"
when 
	Thing "hue:0100:xxxxxxxxxxx:2" changed or
    Item SunElevation changed or
    Item Kuchenlicht changed
then
	var status = ThingAction.getThingStatusInfo("hue:0100:xxxxxxxxxxx:2").getStatus()
	
	if (status.toString() == 'ONLINE' || SunElevation.state >  0 || Kuchenlicht.state == ON ) {
		sendCommand(Gateway_Licht, OFF)		
	}
	else if (Kuchenlicht.state == OFF || SunElevation.state <  0) {
		sendCommand(Gateway_Licht, ON)
	}
end

What I want:

  • Turn ON the Gateway_Light if the SunElevation is lower than 0
  • Turn ON the Gateway_Light if the Kuchenlicht is OFF and SunElevation is lower than 0
  • Turn OFF the Gateway_Light if the Kuchenlicht is ON even if SunElevation is lower than 0
  • Turn OFF the Gateway_Light if the SunElevation is higher than 0

I’m pretty certain this is not valid. Rules do not work with Things directly except in a couple very specific ways, and this isn’t one of those ways.

But even if it were valid as written, it is redundant with Item Kuchenlicht changed because that Item is linked to the Thing.

What you have for the logic is mostly reasonable. I would probably order things a little differently as I like to avoid really long if statements when I can.

  // log warning if the Kuchenlicht is offline and do nothing
  if(ThingAction.getThingStatusInfo("hue:0100:xxxxxxxxxxx:2").getStatus().toString != "ONLINE") {
    logWarn("light", "The Kuchenlicht is not ONLINE!, cannot perform KucheLichtAuto")
    return;
  }

  val gatewayState = ON // default to ON

  // If the Kuchenlicht is ON or the SunElevation > 0 turn OFF
  if(Kuchenlicht.state == ON || SunElevation.state as Number > 0) gatewayState = OFF

  // Only send the command if the new state is different from the current state
  if(Gateway_Licht.state != gatewayState) Gateway_Licht.sendCommand(gatewayState)
1 Like

Thank you very much for your code and way to do it!

I get an info at my log. Should I ignore it?

22:20:17.867 [INFO ] [del.core.internal.ModelRepositoryImpl] - Validation issues found in configuration model 'myrules.rules', using it anyway:
Assignment to final variable

About to get the Gateway_Light ON if Thing “hue:0100:xxxxxxxxxxx:2” OFFLINE, I set it like here:

if(Kuchenlicht.state == OFF || SunElevation.state as Number < 0) gatewayState = ON

But there is an error in log

22:20:27.722 [WARN ] [del.core.internal.ModelRepositoryImpl] - Configuration model 'myrules.rules' has errors, therefore ignoring it: [98,67]: no viable alternative at input '0'

Should I use the Thing instead of the states?

val gatewayState = ON

should be

var gatewayState = ON

Show the code. What you have above is my line, not your line.

Also note, if your Thing is OFFLINE the Rule just logs a warning and exists as I wrote it above. It makes no sense to test it anywhere else.

The rule:

rule "KucheLichtOffline2"
when 
	Thing "hue:0100:xxxxxxxxxxx:2" changed or
    Item SunElevation changed or
    Item Kuchenlicht changed
then
	// log warning if the Kuchenlicht is offline and do nothing
    if(ThingAction.getThingStatusInfo("hue:0100:xxxxxxxxxxx:2").getStatus().toString != "ONLINE") {
        logWarn("light", "The Kuchenlicht is OFFLINE!, cannot perform KucheLichtAuto")
        //return;
    }

    var gatewayState = ON // default to ON

    // If the Kuchenlicht is ON or the SunElevation > 0 turn OFF
    if(Kuchenlicht.state == ON || SunElevation.state as Number > 0) gatewayState = OFF

    if(Kuchenlicht.state == OFF || SunElevation.state as Number < 0) gatewayState = ON

    // Only send the command if the new state is different from the current state
    if(Gateway_Licht.state != gatewayState) Gateway_Licht.sendCommand(gatewayState)
end

Logic:

Well the GatewayLight should go ON if Thing or Kuchenlicht is OFFLINE or SunElevation is lower than 0.
GatewayLight goes OFF if Kuchenlicht is ON and SunElevation is higher than 0.

Explain:

During the day I do not need the GatewayLight, so it should be OFF anyway.
After the Sun is set (SunElevation is lower than 0), if the Thing is OFFLINE (Switched off by the Wallswitch) or Kuchenlicht is OFF (Thing is ONLINE) or anyway if SunElevation is lower than 0 than it GatewayLight should be ON.

By that way I have a small light point in the dark and goes off if I act somehow.

I get the point that it will not run if the Thing is OFFLINE but need to act even if is not ONLINE. Than the part above will not work anyway if is OFFLINE.

Above you said:

  • Turn OFF the Gateway_Light if the Kuchenlicht is ON even if SunElevation is lower than 0
  • Turn OFF the Gateway_Light if the SunElevation is higher than 0

So my original rule turns off the light if the Kuchenlicht is ON or if the SunElevation is higher than 0. All other cases turn ON the light. So at best the following line is redundant but in this case it won’t work at all because you need an and, not an or.

if(Kuchenlicht.state == OFF || SunElevation.state as Number < 0) gatewayState = ON

Put in words:

if(Kuchenlicht.state == ON || SunElevation.state as Number > 0) gatewayState = OFF

if Kuchenlicht.state is ON, gatewayState = OFF. If Kuechenlicht.state != ON AND SunElevevation.state as Number > 0, gatewayState = OFF. In ALL other situations gatewayState = ON.