Sun (raised / set) indicator

I started some day ago with openhab2, never used version 1 :confused:

tnx anyway

Hello Paglia

Set the Switch NightState on your sitemap.
Then you can Test, if your rule works manually (by switch it) and if the Update NightState-Rule is fireing.

Thanks everybody, the “nighstate” solution works like a charm. Sorry for my late reply.

@ThomDietrich in your example how can i insert a time offset? For example, to trigger the rule 30 minutes after sunset?

Astro 2 binding is now supporting events and offsets natively:

Wow this is great! OH2 is growing fast!
After your advice i tested a sunrise/sunset configuration, like this:

astro.items:

DateTime Sunrise_Time “Sunrise [%1$tl:%1$tM %1$tp]” {astro=“planet=sun, type=rise, property=start”}
DateTime Sunset_Time “Sunset [%1$tl:%1$tM %1$tp]” {astro=“planet=sun, type=set, property=end”}
Switch Sunrise_Event {astro=“planet=sun, type=rise, property=start, offset=0”}
Switch Sunset_Event {astro=“planet=sun, type=set, property=end, offset=0”}

astro.rules:

rule "Example Rule at sunrise"
when
Item Sunrise_Event received update ON
then
sendCommand(Switch_Landscape, OFF)
end

rule "Example Rule at sundown"
when
Item Sunset_Event received update ON
then
sendCommand(Switch_Landscape, ON)
end

no idea why, rule is not fired ans unsrise/sunset.
But if i manually trigger switches, works.

You can’t bind events to switch items anymore, you have to directly use them in your rules:

http://docs.openhab.org/addons/bindings/astro/readme.html

rule "Example Rule at sunrise"
when
       Channel 'astro:sun:home:rise#event' triggered START 
then
       sendCommand(Switch_Landscape, OFF)
end
2 Likes

uh… i suppose i should start reading release notes instead of old forums… 2017 resolution!
I’ll use “triggered”, and give a feedback.

thank you

1 Like

works! Sunrise event triggered!

What would be a good way of testing the Range daylight is happening, but not raised by the triggered START but another event e.g. if a switch is made, then check if it is daylight and then perform an action?

Do you mean something like this?

I see what you did there, but as I see astro can give you an string of either if it is spring, or winter, how come it will not just give you a string if it is daylight, night, etc…

Because it wasn’t coded that way. Most people use the events to trigger things to happen.

@gerrieg People are continuously asking about that feature. I would also be happy to see a simple channel updated with something similar to “Sunrise, Morning, Daytime, Evening, Sunset, Night”. What do you think?

added new sun channel with this PR: phase#name

4 Likes

Amazing @gerrieg, thank you!

Hi @ThomDietrich,

I know I’m digging up an old thread here but I would ask if you can cast an expert eye on my system startup rule, which I see is similar to yours…

In the Smarthome Designer I’m seeing red markers trying to declare the sunrise and sunset variables based on them being defined in my Astro.items file.

According to the log the rule worked though (i.e. 3 minutes later the updates were made)

Here is a screenshot of the code in Designer:

The issue:

This seems to show up in the openhab.log when I restart OH2:

2017-06-07 17:09:01.633 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'Astro.items'
2017-06-07 17:09:01.748 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'rrd4j.persist'
2017-06-07 17:09:01.772 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'Kodi.items'
2017-06-07 17:09:01.826 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'Light.items'
2017-06-07 17:09:01.855 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'Presence.items'
2017-06-07 17:09:01.868 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'Virtual.items'
2017-06-07 17:09:01.903 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'Weather.items'
2017-06-07 17:09:01.977 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'Zoneminder.items'
2017-06-07 17:09:04.552 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'Presence.rules'
2017-06-07 17:09:05.038 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'Lighting.rules'
2017-06-07 17:09:05.487 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'DayNight.rules'
2017-06-07 17:09:06.365 [INFO ] [el.core.internal.ModelRepositoryImpl] - Validation issues found in configuration model 'Startup.rules', using it anyway:
Cannot reference the field 'Sunrise_Start' before it is defined
Cannot reference the field 'Sunset_Start' before it is defined
2017-06-07 17:09:06.367 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'Startup.rules'
2017-06-07 17:09:07.081 [INFO ] [el.core.internal.ModelRepositoryImpl] - Validation issues found in configuration model 'Windows.sitemap', using it anyway:
Frames must not contain other frames
2017-06-07 17:09:07.083 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'Windows.sitemap'
2017-06-07 17:09:09.108 [INFO ] [.dashboard.internal.DashboardService] - Started dashboard at /start
2017-06-07 17:09:09.263 [INFO ] [basic.internal.servlet.WebAppServlet] - Started Basic UI at /basicui/app
2017-06-07 17:09:09.295 [INFO ] [arthome.ui.paper.internal.PaperUIApp] - Started Paper UI at /paperui
2017-06-07 17:09:09.327 [INFO ] [panel.internal.HABPanelDashboardTile] - Started HABPanel at /habpanel
2017-06-07 17:09:12.407 [INFO ] [ding.astro.handler.AstroThingHandler] - Scheduled astro job-daily-sun at midnight for thing astro:sun:f3c3056b
2017-06-07 17:09:15.359 [WARN ] [me.internal.engine.RuleContextHelper] - Variable 'sunrise' on rule file 'Startup.rules' cannot be initialized with value 'org.eclipse.xtext.xbase.impl.XConstructorCallImplCustom@1c8d8b2 (invalidFeatureIssueCode: null, validFeature: false, explicitConstructorCall: true, anonymousClassConstructorCall: false)': org.eclipse.smarthome.core.library.types.DateTimeType
2017-06-07 17:09:15.361 [WARN ] [me.internal.engine.RuleContextHelper] - Variable 'sunset' on rule file 'Startup.rules' cannot be initialized with value 'org.eclipse.xtext.xbase.impl.XConstructorCallImplCustom@1a9d429 (invalidFeatureIssueCode: null, validFeature: false, explicitConstructorCall: true, anonymousClassConstructorCall: false)': org.eclipse.smarthome.core.library.types.DateTimeType
2017-06-07 17:09:15.583 [INFO ] [.astro.internal.job.AbstractDailyJob] - Scheduled astro event-jobs for thing astro:sun:f3c3056b
2017-06-07 17:11:19.880 [INFO ] [.model.script.Craig presence updated] - Craig is AWAY
2017-06-07 17:12:10.206 [INFO ] [.model.script.Craig presence updated] - Craig is HOME
2017-06-07 17:12:15.457 [INFO ] [home.model.script.NightState updated] - --> NightState set to OFF
2017-06-07 17:12:15.469 [INFO ] [.model.script.LateNightState updated] - --> LateNightState set to OFF


I’ve tried for ages to find why the designer is unhappy.

Here is the code…

val sunrise = new DateTime((Sunrise_Start.state as DateTimeType).calendar.timeInMillis)
val sunset = new DateTime((Sunset_Start.state as DateTimeType).calendar.timeInMillis)
val latenight = now.withTimeAtStartOfDay.plusHours(22).millis // 10 PM

rule "Switch NightState and lights on / off depending on night or day"
when
    System started
then
	createTimer(now.plusSeconds(180)) [ |
// Switch NightState to on if it is dark otherwise switch it to off
    	if(now.isAfter(sunset) && now.isBefore(sunrise)){
		postUpdate(NightState, ON)
		logInfo("NightState updated", "--> NightState set to " + NightState.state)
    	}
    	else {
        	postUpdate(NightState, OFF)
			logInfo("NightState updated", "--> NightState set to " + NightState.state)
    	}

// Switch LateNightState on if it is after 22h00 and before sunrise
  		if(now.isAfter(latenight) && now.isBefore(sunrise)){
     	postUpdate(LateNightState, ON)
      	logInfo("LateNightState updated", "--> LateNightState set to " + LateNightState.state)
    	}
    	else {
      	postUpdate(LateNightState, OFF)
      	logInfo("LateNightState updated", "--> LateNightState set to " + LateNightState.state)
    	}
    ]
end

The items definition:

DateTime	Sunrise_Start	"Sunrise Start [%1$tH:%1$tM]"		(gAstro)		{channel="astro:sun:f3c3056b:rise#start"}
DateTime	Sunset_Start	"Sunset Start [%1$tH:%1$tM]"		(gAstro)		{channel="astro:sun:f3c3056b:set#start"}

Also it seems unhappy with my ‘IF’ statements, here is the issue:

Am I anywhere close to getting it right?

many thanks in advance,

Craig

Hey Craig,
is there any special reason to have these three lines above all? Could you try moving them inside the rule?

@ThomDietrich: not only inside the rule, but inside the lambda right after:

createTimer(now.plusSeconds(180)) [ |
val sunrise = new DateTime((Sunrise_Start.state as DateTimeType).calendar.timeInMillis)
val sunset = new DateTime((Sunset_Start.state as DateTimeType).calendar.timeInMillis)
val latenight = now.withTimeAtStartOfDay.plusHours(22).millis // 10 PM
1 Like

You might like to think about what you are trying to achieve with variables ‘sunrise’ etc.
Like the Designer warning says, you cannot reference Items outside of rules.
You have also defined them as val meaning they can never be altered - i.e. could never keep track of sunrise tomorrow.
The definition with now in it is also going to go wrong for you eventually - ‘now’ would be when the rules file is loaded, and again is one-time-only, would never update for tomorrow.

1 Like