was playing around with “close to my first” rule, tried to think of something i could actually use…
i came up with the following but have lot of questions.
My idea is: i need a light to turn on if i am not seen at home between 17:00 and 18:00, but it should only work when my sunrise event has triggered (so it starts to get dark outside)
i dont have any other motion or light sensors yet.
rule "Not home between 17u - 18u - lights on"
when
(now.getHours > 17 || now.getHours <=18)
Channel "astro:sun:home:set#event" triggered START
Channel "network:pingdevice:192_168_1_3:lastseen")
then
Livingwandlicht.sendCommand(ON)
end
Can i just add multiple “whens” like this example underneath eachother, will that work?
Is my statement to get this working between 17u and 18u correct?
I need this rule to only work if the “last seen” is NOT between 17-18u and i can not seem to figure this one out.
This needs to be an if statement used in the then section. When writing a rule think, when something ( you can have more than one when, use the or like below) happens, then do this.
rule "Not home between 17u - 18u - lights on"
when
Channel "astro:sun:home:set#event" triggered START or
Channel "network:pingdevice:192_168_1_3:lastseen"
then
if((now.getHourOfDay() >= 8) || (now.getHourOfDay() <= 20)){
Livingwandlicht.sendCommand(ON)
}
end
In the then section you can use an if statement to evaluate a condition such as the time like above.
Here’s a link that I keep in my book marks to help with rules and using if, else, if else and more.https://www.w3schools.com/js/js_if_else.asp Also check the other selection offered, on the left side, from this link.
wow, guys, this has become a very intresting post with a lot of new information for me.
I will try to understand this and capture it all.
@H102
When using multiple “whens” you advise the “or”, can i use “and” , sounds more logic to what i need, or not?
just thinking out loud.
@Ctrl-G and @H102
I can not follow the discussion, i do not understand when it would return as a true or false.
Or to say make this work between 17:00 and 18:00.
i think i do get this:
and this one:
so with all this info i think this is what i make of it:
rule "not home between 17-18 - lights on"
when
Channel "astro:sun:home:set#event" triggered START
then
if((now.getHourOfDay() == 17) && Bjornphonelastseen.state == OFF){
Livingwandlicht.sendCommand(ON)
}
end
Please correct me if this does not make any sense
Is there a way to check what the output was for last seen state and use this?
I think i need a:
“ok last seen was between >18u <17u” instead of looking for a on/off
or a:
“ok last seen was between >17- <18” i do not need to trigger te rule
My phone will go to sleep sooner or later, meaning that if i would be home at 17u and the astro triggers at 17:30 it would still turn on the light while beeing home.
The condition will be true when everything in the statement is true. If there are multiple items to check then all of them must be true. After evaluating the statement everything between the { } will happen if true, if false the rule ends or moves to the next statement.
I think there are several ways you can do this, here is one item I use for indicating the time last changed.
To help with using all the statement condition such as &&, ||, != see the link I posted above and work some of their examples. That site was, and still is, a big help for me.
I thought this would only work if the time was = 17 but not work when 1701 or greater. Guess I shouldn’t think so much, but it’s always nice to learn something new.
@Peacemaker thought I would share my things, items and rule for iPhone presents. It works by pinging the device and if away the rule starts a timer (20 minuets) that will indicate I’m away after the timer expires. The 20 minuets can be tweaked to suit your need but I use it to make sure that I’m actually away, not just out of range for a few minuets, before turning something on or off.
Note, the Expire binding is needed for this to work.
rule "start gPresent on system start"
when
System started
then
Present.sendCommand(OFF) // assume no one is home
end
rule "gPresent updated, at least one change of state"
when
Item gPresent received update
then
// someone came home
if(gPresent.state == ON && Present.state != ON) {
Present_Timer.postUpdate(OFF) // cancel the timer if necessary
Present.sendCommand(ON)
}
// no one is home and timer is not yet ticking (otherwise endless loop)
else if(gPresent.state == OFF && Present.state != OFF && Present_Timer.state != ON) {
Present_Timer.sendCommand(ON) // start the timer
}
end
rule "Present_Timer expired"
when
Item Present_Timer received command OFF
then
Present.sendCommand(OFF)
end
Hope it helps with ideas and different ways to use OH rules.
You seem to be well supported by the others here (thanks guys!) but I wanted to make sure this question got answered.
OH Rules are triggered by events. Events are instantaneous. Consequently no two events will ever occur at the same time. So it makes no sense to have an and in the when clause because no two or more of those events will ever happen at the same time.
Testing to see if it is currently between 17:00 and 18:00 is testing state. To test for state you must do so using an if/else or switch statement in the then part of the Rule.
Hey rlkoshak, thx for your reply, this makes so much sense when i read this
i can think of some case where 2 triggers is needed before something happends though.
Though nothing that cant be solved by adding the “if” in the “then” option.
wow, flattered
Well… you know what i think of you … luckely there are such nice people that are so willingly in helping other people!
Its because of people like you that a community is great!