Timers for on/off delay

I have tried the network presence but on the current release of android (and I think iphone) when locked, and doing the deep sleep cycle it doesnt respond to ping request.

I know this can be changed, but to be honest, I want to not have to worry about that, so I was thinking what would be better is if the presence is detected, start a timer for Xminutes. if the timer runs out without the presence pinging again, go off, otherwise if it pings reset the timer.

Not sure how to implement that though so Im just after some advice there.

This will also come in handy for things like off delay for pumps, lights, etc.

Thanks

RULE FILE for a timer that is triggered/retriggered when a specific item receives an update.
This works for all the use cases you describe above:

In this case “var Integer pantry_timeout = 1” defines a timer with a duration of 1 minute.
Each time " MotionSensor_Pantry_Motion" is updated the timer is triggered/retriggered for the duration of “var Integer pantry_timeout = 1”.

For presence purposes like you described you would replace " MotionSensor_Pantry_Motion" for a switch item that represents your phone.

But please look around on the forum a bit, there are several better ways for presence detections using phone-openhab. (expire binding, Unifi binding, dd-wrt trick)

var Timer pantry_timer = null
var Integer pantry_timeout = 1

rule "Pantry light motion"
when
	Item MotionSensor_Pantry_Motion received update
then    
	if (MotionSensor_Pantry_Motion.state == ON && Night.state == ON) {   
  	  if (pantry_timer != null) 
    {            
        pantry_timer.reschedule(now.plusMinutes(pantry_timeout))
        logInfo("Lights","Pantry light timer rescheduled for " + pantry_timeout + " minutes")
    }
    else
    {            
        sendCommand(Switch_Pantry_1, ON)
        logInfo("Lights","Pantry light timer created for " + pantry_timeout + " minutes")
        RulePantryLightMotionTS.postUpdate( new DateTimeType() )
        pantry_timer = createTimer(now.plusMinutes(pantry_timeout)) 
        [|        
            if (MotionSensor_Pantry_Motion.state ==  ON)   
            {
                pantry_timer.reschedule(now.plusMinutes(pantry_timeout))
                logInfo("Lights","Pantry timer triggered, but rescheduled again for " + pantry_timeout + " minutes")                    
            } 
            else 
            {            
            logInfo("Lights","Pantry light timer expired")
                sendCommand(Switch_Pantry_1,OFF)
                pantry_timer = null
            }
        ]
    }
}
end

Also have a look at the expire binding.

Thanks, I was just looking up the createTimer function there.

I have one question, ‘received update’ on the item. what exactly does that mean.

I have a few MQTT items and I want to send a status into the item. It would be a button press to be exact. But if that button only ever sends the same signal eg ‘ON’ or ‘button pressed’, does that mean the Item has been updated, even if it still has the same state as it did previously? I wouldnt send a signal when the button is pressed, and then another when it is released.

Ill check out those other bindings too. Thanks

Yes that is exactly how “received update” works.
The item is updated, but could have the same value.

If you are on OH 2.2 and configure ARPING it will detect your phone even while it is asleep.

This is already basically how the Network binding works.

if you want to use the state of your Network binding to set the state in some other item based on a timer see the following.

Im really struggling with openhab. Everytime I think Im going add something that seems to be well documented and look simple I have nothing but headache.

Ive followed your link to the presence detection, copied and pasted it but nothing changes state.

I have tried to check the arping is ok and the binding config is ok but really dont know where/what the problem could be. I can use it from the terminal with no problem, I found a post about the command the network binding uses and have no problems there.

There are no issues in the log but also no logs about the period ping requests. I think my log is only showing warnings and info though so I guess there is another config if I want it to show any more eg the network binding info.

thing file (ive also tried this with all the other parameters manually added but it made no difference)

Thing network:device:StevensPhone "Stevens Phone" [hostname="192.168.0.25"]
Thing network:device:StevensLaptop "Stevens Laptop" [hostname="192.168.0.11"]
Thing network:device:KellysPhone "Kellys Phone" [hostname="192.168.0.15"]

item file

Switch Present "Someone is in" <present>
Group:Switch:AND(OFF,ON) gPresent <present>
Switch Present_Timer {expire="15m,command=OFF"}


Switch StevensPhone_presence (gPresent) {channel="network:pingdevice:StevensPhone:online"}
Switch StevensLaptop_presence (gPresent) {channel="network:pingdevice:StevensLaptop:online"}
Switch KellysPhone_presence (gPresent) {channel="network:pingdevice:KellysPhone:online"}

rules

rule "System started, Presence"
when
    System started
then
    Present.sendCommand(OFF) // assume no one is home on startup
end

rule "gPresent updated, at least one sensor changed 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
    else if(gPresent.state == OFF and Present.state != OFF){
        Present_Timer.sendCommand(ON) // start the timer
    }
end

rule "Present_Timer expired"
when
    Item Present_Timer received command OFF
then
    Present.sendCommand(OFF)
end

sitemap file (ive tried the presence items with the icon switch just to see if it made a difference and even manually switching them the presence timer switch doesnt switch on)

sitemap home label="Home Page"{
        Frame label="Temperatures"{
                Text item=sensor_1 icon="temperature"
                Text item=sensor_2 icon="temperature"
                Text item=sensor_3 icon="temperature"
                Text item=sensor_4 icon="temperature"
                Text item=sensor_5 icon="temperature"
        }
        Frame label="Setpoints"{
                Setpoint item=Heating_Setpoint icon="heating" step=0.1 minValue=16 maxValue=24
                Setpoint item=HWS_Setpoint icon="heating" step=0.5 minValue=40 maxValue=60
                Setpoint item=OAT_HoldOff icon="heating" step=0.5 minValue=10 maxValue=26
        }
        Frame label="MQTT buttons"{
                Text item=heating_ovr_status icon="switch"
                Text item=hws_ovr_status icon="switch"
        }
        Frame label="Who is in?"{
                Switch item=Present_Timer icon="network"
                Switch item=StevensPhone_presence icon="network"
                Switch item=StevensLaptop_presence icon="network"
                Switch item=KellysPhone_presence icon="network"
        }
}

network config file (ive tried allowSystemPings true and false, neither make a difference)

allowSystemPings=false
arpPingToolPath=arping

I’m on my phone so I can’t do a full recovery but your Thing definition do not look right. Have you looked at the Network binding readme? I don’t think it lets you apply a label like that, though I could be won’t about that.

This is called cargo cult programming. You should try to understand what the code does, not just blindly copy it. If you have questions about how it works please ask. You set yourself up for nothing but frustration if you don’t try to understand what the code you are copying from first. And it is REALLY hard for us to help in this situation too because you can’t give us the information we need to help and we spend hours playing 20 questions just to figure out what the problem isn’t.

My point was that I copied it, rather than typed it out as to not make mistakes that could easily be avoided. I do try to understand the code, The only thing I dont really get at the minute is some of the group set up, but as every example ive seen is the same I guessed it must be right.

I will go through it again and check the thing definition as you have said but again, I used other examples so…

But as with the exec binding issues, Ive written the arp ping as a program in python and publish the device presence as MQTT. That works fine. Im considering just doing it this way from now on. At least I have something to fall back on if the binding still doesnt get me anywhere.

I removed the labels and put them in the items instead.

I found an error in the rule ‘and’ instead of ‘&&’

I changed ‘pingdevice’ to ‘device’ as I see multiple examples of both so I assume it’s a version thing. I believe I installed v2 of openhab at the time. I know there is v2.2 available now so that may be a thing but I installed the network binding from the paper ui so assume it would’ve chosen the latest compatible version.

Anyway it’s working now, sort of. The present state seems be working backwards, the timer, I haven’t worked out what that’s doing yet. And one of the devices shows ‘on’ while the icon was off and another seemed to work as expected. At least while I looked on the site map. Who knows. I’ll do a big more digging but at least it’s a bit of progress.

If you are on the latest 2.2 version you need to make your Thing syntax match that described in the Network Binding’s README.

If you are ever in doubt, there is a “Switch Article Version” in the upper right corner. You can change that to match the version of OH you are running and get the appropriate documentation for your version.

All the bindings and the core of OH are released as a unit. Unless you manually download and install a jar file, the version of the binding you have will always be the same as the version of OH you are running.

In version 2.2, “device” is no longer a supported Thing type. Only “pingdevice” and “servicedevice” are supported.

The issues with the toggle not matching the state may be related to a BasicUI bug I’ve seen reported recently where, once again, BasicUI doesn’t always refresh appropriately.

One comment is unless you plan on manually toggling these Switches from your Sitemap, you should use Text to display them, not Switch. That will make them “read only” on the sitemap and get rid of the toggle icon.

I did wonder how you got to the right version of the documents.

I have 2.0 I believe so maybe at the weekend Ill try 2.2.

I managed to get the presence detection working now but I think the locked iphone is not responding, I havent really looked into yet though. I have the allow system thing so assuming it is using arp but if Im changing the version, theres more info seemingly related to v2.2 so Ill try more with that version.

The switch buttons were there for testing, like most things on my sitemap, everything is a mess until I work out what I want and how I want it.

Is there a way to jump between sitemaps? Officially I mean? I have found that if you break a sitemap it give you hyperlinks for the remaining sitemaps. I suppose that works… in way.

That is a REALLY important detail. 2.0 is really old. You will notice the docs don’t even go back that far. There have been so many changes and improvements since then I honestly can’t begin to offer further suggestions.

The arping was added in 2.2. It doesn’t exist in 2.0 or 2.1.

Not really. None I know of anyway. I don’t use sitemaps much so haven’t tried to do much along those lines.

To be honest I didnt download openhab2 that long ago and until the other day, didnt even realise there were 2 more versions released since then. The guide I used must have been out of date.

I like the look of the habpanel, it seems much closer to what I want to see so Ill look into that in the future and use sitemaps as a quick go to for now.

That is one of the frustrating things about all the third party tutorials out there. They all link to specific versions of OH. I wonder how many people are still installing OH 1.7 because of the way some of these tutorials were written.

So Im all up to date now (i think). Is there a terminal command I can type that will tell me the current version? I did the showpgk as described in the manuals but it shows that I have 2.0 2.1 and 2.2 in the cache. I guess that means they are there ready to be installed if I want? But I assume after updating it would have installed the latest as I didnt specify.

Anyway…

After that I couldnt get the cron triggers to work, I found various posts about them only reading the first trigger in each rules so I separated them out but still receive an error that the format is incorrect.

I have changed some and got rid of the error but I dont know what the purpose of the ? is in it.

Time cron "0 30 4 * * *"

changed to

Time cron "0 30 4 * * ?"

I dont know the difference here between * and ? and why the first one gave an error but the second didnt.

But how to I resolve this??

Time cron "0 30 4 * * 6/7"


pi@raspberrypi:/etc/openhab2/rules $ tail /var/log/openhab2/openhab.log
2018-01-11 16:35:01.935 [ERROR] [e.internal.engine.RuleTriggerManager] - Cannot create timer for rule 'Heating Occupancy ON1': CronExpression '0 0 5 * * *' is invalid.
2018-01-11 16:35:01.942 [ERROR] [e.internal.engine.RuleTriggerManager] - Cannot create timer for rule 'Heating Occupancy ON2': CronExpression '0 0 15 * * *' is invalid.
2018-01-11 16:35:01.949 [ERROR] [e.internal.engine.RuleTriggerManager] - Cannot create timer for rule 'Heating Occupancy ON3': CronExpression '0 0 9 * * 6/7' is invalid.
2018-01-11 16:35:01.955 [ERROR] [e.internal.engine.RuleTriggerManager] - Cannot create timer for rule 'Heating Occupancy ON4': CronExpression '0 0 12 * * 6/7' is invalid.
2018-01-11 16:35:01.962 [ERROR] [e.internal.engine.RuleTriggerManager] - Cannot create timer for rule 'Heating Occupancy ON5': CronExpression '0 0 15 * * 6/7' is invalid.
2018-01-11 16:35:01.968 [ERROR] [e.internal.engine.RuleTriggerManager] - Cannot create timer for rule 'Heating Occupancy OFF1': CronExpression '0 30 7 * * *' is invalid.
2018-01-11 16:35:01.975 [ERROR] [e.internal.engine.RuleTriggerManager] - Cannot create timer for rule 'Heating Occupancy OFF2': CronExpression '0 30 21 * * *' is invalid.
2018-01-11 16:35:01.983 [ERROR] [e.internal.engine.RuleTriggerManager] - Cannot create timer for rule 'Heating Occupancy OFF3': CronExpression '0 0 10 * * 6/7' is invalid.
2018-01-11 16:35:01.988 [ERROR] [e.internal.engine.RuleTriggerManager] - Cannot create timer for rule 'Heating Occupancy OFF4': CronExpression '0 0 13 * * 6/7' is invalid.
2018-01-11 16:35:01.994 [ERROR] [e.internal.engine.RuleTriggerManager] - Cannot create timer for rule 'Heating Occupancy OFF5': CronExpression '0 0 16 * * 6/7' is invalid.

Easiest way is to bring up your dashboard (http://ohserver:8080) and look at the bottom of the page. It will show the version there.

Alternatives are to log into the karaf console or look in /var/lib/openhab2/etc/version.properties.

If you do not see the version at the bottom of your dashboard, you are indeed running an old version of OH, not the latest 2.2 release.

The easiest thing is to go to an online cron builder.

https://www.freeformatter.com/cron-expression-generator-quartz.html

So I’ve realised that I put 6-7 in the year slot rather than the day. That would probably explain that one.

But as for the ?

The site suggests using ? For every day but * is used for all others eg every month, ever year, etc. I don’t get that but I’ll give it a go.

From the Quartz documentation:
http://www.quartz-scheduler.org/documentation/quartz-2.x/tutorials/crontrigger.html

? (“no specific value”) - useful when you need to specify something in one of the two fields in which the character is allowed, but not the other. For example, if I want my trigger to fire on a particular day of the month (say, the 10th), but don’t care what day of the week that happens to be, I would put “10” in the day-of-month field, and “?” in the day-of-week field. See the examples below for clarification.

My cron times dont seem to do anything.

Items here

//Group to store any occupancy state as common switch
Group:Switch:OR(ON,OFF) gOccupancy <present>
Group:Switch:OR(ON,OFF) gOccupancyHeating <present>
Group:Switch:OR(ON,OFF) gOccupancyHWS <present>

//Switches to store states of any time schedule
Switch occupancy (gOccupancy)
Switch occupancyHeating (gOccupancy,gOccupancyHeating)
Switch occupancyHWS (gOccupancy,gOccupancyHWS)

//text item to store mode
Number occupancyMode "Mode [MAP(mode.map):%s]"

//for heating
Switch occProxy1 (gOccupancyHeating)
Switch occProxy2 (gOccupancyHeating)
Switch occProxy3 (gOccupancyHeating)
Switch occProxy4 (gOccupancyHeating)
Switch occProxy5 (gOccupancyHeating)
Switch occProxy6 (gOccupancyHeating)
Switch occProxy7 (gOccupancyHeating)
Switch occProxy8 (gOccupancyHeating)
Switch occProxy9 (gOccupancyHeating)


//for hws
Switch occProxy10 (gOccupancyHWS)
Switch occProxy20 (gOccupancyHWS)
Switch occProxy30 (gOccupancyHWS)
Switch occProxy40 (gOccupancyHWS)

rules here

rule "System started, occupancy"
when
        System started
then
        occupancyHeating.sendCommand(OFF)
end

//Heating schedules

rule "Heating Occupancy ON1"
when
        Time cron "0 0 5 ? * *"
then
        occProxy1.sendCommand(ON)
end

rule "Heating Occupancy ON2"
when
        Time cron "0 0 15 ? * *"
then
        occProxy2.sendCommand(ON)
end

rule "Heating Occupancy ON3"
when
        Time cron "0 0 9 6/7 * ?"
then
        occProxy3.sendCommand(ON)
end

rule "Heating Occupancy ON4"
when
        Time cron "0 0 12 6/7 * ?"
then
        occProxy4.sendCommand(ON)
end

rule "Heating Occupancy ON5"
when
        Time cron "0 0 15 6/7 * ?"
then
        occProxy5.sendCommand(ON)
end



rule "Heating Occupancy OFF1"
when
        Time cron "0 30 7 ? * *"
then
        occProxy1.sendCommand(OFF)
end

rule "Heating Occupancy OFF2"
when
        Time cron "0 30 21 ? * *"
then
        occProxy2.sendCommand(OFF)
end

rule "Heating Occupancy OFF3"
when
        Time cron "0 0 10 6/7 * ?"
then
        occProxy3.sendCommand(OFF)
end

rule "Heating Occupancy OFF4"
when
        Time cron "0 0 13 6/7 * ?"
then
        occProxy4.sendCommand(OFF)
end

rule "Heating Occupancy OFF5"
when
        Time cron "0 0 16 6/7 * ?"
then
        occProxy5.sendCommand(OFF)
end


rule "Proxy Heating"
when
        Item gOccupancyHeating received update
then
        if(gOccupancyHeating==ON){
                occupancyHeating.sendCommand(ON)
        }
        else if(gOccupancyHeating==OFF){
                occupancyHeating.sendCommand(OFF)
        }
end