I have a ESP with a PIR Sensor, sending MQTT “commands”.
When a motion is detected, it’s sending “OPEN”, and then “CLOSED”.
Thats working and Openhab is also registering “the events”:
2018-05-16 18:27:50.028 [vent.ItemStateChangedEvent] - pirone changed from CLOSED to OPEN
But how can i make them turning on a switch and after 5 minutes off?
The rule files i found here in the forum had mistakes (log)
My home.items file:
// Item File
// Switches
Switch bed "LED-Strip Bett [%s]" {mqtt=">[broker:/max/light/bed:command:ON:1],>[broker:/max/light/bed:command:OFF:0]"}
// Temperatursensoren
Number temperature "Temperatur [%.1f]" {mqtt="<[broker:/max/temperature/one:state:default]"}
// PIR Sensoren
Contact pirone "PIR" {mqtt="<[broker:/max/pir/one:state:default]"}
Use the expire binding. Turn it on when the PIR message is received and have it expire after 5 minutes with an OFF command. Check out the binding documentation for examples. I’m curious what ESP are you using and what PIR sensor?
I have never used a Contact Type item but you can try something like:
rule "Turn on lights when motion triggered"
when
Item pirone received update
then
if(pirone.state == OPEN) {
bed.sendCommand(ON)
}
end
I’m not sure that it will send Open command, check it out please!
However you will still have to add expiration.
If you want to control this light not just by motion and you don’t want to have it to turn off when you power on manually, you will have to create a new switch, add expiration for that and create another rule when that switch changes to OFF, turn off the lamp
Hope this helps, I’m not an expert of openHab, so I’m not sure that this is 100% correct.
var Timer tBedLight = null
rule "Turn on lights when motion triggered"
when
Item pirone received update
then
if(pirone.state == OPEN) {
if(bed.state != ON) bed.sendCommand(ON) // only send ON if OFF
if (tBedLight !== null) tBedLight.cancel // cancel timer if started
tBedLight = createTimer(now.plusMinutes(5), [ | // start timer
bed.sendCommand(OFF) // when elapsed send OFF
tBedLight = null // reinitialize Timer
])
}
end