Light Day night Controll

Hi All.
Hope you Guys are covid safe.

i have inbarked on a new yuarny .
i want to create my rules in openhab. use to run some on node red. do to the lack of knoladge on how to do rules. in text form.

so what i want to do is:
i have a porch light that i want to have turn on on sun set and off at sun rise.

my Rule :

rule "Sun set Light"
when
    Channel 'astro:sun:home:set#event' triggered START
then
    item Kitchen_Light.sendCommand(ON)
end

rule "Sun Rise Light"
when
    Channel 'astro:sun:home:rise#event' triggered START
then
    item Kitchen_Light.sendCommand(OFF)
end

But there is where i get stuck.
it is a Sonnoff T1 3 Gang.
the one is the porch light.

So my problem comes in here:

1)i want to still be able to turn it on and off by the switch
(if light is turned on by the rule,i want to be able to turn it off by the switch or via openhab and it stays off.or vice versa.) on node red with big timer if i turn it off at the switch it just turns back on.

What must i do to get it to work this way ?
Kind regards

Bad syntax.
If you look in your openhab.log you will probably find an error report from when it tried to read your rules file after it was last edited.

You just want

then
   Kitchen_Light.sendCommand(ON)

The rules as shown will turn on the light at sunset and turn it off at dawn.

Anything else you do to it in between those moments will not be affected by these rules.

So if i turn it off by the switch it will stay off ?

The rules only do something at the instant they are triggered. They don’t do anything in between times.
If you change the light in between times, the rules still don’t do anything.

You could always try it out.

Thank you. i am uploading my rule now.

For some reason it seems like my rule does not get triggered? Is there anything i need to add to the thing file or something els? :see_no_evil::sob:

Obviously, you would need to have the Astro binding installed.
You would need an Astro sun type Thing called “home”, because that is what you specified in your rule.
You would need to wait for sunup or sunrise for anything to happen. You should see the event in your events.log
You would need to have a valid rules file loaded, your openhab.log will tell you if that happened.
If your rules are still as simple as the broken versions you showed before, you should be able too see if they act by looking in your events.log for commands issued.

Ok So i re thougth the whole controll and i implemanted DP time of Day.
Got that working for me. now i whant to use that to turn the light on and off.

rule "Stoep Light On"
when
    Item vTimeOfDay == Evening
then
Kitchen_Light_Stoep.sendCommand(ON)
end
    
rule "Stoep Light Off"
when
    Item vTimeOfDay == Bed
then
   Kitchen_Light_Stoep.sendCommand(OFF)
end

Does the syntex look right ?
then my second part is i want to have it look to my alarm also.
if my alarm is disarmed or armed-Away or Armed-Stay and it is Evening it must turn the light on
when armed stay and tod is Bed then turn off.
if armed away and tod changes to bed the light must stay on. only turn off when tod changes to morning.

this is what i think.

rule "Stoep Light On"
when
    Item vTimeOfDay == Evening &
    Item Alarm_Status == Disarmed  or 
    Item Alarm_Status == Armed-Away or 
    Item Alarm_Status == Armed-Stay
    
then
Kitchen_Light_Stoep.sendCommand(ON)
end
    
rule "Stoep Light Off"
when
    Item vTimeOfDay == Bed
then
Kitchen_Light_Stoep.sendCommand(OFF)
end

Well, no.
Rules are triggered by events

That’s a steady state.
You’d want to trigger from a change, perhaps?

when
   Item vTimeOfDay changed to "Evening"

Something Like This ?

rule "Stoep Light On"
when
    Item vTimeOfDay changed to "Evening" &
    Item Alarm_Status == Disarmed  or 
    Item Alarm_Status == Armed-Away or 
    Item Alarm_Status == Armed-Stay
    
then
Kitchen_Light_Stoep.sendCommand(ON)
end
    
rule "Stoep Light Off"
when
    Item vTimeOfDay changed to "Bed" & 
    Item Alarm_Status == Armed-Stay
then
Kitchen_Light_Stoep.sendCommand(OFF)
end

rule "Stoep Light Off Morning"
when
    Item vTimeOfDay changed to "Morning" & 
    Item Alarm_Status == Armed-Away
then
Kitchen_Light_Stoep.sendCommand(OFF)
end

Nope. You’ve seen no examples of this, and cannot just make it up.

All that can go in the “when” triggers section is events.
Think through the implications - if there were such a thing as a “AND” trigger, you would need two events to occur in the exact same microsecond.

So, separate out the events that you want to trigger your rule, from the state conditions that you want to be true.

Example -

when
    Item vTimeOfDay changed to "Bed" 
then
    if ( Alarm_Status.state == "Armed-Stay" ) {
        // do stuff
    }

I miss understood the rule system.
i am currently moving my rules from node red as it is a lot more work to get a simple rule to work on it. i will need to sharpen up my rule writing. thank you for pointing out my problem.
i understood it like i would write plc code.
as example. item 1 and item 2 or item 3 or item 4 (as plc code.)
but with Openhab it is a triger and then conditions.
item 1 Changes.
if condition 1 is met. then do this and so on.
will go through the rule documents again. with this view.

Thanks again.

1 Like

openHAB is events driven, it’s a key understanding.

Ok Now. if i write it this way .it seems i am still wrong somewhere.

My Rule

ule "Stoep Light On"
when
    Item vTimeOfDay changed to "Evening"
    
then
 if (Item Alarm_Status == "Disarmed") {
  Kitchen_Light_Stoep.sendCommand(ON) }
 if ( Item Alarm_Status == "Armed-Away" ) {
  Kitchen_Light_Stoep.sendCommand(ON) }
 if ( Item Alarm_Status == "Armed-Stay" ) {
  Kitchen_Light_Stoep.sendCommand(ON) }

end
    
rule "Stoep Light Off"
when
    Item vTimeOfDay changed to "Bed"
   
then
 if ( Item Alarm_Status == "Armed-Stay" ) {
  Kitchen_Light_Stoep.sendCommand(OFF) }
 if ( Item Alarm_Status == "Disarmed" ) {
  sendBroadcastNotification("Stoep Light Still On" + " - " + now.toString("MM/dd HH:mm ")) }

end

rule "Stoep Light Off Armed"
when
    Item vTimeOfDay changed to "Morning"
then
 if ( Item Alarm_Status == "Armed-Away" ) {
  Kitchen_Light_Stoep.sendCommand(OFF) }
end


In my Log:

2020-05-16 16:12:26.448 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'Outlight.rules' has errors, therefore ignoring it: [7,6]: no viable alternative at input 'Item'

[7,11]: missing ')' at 'Alarm_Status'

[7,43]: mismatched input ')' expecting 'end'

[21,7]: no viable alternative at input 'Item'

[21,12]: missing ')' at 'Alarm_Status'

[21,47]: mismatched input ')' expecting 'end'

[32,7]: no viable alternative at input 'Item'

[32,12]: missing ')' at 'Alarm_Status'

[32,47]: mismatched input ')' expecting 'end'

Item Alarm:

string  Alarm_Status    "Alarm Status"                              {channel="mqtt:topic:mosquitto:Alarm_Stat:alarm_status"}

The alarm Statuses comes from node red on a raspberry pi installed over my alarm.

Bad syntax, for the “then” section of the rule.

if (Alarm_Status == …

But that still wouldn’t do what you want. The whole Item object has many properties; label, type, etc.
I expect you are interested in its state.

if (Alarm_Status.state == "Disarmed") {

Have a look at the docs, have a look at examples. It’ll be quicker than guessing syntax.

1 Like

Thank you.
i did change it to state. right after i send the reply.
it seems to be working now. tonight i am studing the rule document . i want to be able to get all my rules right first time.

Thank you for your help.

1 Like

Can it be done like this ?

rule "Stoep Light Off"
when
    Item vTimeOfDay changed to "BED"
   
then
 if ( Alarm_Status.state == "Armed-Stay" ) {
  Kitchen_Light_Stoep.sendCommand(OFF) }
 if ( Alarm_Status.state == "Disarmed" ) { if ( Kitchen_Light_Stoep.state == "ON" ){
  sendBroadcastNotification("Stoep Light Still On" + " - " + now.toString("MM/dd HH:mm ")) }
 }
end

Looks reasonable, try it.

Note that if Kitchen_Light_Stoep is a Switch type Item, then it has states like ON, not string states like “ON”.

Thanks. will test it.