PIR Sensors and Timers

Hello,

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?

Bjarne

Hi Bjarne,

Thanks for your answer.
Do you or anyone else has a code example for this?
How to turn it on when a motion is registered?

And: This time i’m using a Wemos D1 Mini with ESP8266 and a cheap PIR module.

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.

The states of a contact Item are OPEN, CLOSED or (when uninitialized) NULL. So the rule trigger would be

Item pirone received update OPEN

or you could use it in an if clause:

rule "Turn on lights when motion triggered"
when
        Item pirone received update
then
        if(pirone.state == OPEN) {
              bed.sendCommand(ON)
        }
end

Thanks, I have updated my own post!

Thanks Udo,
That’s working, but how can i make the delay for turning off?

There are different ways for this. Easy (but limited) is the expire binding. Just take a look at the documentation https://docs.openhab.org/addons/bindings/expire1/readme.html

A more complex way would be a rule:

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