Sunset: Light On

Hello

in my chicken house i need “light” from 6:00 to 22:00.
i mean with “light” my lamp or daylight.

can anyone help with a rule?

before sunrise and after 6:00 -> lamp on
after sunset and before 22:00 -> lamp on
else -> lamp off

Astro Binding is installed.

you likely will find all your answers (and then some) captured here: Design Pattern: Time Of Day

thx, i will try it. my first attempt

import org.joda.time.*
var Number hour = now.getHourOfDay

rule "Innnenlicht"
when
	Item Sunrise_Event received update OFF // Dark outside
	
then
	if ((hour >= 6)  || (hour <= 22))    // after 6:00 before 22:00
	{
		Sonoff_CH1.postUpdate(ON)
	}
	
	else
		Sonoff_CH1.postUpdate(OFF)
end

could this run?

my switch:

Switch Sonoff_CH1 "Innenlicht" { mqtt="<[mosquitto:cmnd/huehnerstall/POWER1:state:default], >[mosquitto:cmnd/huehnerstall/POWER1:command:*:default]" }

or is this better?

rule "Stallbeleuchtung"
	when 
		Item Sunrise_Event changed to OFF //  its dark
	then
		var Number hour = now.getHourOfDay
		var lightState = OFF
		
		// 
		if(hour >= 6 && hour <= 10) lightState = ON

		// 
		else if(hour >= 16 && hour <= 22) lightState = ON
		
		Sonoff_CH1.sendCommand(lightState)
end

Hi Ralf,
the Event Sunrise_Event only fires once. If this event is outside of your fences (I don’t know your region, but sometimes in Middle Europe sunrise is before 6am), it won’t fire. (and it also won’t fire at sunset, which is a different channel).

You could use the “rangeEvent” properties of Astro2 binding (I guess, you have this one installed?). So do you configure it via files or in PaperUI? with “rangeEvent” you can set a “earliest” and “latest” time. So you won’t need extra IF-clauses, but the event fires and you must distinguish between sunrise and sunset.

rule "Stallbeleuchtung morgens aus"
when
	Item Sunsrise_Event triggered END // this means, if the sun is risen
then
	Sonoff_CH1.postUpdate(OFF)
end

rule "Stallbeleuchtung abends an"
when
	Item Sunset_Event triggered END // this means, if the sun is set, perhaps START would be better => or DAYLIGHT at all
then
	Sonoff_CH1.postUpdate(ON)
end

So, if your chicken house needs really light permanently, you should consider using the channel “Daylight”, so just change use this one (also with rangeEvent if you’ll need it). Daylight uses the civil dusk and should work fine with your house.

regarding your switch. Please test this one e.g. with you Switch on a sitemap, if it works. From the Syntax it should work, if you named your Sonoff 4ch “huehnerstall” in MQTT config.

thx for answer.
rules: i try to understand. is this here better?

rule "Stallbeleuchtung"
	when 
		Time cron "10 0 * ? * * *"
	then
		var Number hour = now.getHourOfDay
		
		// Sunrise_Event OFF = dark
		if(Sunrise_Event == OFF && hour >= 6 && hour <= 10) lightState = ON

		// Sunrise_Event OFF = dark
		if(Sunrise_Event == OFF && hour >= 16 && hour <= 22) lightState = ON
		
		// Sunrise_Event OFF = daylight
		if(Sunrise_Event == ON) lightState = OFF
		
		Sonoff_CH1.sendCommand(lightState)
end

i dont need light permanently. only between 6:00 and 22:00. after sunrise (daylight) the lamp can switch off.

switch:
you mean the mqtt.cfg ?

i add the button in my *.items and *.sitemap
in the basic UI i can switch my button/Sonoff

but, if i switch via sonoff-webgui, openhab get no new state

Your rule is better, but still:

Try to reduce the number your rule runs to a Minimum. That’s why I suggested “rangeEvent”, which basically does, what you do in the IF-clauses above.
Still: Your “Sunrise_Event”, if taken from Rich’s model is only Sunrise, it won’t happen at 16pm or even 22pm! :wink: You need two Events: Sunrise or Sunset. Or, you could use the channel “DAYLIGHT”, which tells you, whether there’s daylight. You should run through the docs here:

So, do you define your astro2-binding in PaperUI or via Texts? I assume in PaperUI, then open your Configuration > Things, there should be a Thing called “Astro Sun Data”, in which there are channels like Sunrise, Sunset, … You can insert some configuration there for “rangeEvent” (click on the edit-icon for let’s say “daylight start time” you can insert there earliest (here 06:00) and latest (here 10:00). So, OH2 should set the Event for daylight (if civil dawn is END) to 6:00am earliest and 10am latest (ok, this won’t happen in Western Europe - and: i won’t do any harm here, but it could be, that civil dawn is END already before 6am)
So, just check the docu and if there are any questions left, come back!

regarding your Sonoff. In mqtt.cfg you define the connection to the broker. If OH2 won’t get an state update (you have checked the events.log?), then there’s some work to do here also.
What I meant is, in the WebGUI you can define the name of the topic, your Sonoff CH4 uses. If you didn’t Change anything, it’s unlikely the topic is cmnd/huehnerstall. For the correct topic either change the config in configuration - MQTT or open the console (link on the WebGUI startpage) and have a look what happens, if you click the Hardware button on the Sonoff or click it in the WebGUI:

14:37:35 MQT: stat/SonoffDryer/POWER = ON

that is my Sonoff POW’s message, but I named the Topic “SonoffDryer”, so you’re definition of the Switch should read:

Switch Sonoff_CH1 "Innenlicht" { mqtt="<[mosquitto:stat/huehnerstall/POWER1:state:default], >[mosquitto:cmnd/huehnerstall/POWER1:command:*:default]" }

please note (sorry, I didn’t see it earlier): incoming message should read stat/huehnerstall/POWER1 instead of cmnd/huenerstall/POWER1

thx
this happens in OH Log if i switch via Sonoff GUI

2017-12-30 15:40:50.361 [vent.ItemStateChangedEvent] - Sonoff_CH1 changed from OFF to ON

2017-12-30 15:40:56.313 [vent.ItemStateChangedEvent] - Sonoff_CH1 changed from ON to OFF

but nothing happens in the basic-ui with my switch.

moquitto show this:

stat/huehnerstall/RESULT {"POWER1":"ON"}
stat/huehnerstall/POWER1 ON
stat/huehnerstall/RESULT {"POWER1":"OFF"}
stat/huehnerstall/POWER1 OFF

and Sonoff (Tasmota) show this:

15:40:50 MQTT: stat/huehnerstall/POWER1 = ON
15:40:56 MQTT: stat/huehnerstall/RESULT = {"POWER1":"OFF"}
15:40:56 MQTT: stat/huehnerstall/POWER1 = OFF
15:43:12 MQTT: stat/huehnerstall/RESULT = {"POWER1":"ON"}
15:43:12 MQTT: stat/huehnerstall/POWER1 = ON
15:43:14 MQTT: stat/huehnerstall/RESULT = {"POWER1":"OFF"}
15:43:14 MQTT: stat/huehnerstall/POWER1 = OFF

Try to update your browser manually after you see the event in the log; quite a few (including myself) have trouble with the auto-update not reliably working in OH2 (various versions); not very elegant as a solution, but maybe it helps in the search of what is working (and what not).

only the color changes (if i switch via sonoff webgui) after Reload Basic-UI

@noxx strange…unfortunately, I am out of ideas

but only in my browser, in my Android app the see the changes

There seem to be some problems with the autoupdate-feature of the GUI. If you press F5, it should reload.

This one should turn off the light right away even if it is time to turn it on because logic is:

if it is time to be on, set to ON but then the last if statement turns it OFF if it is ON right away no matter what time it is. So last IF it is ON should be in an “else if” or “else” clause.