[SOLVED] Rule question with multiple "whens" + "last seen option"

Hey Guys,

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
  1. Can i just add multiple “whens” like this example underneath eachother, will that work? :slight_smile:
  2. Is my statement to get this working between 17u and 18u correct?
  3. 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.

Any help would be appreciated.
Thx.

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.

1 Like

Shouldn’t that be:

if ! (now.getHours > 17 && now.getHours <=18) {

or maybe

if ( ! (now.getHours > 17 && now.getHours <=18) ) {

if he wants it to be true when NOT between 17 & 18 ?

It looks to be that the suggested ‘if’ would always be true, as it is ALWAYS > 17 or <= 18.

Also, not sure if the NOT construct “!” is usable here or in this way or not. Kind of guessing.

1 Like

@Ctrl-G No, it should be more like

 if((now.getHourOfDay() >= 17) || (now.getHourOfDay() <= 18)){

The && meas AND, the || mean OR, the op can choose which fits their needs.

Edit I see what you’r asking about the not.

Maybe the best solution is to create an item for the “lastseen” network thing and use it in the if statement like:

if((now.getHourOfDay() >= 8) && (now.getHourOfDay() <= 20) && Lastseen.state == OFF){

Yes I see the point about, time between 8 and 20 vs 8 or 20

1 Like

Right! So what hour(s) / minutes of the day would your if statement …

if((now.getHourOfDay() >= 17) || (now.getHourOfDay() <= 18)){

… evaluate to false?

1 Like

When you change the time in the statement to

if((now.getHourOfDay() >= 18) || (now.getHourOfDay() <= 17)){

results in a true statement but needed for adjusting to suit a particular need

1 Like

Yes - Agreed!

If you want between 17:00 and 18:00 then the statement needs to be:

if(now.getHourOfDay() == 17) {

Simples!!!

2 Likes

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 :slight_smile:

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.

DateTime Garage_update "Last change [%1$tm/%1$td %1$tH:%1$tM]"

In the rule you may want to change the item to a val then compare the val with other items in the if statement e.g.

val time = Bjornphonelastseen.state as Number

This can be used if the item is a number like the DateTime item above.

Here is a nice tutorial about date and time conversion you may find interesting. https://community.openhab.org/t/datetime-conversion/54266?u=h102

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.:grinning:

@vzorglub I wasn’t aware you could use

for the entire hour.

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,:roll_eyes: but it’s always nice to learn something new.:grinning:

Thanks

1 Like

@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.

Things

Thing network:pingdevice:SiPhone [ hostname="10.0.1.27", uses_ios_wakeup=1, uses_arp_pings=1, retry=30, timeout=15000, refreshInterval=60000 ]

Items

Group:Switch:AND(OFF,ON) gPresent <present>
Switch Present "Phone is home" <present>
Switch Present_Timer { expire="20m, command=OFF" }
Switch MyDevice <network> (gPresent) { channel="network:pingdevice:SiPhone:online" }

Rule

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.:grinning:

1 Like

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.

1 Like

Hey rlkoshak, thx for your reply, this makes so much sense when i read this :slight_smile:
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.

Thx!

H102, thx for the examples and support, again! :slight_smile:
i think i got some testing and playing around to do :slight_smile:

1 Like

vzorglub, thx for you assistance, again!

Its always a pleasure to help someone like @Peacemaker that reads the documentation and ask good questions, I usually learn a few things myself.:grinning:

Maybe one day I’ll be able to answer questions as clearly and complete as you often do.:crossed_fingers:

Thanks to all

wow, flattered :flushed:
Well… you know what i think of you :slight_smile: … 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!

1 Like

i was just about to add a question, but i’ll make a new post, think it will be great as question + answer for noobies like me :slight_smile: