Help me - Turn off light after 5 minutes human body go off

rules
openhab2
Tags: #<Tag:0x00007fe055259d78> #<Tag:0x00007fe055259c10>

(Rosario De Gaetano) #1

Help me - Turn off light after 5 minutes human body go off

Good evening I’m a beginner
I would like to implement a rule that does the following:
I turn on the light with the physical button and go into the room, at the same time a 5 minute timer starts to turn off the light but if the body sensor detects a movement in the room it starts the timer again. The light should switch off after 5 minutes if the body sensor detects no movement or if I switch off the physical button.
Thank you all

ITEMS:
Switch Luce_Centrale_Cucina “Luce Centrale Cucina” (Luci, LuciCucina) [“Lighting”] { channel=“openwebnet:bus_on_off_switch:xxxxxxxxx:xxxx:switch” }

Switch Pir_Cucina_Motion “Stato Movimento Cucina” (gPirPresenze) { channel=“mihome:sensor_motion_aq2:xxxxxxxxxxxxxx:motion” }

now I use this rule to turn off the light automatically after x minutes but I can not integrate the body sensor.

Thanks to everyone who wants to help me

var Timer TimerLuceCucina = null

rule “Luce Cucina Auto OFF dopo 2 Minuti”
when
Item Luce_Centrale_Cucina changed
then
if (Luce_Centrale_Cucina.state == ON) {
if (TimerLuceCucina === null) {
logInfo(“rules”, “Avvio del TimerLuceCucina”)
TimerLuceCucina = createTimer(now.plusMinutes(1)) [|
sendCommand(Luce_Centrale_Cucina, OFF)
logInfo(“rules”, “Tempo Scaduto Spengo la Luce Cucina”)
]
} else {
logInfo(“rules”, “Resetto il TimerLuceCucina”)
// subsequent ON command, so reschedule the existing timer
TimerLuceCucina.reschedule(now.plusMinutes(2))
}
} else if (Luce_Centrale_Cucina.state == OFF) {
logInfo(“rules”, “Spengo la luce cucina”)
// remove any previously scheduled timer
if (TimerLuceCucina !== null) {
logInfo(“rules”,“Cancello il Timer”)
TimerLuceCucina.cancel
TimerLuceCucina = null
}
}
end


(anonymous.one) #2

Head over to the Add-ons wiki and review the expire binding, you put the timer at the item level (I placed it on my light switch item) and then set your rule to turn the light on if it detects motion which will in turn refresh the expire.

I found in my case the motion detection refreshes every 5 - 6 mins, so I had to set that particular light to expire after 10 mins.


(Vincent Regaud) #3

Please use the code fences


(CM6.5 H102) #4

As @anonymous.one mentioned, using the expire binding is a good route. See my example rule below that uses iPhone detection and the expire binding as a timer. The rule executes when gPresent receives an update. If no one is detected a timer set for 20 minutes is started. After the 20 minute timer expires then Presents will send a command off. If during the 20 minutes presents is detected then the timer is canceled.

If you modify the items and the expire timer, to suit your needs, you should be able to use the rule with motion as well.

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 this helps.