Design Pattern: Motion Sensor Timer

Tags: #<Tag:0x00007f183130dfb8> #<Tag:0x00007f183130da40> #<Tag:0x00007f183130d1a8>

(John Cocula) #21

Your rule above, “KG Garage Licht auto off”, could be removed and replaced with adding the expire binding to the item:

Switch Licht_KG_Garage_1 "KG Garage Licht" { yourbinding="..." or channel="...", expire="5m,command=OFF" }

(Tom) #22

I found this very useful, it allows you to have different timeout values for items as well which is a benefit.

(Ham Wong) #23

HI @rlkoshak, I try to run your code but with error, I don’t know how to make it work,
do you mind gave a hand on it?

10:24:45.871 [ERROR] [.script.engine.ScriptExecutionThread] - Rule ‘A Motion Detector triggered’: org.eclipse.smarthome.core.library.items.SwitchItem

import java.util.Map

val Map<String, Timer> timers = newHashMap

rule "A Motion Detector triggered"
    Item MS_LivingRoom_MotionSensor1 received update MOTION or
    Item MS_LivingRoom_MotionSensor2 received update MOTION
    Thread::sleep(100) // give persistence time to save the update
    val sw =  gMotionSensor.members.sortBy[lastUpdate].last as SwitchItem
    logInfo("Motion.Sensor", "Motion Sensor Counter start")

    val timeoutMinutes = 3 // use an appropriate value

    if(timers.get( == null){
        timers.put(, createTimer(now.plusMinutes(timeoutMinutes), [|
        	logInfo("Motion.Sensor", "Motion Sensor Counter works")
            timers.put(, null)
    else {
        logInfo("Motion.Sensor", "Motion Sensor Counter timer extend")
    logInfo("Motion.Sensor", "Motion Sensor Counter finished")

(Rich Koshak) #24

What item types are you motion detectors. I’ve never seen an Item with a MOTION state. In the rule you cast then to a SwitchItem which only has the states of ON and OFF.

Do you get the first log statement in your logs?

You may need to filter out the items that have yet to be saved to persistence.

val sw =  gMotionSensor.members.filter.[s.lastUpdate != null].sortBy[lastUpdate].last as SwitchItem

(Ham Wong) #25

I use mihome, which motion sensor announce MOTION when detect motion,
I set String type at items,

yesterday I have setup mapdb and have * : strategy = everyChange, restoreOnStartup in mapdb.persist
I remove restoreOnStartup from influx and I see item state restored after system startup,
so I assume value should have saved.

I really don’t know how to do coding, according to your last reply, does your code only able to apply to item type is switch?

(Rich Koshak) #26

My code as written expects the motion sensors to be SwitchItems. That is what the as SwitchItem means when you get the latest updated item.

Change that to StringItem and it might work.

Also note the was a bug that was preventing the use of Strings in the updated or changed to rule triggers. Perhaps this has been fixed…

(Ham Wong) #27

Thanks, I will try again once I am back home,
you mention OH2 prevent update or changed to rule triggers on Strings type,
does that mean if item type is string will not get trigger in rules if it’s not been fixed?
if it is than no wonder why I can’t set a test rules which will light up when door open,
I tried it last night without success, I thought I was using wrong code in rules.

(Rich Koshak) #28

Item MyStringItem changed works.

Item MyStringItem changed to "Some String" currently broken (unless I missed an update with a fix).

Item MySwitxhItem changed to ON works.

(Ham Wong) #29

gerat info, Thanks for let me know, it will save me lot of time on trying,
I will continue try on the code see which part goes wrong

if you don’t mind, would you just a short question,
will if(receivedCommand==OPEN){ works? or it should only works when ==ON?

(Rich Koshak) #30

If the item that triggers the rule is a Contact, OPENED. If it is a Switch, ON.

Each Item type has different state types. You have to use what is appropriate for the Item type.

(Ham Wong) #31

Thanks, I think I need to study on Docs regarding item type,
It’s really a long way to go for me to make OH2 runs as I expecte

(Ham Wong) #32

Hi @rlkoshak

change to StringItem still not works, rules get trigger without error but just no further action
I change to switch items with transform than it works :grinning:
your provide information helps me a lot, Thank you in deep.

(papabrenta) #33

As Watou said on Jan 11, addressing a state that has not been updated for a while works well with the expire binding.

Here is a tutorial I did on using expire binding to catch nodes that have not reported in for a while. The tutorial includes links to expire binding documentation by Watou.

(John Cocula) #34

Great tutorial! Thanks for sharing it.

Just a note that the Expire binding is the brainchild of Michael Wyraz; I just pushed it along a bit. :smile:

(Ole Morten Rønning) #35

Thank you @rlkoshak for great examples.
Just want to point out a small typo:

There should be a terminating ) just before the ] at the end of the lambda I think


(Rich Koshak) #36

Actually the paren goes after the ]. Thanks for catching the error.

(Alexander G) #38



sorry for opening this old thread…
concerning the expire binding on a motion sensor, what happens if motion is constantly being triggered?
Does it still turn off after X time, or will it restart the timer if for example a rule calls for turning a switch on?

I have a functioning motion sensing rule which adds 1m to a timer when continous motion is tracked. Unfortunately once in a while, sometimes daily this timer is not being cancelled correctly, thus the lights remain on.

Checking for this “open” timer is quite hard, as the system cannot know whether it should/should not be there.

(Rich Koshak) #39

It gets rescheduled for each trigger. It is only after the set amount of time after the last motion event that the expire binding triggers.

(Alexander G) #40

Great, thanks for the swift reply.
Will rebuild my rules accordingly with the expire bindings.

(Oliver) #41

is it possible to post a running motion detection rule (whole code) when someone succeed in?
I try to make some tests with the aeon multisensor 6… and want to use it as a trigger when motion than message and when dark and motion than turn on light as long as 5 min are no more motion…
another combination could be turn on light if motion but dont turn off if tv still one ore something like this… there are a lot of examples, but not completly explained and often missing code or information or with some problems…
I am new here, but isn’t here a section with complet examples and not with 100 of posts in it with coments which is working and which not etc…?
Till now everyone has to search in several diferent websites for answers regarding a special item, f.ex. zwave items like my sensor… and than collect all this info with diferent examples to adapt… isn’t it possible to acumulate here one by one the devices and examples etc???

kind regards,