[SOLVED] Help with rules for turning on lights when door opens and sun has gone down

Here’s what I’m trying to make in pseudo:

When the front door opens

Check: Has the sun set? if yes: Turn on hallway lights. If not: Do nothing.

This is my rule:

rule "Gang Lys"

when
    Item FrontDoor_Switch changed from OFF to ON
then

    if(LocalSun_Position_Elevation.state < 0) {

        Sonoff_Gang.sendCommand(ON)
    
    }
end

I’ve tried adding debug lines, but it seems they never fire at all. however, when I made a typo in the elevation state, and I tried to run the rule by opening my front door, OH logged an error, after fixing the typo, I haven’t gotten it to run.

Edit: The issue has been solved, For good measure here’s the complete and correct rule, should you find this by way of google and need a template:

	rule "Gang Lys"

	when
		Item FrontDoor_Switch changed from OFF to ON
	then

		if(LocalSun_Position_Elevation.state < 0|°) {
        GangLys.sendCommand(ON)
		}
	end

I use the Astro binding with an Item that I call IsItDark.

1 Like

Does FrontDoor_Switch actually change from OFF to ON? If it changes from any other state or does not change at all the rule won’t trigger. Look in events.log to see the Item changing states.

Add back the logging to get evidence that rule is triggering or maybe just the if statement evaluates to false.

1 Like

I’d love to see how you made that Item. It would be handy for several things I have in mind.

It does, yeah.

Just to try something i changed the code to:

rule "Gang Lys"

when
Item FrontDoor_Switch changed from OFF to ON
then

if(LocalSun_Position_Elevation.state < 0) {
	return;

}
	 GangLys.sendCommand(ON)
end

And that works, insofar that if the door opens, the switch goes from off to ON, then the if is basically disregarded, and the command is sent to the light which turns on.

I assume it’s something to do with LocalSun_Position_Elevation.state < 0 but I’m not sure what.

I get the value from the Astro binding, it’s entry in the databse looks like this:

 "link": "http://192.168.1.226:8080/rest/items/LocalSun_Position_Elevation",
    "state": "-14.922239921456542 °",
    "stateDescription": {
      "pattern": "%.2f %unit%",
      "readOnly": true,
      "options": []
    },
    "editable": true,
    "type": "Number:Angle",
    "name": "LocalSun_Position_Elevation",
    "label": "Elevation",
    "category": "sun",
    "tags": [],
    "groupNames": []
  },

And whenever the elevation changes, the change value is updated, correctly:

2019-08-15 21:16:25.416 [vent.ItemStateChangedEvent] - LocalSun_Position_Elevation changed from -14.520239273750608 ° to -14.922239921456542 °

I use the Astro binding with an Item that I call IsItDark .

That could work, but seems unnecessary to me.

The Astro binding already has a channel for the elevation of the sun, and when it’s positive, it’s daytime, and when it’s negative, it’s nighttime, giving basically the same functionality, no?

The item type of LocalSun_Position_Elevation is a String with a value of “-14.922239921456542 °”.
If you compare this with a integer (0 in your case) this won’t work.

You’ll have to strip the “°” symbol and convert the rest to a number (decimal) to compare it.

Better way: You can make an item and bind it to the the channel “astro:moon:home:phase#name” and use this to compare it with the desired phase (NIGHT for instance). See https://www.openhab.org/addons/bindings/astro/#channels

Is it a string, or is it Units of Measurement making it proper degrees? If so, shouldn’t the UOM stuff be able to convert it?

Sadly, there’s still things about that I don’t follow and need to dig into, so I’m not sure.

1 Like

First, for sunset with the astro binding you should be able to use astro:sun:local:set#event to detect sunset. I did it differently based on something I found here.
I set up 2 Items for sunrise & sunset

DateTime sunrise_time "Sunrise [%1$tr]" <sunrise>  {channel="astro:sun:local:rise#start"}
DateTime sunset_time "Sunset [%1$tr]" <sunset> {channel="astro:sun:local:set#start"}

For whether it is dark I set up an item Switch IsItDark <moon> Here are its rules

rule "Sunset"
when
    Channel 'astro:sun:local:set#event' triggered START
then
    IsItDark.postUpdate(ON)
end

rule "Sunrise"
when
    Channel 'astro:sun:local:rise#event' triggered START
then
    IsItDark.postUpdate(OFF)
end

You can then use IsItDark in rules.

For my motion sensor (for my son’s Papillon dogs), if it is dark motion turns the light on. If no motion has been detected for 5 minutes, the light is turned off. I use the expire binding.
5 minute Timer item : Switch MyTimer { expire="5m,command=OFF" }
rules

rule "Papillon Motion On"
when
    Item Papillon_Motion changed from OFF to ON
then
    if (IsItDark.state == ON) {
        Papillon_Light.sendCommand(ON)
        // start Timer
        MyTimer.sendCommand(ON)
    }
end

rule "Papillon Motion Off"
when
    Item MyTimer changed from ON to OFF
then
    Papillon_Light.sendCommand(OFF)
end

You are right, my bad!
The item type is number:Angle

Unfortunately I’m not familiar enough with UOM to say how to use in a comparrison

Will something like this work?

if(LocalSun_Position_Elevation.state < 0|°) {
        Sonoff_Gang.sendCommand(ON)
}

Not sure and not able to test right now but found something about it here:
https://www.eclipse.org/smarthome/blog/2018/02/22/units-of-measurement.html

1 Like

Yes, that should work.

I’m not sure I follow, you’re still comparing an integer to a string, aren’t you?

Or is some conversion before the comparison implied?

Edit: Ah I see we established it’s not a string. However after reading the Eclipse docs I see why it should work, but it dosen’t seem to, when I change it to >0 (because it’s daytime here now, so testing with that).

Edit 2: My mistake, had 1 too many brackets, it works perfectly no, thanks!

Furiously jots down, Read the binding documentation dammit.

2 Likes

Another option (just to mention) for a UoM workaround:

if((LocalSun_Position_Elevation.state as Number).floatValue < 0)
2 Likes