[SOLVED] Turn on light in the morning, if sun is down

Hi out there.

I have been looking around for a while, and find the most solutions for me on this great forum.

I´m quite new to Openhab, and now I need some help.

I have some rules for turning on the light when the sun goes down (via Astro), and to turn them off at certain time and day.

Here in the dark time, I would also like to have the lights to turn ON at 7.00 (AM), but only if the sun is down (dark outside)

Is there a workaround for this?

Check the docs for astro again:


you can configure the channels with a Latest attribute that does exactly what you want

Hello.
The reason why I made this topic, is because I can’t find the answer for my question.
Neither in the link you have sent, and I have seen the docs before.

Go it, sorry…here a design pattern that seems to apply:

Will this do the trick for me?

rule “Morning light”

when

Time cron "0 45 6 ? * MON,TUE,WED,THU,FRI *"

then

if(astro:sun:local:daylight#event == END){
sendCommand(Indkorsel, 25)
}

end

Sorry we have to wait until more knowledgeable people join
But here a few pointers: id you get any errors in the editor VSC? did you et errors saving the file?
Try some other times (like every 10 seconds or so) add some logInfo statements to see when your rule triggers, and to log out all times, etc…
You can also just adapt the Day of Time pattern that I linked, it works great, I am using it for years.

The time of day pattern mentioned above will do what you want, but if you want a smaller starting point, you can set up two rules, one with a trigger of:

Channel 'astro:sun:home:rise#event'    triggered START

and a second with a trigger of:

Channel 'astro:sun:home:set#event'    triggered START

In the upper rule, change an item to indicate day time, something like dayTime.sendCommand(ON). In the lower rule, turn dayTime off: dayTime.sendCommand(OFF). Now the Item dayTime will indicate day or night. Use the state of the item along with your cron rule to decide to turn the light on or not.

Change the astro line appropriately, might be ‘local’ instead of ‘home’ in your case.

I find this easiest. It is also possible to do time comparison since the astro binding does indeed give you the sunrise time, but I find that more trouble than just triggering an item on and off at sunrise and sunset. You can see examples of this in the time of day pattern that was mentioned.

No. because you’re looking at an event, which exists for only a moment in time.

The design pattern shows how to use astro events to control a state item like “dark outside” on or off.
Then you can make a rule trigger at 07:00 that looks to see, “is it dark? Lights on”
Another rule to run at sunrise, “Are lights on? turn them off”

For me I don’t use daylight. I use sun elevation. If sun elevation is zero, 5 minutes later , I’m pretty sure it’s dark.

I´m really, really lost.
Everything else about the configuation of openhab I found a solution for.
But I´m getting more and more confused.

My head is done for today!

What you basically need to do, is to make two rules: 1 that turns on the light when it is 7 AM for example, but only if the current time is before the time at which the sun gets up (for which I use the civil dawn end, but you are free to change that of course)
Secondly I turn off the light when the sun gets up in a second rule.

rule "turn on lights if still dark"
	when
		Time cron "0 0 7 ? * MON-FRI *"
	then
		if (now.isBefore((astro_sun_local_civilDawn_start.state as DateTimeType).getZonedDateTime.toInstant.toEpochMilli)){
	            LightSwitch.sendCommand(ON)
		}
end

rule "Turn off light when it gets light"
	when
		Channel "astro:sun:local:rise#event" triggered END
	then
		LightSwitch.sendCommand(OFF)
end
1 Like

That was a nice and simple solution.
I wrote it in in VSC, and no errors accured, so I will see on Monday, if it works.

I did alter it a bit, so it fits my configuration.

Thank you.

rule “Tænd indkørsel om morgenen hvis der er mørkt”
when
Time cron “0 45 6 ? * MON-FRI *”
then
if (now.isBefore((astro_sun_local_rise_start.state as DateTimeType).getZonedDateTime.toInstant.toEpochMilli)){
sendCommand(Indkorsel, 25)
}
end

rule “Sluk indkørsel om morgenen”
when
Time cron “0 00 8 ? * MON-FRI *”
then
sendCommand(Indkorsel, 0)
end

You’re welcome. Please mark my answer as solution to help others find it easier.
As a general remark, it is preferred to use the method item.sendCommand(ON) rather than the function sendCommand(item, ON).

I will do that.

Also corrected the Command tags as described, and put in an OR for turning off the light :slight_smile:
Thank you for the solution, I knew it could be done :smiley:

rule "Tænd indkørsel om morgenen hvis der er mørkt"
	when
		Time cron "0 45 6 ? * MON-FRI *"
	then
		if (now.isBefore((astro_sun_local_rise_start.state as DateTimeType).getZonedDateTime.toInstant.toEpochMilli)){
	            Indkorsel.sendCommand(25)
		}
end

rule "Sluk indkørsel om morgenen"
	when
		Time cron "0 00 8 ? * MON-FRI *"
		or
		Channel 'astro:sun:local:rise#event' triggered END
	then
		Indkorsel.sendCommand(0)
end

That’s not a magic phrase, by the way. It’s an Item that you need to somehow fill with the required time value.
You need to create an Item by that name, and connect it to an astro channel (hint: very similar channel name!), to get the astro binding to place its calculated value in there.

I apologize if this post looks odd, this is my first post :slight_smile:

Hi,

Have spend evenings trying to figure out why above doesn’t work for me.
Turning to the forum, and hope someone can help me further.

I’m running Openhabian 2.4

My code is identical to the one Michael Pedersen added above.
timer.rules

rule "Tænd spot facade når det er mørkt"
	when
		
        Time cron "0 45 06 ? * MON-FRI *"
	then
		if (now.isBefore(("astro_sun_local_rise_start.state" as DateTimeType).getZonedDateTime.toInstant.toEpochMilli)){
                       
                    Spot_Facade.sendCommand(ON) 
                    Spot_Terrasse.sendCommand(ON)

                }else{
                    
                    Spot_Facade.sendCommand(OFF) 
                    Spot_Terrasse.sendCommand(OFF)
                   
		} 
end

And the log return this error:

2019-11-28 20:50:27.408 [INFO ] [el.core.internal.ModelRepositoryImpl] - Validation issues found in configuration model 'timer.rules', using it anyway:

Cannot cast from String to DateTimeType

2019-11-28 20:50:27.508 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'timer.rules'

==> /var/log/openhab2/events.log <==

2019-11-28 20:50:33.505 [vent.ItemStateChangedEvent] - mpower_socket_24_A4_3C_D7_4F_F3_1_power changed from 16.7 to 17.3

2019-11-28 20:50:34.853 [vent.ItemStateChangedEvent] - CurrentDate changed from 2019-11-28T20:49:34.792+0100 to 2019-11-28T20:50:34.800+0100

2019-11-28 20:50:34.862 [vent.ItemStateChangedEvent] - Sunset_Time changed from 2019-11-28T20:49:34.792+0100 to 2019-11-28T20:50:34.800+0100

2019-11-28 20:50:34.867 [vent.ItemStateChangedEvent] - Weather_LastUpdate changed from 2019-11-28T20:49:34.792+0100 to 2019-11-28T20:50:34.800+0100

2019-11-28 20:50:34.872 [vent.ItemStateChangedEvent] - Sunrise_Time changed from 2019-11-28T20:49:34.792+0100 to 2019-11-28T20:50:34.800+0100

That’s a string, the one complained about in the error.
astro_sun_local_rise_start.state , without the quotes, would refer to the state of the Item

Thanks for the fast reply - that actually made sense.
Now I’m back with the other error:

2019-11-28 22:44:00.635 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule 'Tænd spot facade når det er mørkt': The name 'astro_sun_local_rise_start' cannot be resolved to an item or type; line 6, column 21, length 26

Any idea ?

It’s self explanatory. You do not have an Item named astro_sun_local_rise_start
Have you installed Astro binding, do you have Astro Thing(s), have you linked any Items to channels of those Things, what are the Items named?

Yes, Astro Binding is installed, and I have things.
My Things syntax is as:
image

My problem might be with understanding when to use “_” vs “:”

If I use this instead

		if (now.isBefore((astro:sun:local:rise#start.state as DateTimeType).getZonedDateTime.toInstant.toEpochMilli)){
                       
                    Spot_Facade.sendCommand(ON) 

I get new errors:

2019-11-28 23:09:56.966 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'timer.rules' has errors, therefore ignoring it: [6,26]: mismatched input ':' expecting ')'
[6,27]: mismatched input 'sun' expecting ')'
[6,30]: mismatched input ':' expecting 'end'
[34,1]: mismatched input '<EOF>' expecting 'end'

I know my syntax properly are wrong, but no matter what I do i get different errors.

These are the Items I have created in the default.items file.

DateTime         Sunrise_Time       "Sunrise [%1$tH:%1$tM]"                   { channel="astro:sun:local:rise#start" }
DateTime         Sunset_Time        "Sunset [%1$tH:%1$tM]"                    { channel="astro:sun:local:set#start" }