Motion Sensor configuration & Rules (mysensors)

Hi Heys,

I’ve just completed my third mysensor which is the motion sensor, which is working perfectly and sending messages thought to my mosquito mqtt server.

I’ve been trying to figure out how to set it up in openhab, so far I’m having no luck with the following

I have created the following items

Number hall_motion_raw          "Hall motion is [%d]" {mqtt="<[matt_broker:mygateway1-out/3/1/1/0/16:state:default]"}
Number hall_motion "Hall Motion [MAP(motion.map):%s]"{mqtt="<[matt_broker:mygateway1-out/3/1/1/0/16:state:default]"} 

I have also set these up in my sitemap just to double check they are working, and I can see their states change from on to off, which is great.

I have tried to alter a rule from here to switch on my hall light https://github.com/openhab/openhab/wiki/Samples-Rules#how-to-turn-on-light-when-motion-detected-and-is-dark but i obviously have something wrong, any pointers would be much appreciated.

My rule is setup below and isn’t working for some reason thanks

var Number counter = 0
var Number lastCheck = 0

    rule "hallLightOn"
    when   
        counter = counter + 1
        Item hall_motion changed from OFF to ON 
    then   
        sendCommand(Toggle_1,ON)    
    end

    rule "hallLightOff"
    when   
            Time cron "0 * * * * ?"
    then   
            if(lastCheck == counter) {
                    counter = 0
                    lastCheck = -1;
                    sendCommand(Toggle_1,OFF)
                    sendCommand(hall_motion,OFF)
            } else {
                    lastCheck = counter
            }
    end

Matt,

Great to see you have the mysensor PIR’s operating, I got a few running recently as well.

I am not an expert on rules but can see a couple of challenges,

The item hall_motion looks to be a Number item, so I don’t think it will turn OFF or ON.
The way I get my PIR data from mysensors is like this,
Number mysPIR1 "mysPIR1" {mqtt="<[mosquitto:mygateway1-out/20/1/1/0/16:state:default]"}
This grabs the number (0 or 1) coming from the sensor I do the transform via the sitemap display.
On the sitemap,
Text item=mysPIR1 label="myPIR1 [MAP(1on0off.map):%s]" icon="info"

While I havn’t yet got around to triggering lights I have tested a rule,

    rule "Testing mysPIR1"
    when 
    	Item mysPIR1 changed
    then
    	if((mysPIR1.state as DecimalType) ==1) {
    		//sendMail("myemail@gmail.com", "PIR triggered!", "")
    		sendBroadcastNotification("PIR upstairs triggered... testing")
    		}	
    end 

In this case I work in the rule with the Number item rather than a Contact type item, the ON or OFF.

I think you can do the transform in the MQTT item before it gets to openHAB, unfortunately I havn’t quite got my head around that yet.

I don’t think you can put ‘counter = counter + 1’ in the trigger of a rule, that is best done in the execution area.

Regards,
George

Thank @George_Whitehouse your help is very much appreciated.

I have modified my items and rule based on your suggestion to this

sitemap:

Text item=hall_motion_raw label="Hall PIR [MAP(motion.map):%s]" icon="info"

Item:

Number hall_motion_raw          "Hall motion is [%d]" {mqtt="<[matt_broker:mygateway1-out/3/1/1/0/16:state:default]"}

Rule below is still not working:

  rule "hallLightOn"
    when   
        Item hall_motion_raw changed 
    then   
    if((hall_motion_raw.state as DecimalType) ==1) {
        sendCommand(Toggle_1,ON)    
        } 
    end

Thanks @George_Whitehouse based on your rule I’ve managed to get it working see below. This is a pretty crude rule, but it seems to work, i’m just wondeing how I now also add the time of day to the rule, so the light only switches on when motion is detected and its dark?? Can I use the astro binding for this can anyone show me how to add that my my rule?

   rule "hallLightOn"
    when   
        Item hall_motion_raw changed 
    then   
    if((hall_motion_raw.state as DecimalType) ==1) {
        sendCommand(Toggle_2,ON)    
        } 
    end

   rule "hallLightOff"
    when   
            Time cron "0 * * * * ?"
    then   
           if((hall_motion_raw.state as DecimalType) ==0) {
        sendCommand(Toggle_2,OFF)  
            }
    end

Matt, I have a string item which I use for this very function. The item is either set to “Day” or “Night” and in order to set this I have a rule which is triggered from the sunrise sunset event from the Astro binding. I set this up to control what happens with my security cameras at different times of the day based on presence and assumed that at some point in time I would need it for something else and was being lazy because I didn’t want to check time of day against the sunrise sunset times and I don’t have a Sensor for light levels yet…HTH
Rex

Thanks @rex_nightingale thanks for your reply, are you able to share your rule? as I would like to incorporate it with my rule too.

Thanks Matt

Yep will do. I’ll have a look later outside of work time

Rex

Matt, as promised information which should help you.

You need to install the ASTRO binding to make this work, I won’t go into the details here for this as there is plenty of info as to how to install and configure out there already.

For the rest here goes (bear in mind that this is how I have it set up and you may want to do it another way and different names, etc)…

Items
/* Astro Items */
Switch Sunset_Event {astro=“planet=sun, type=set, property=end”}
Switch Sunrise_Event {astro=“planet=sun, type=rise, property=start”}

/* Miscelaneous Items */
String DayNight “Day / Night Mode [%s]”

Rules
import org.openhab.core.library.types.*
import org.openhab.core.library.items.SwitchItem
import org.openhab.model.script.actions.Timer

rule "Sunset based events"
when
Item Sunset_Event received update ON
then
postUpdate(DayNight, “Night”)
end

rule "Sunrise based events"
when
Item Sunrise_Event received update ON
then
postUpdate(DayNight, “Day”)
end

Method
Basically what happens here is that when the Astro Sunrise event is activated, then I set the item DayNight to Day, when the Astro Sunset event is activated, then I set the item DayNight to Night…simples

Then all I have to do is check for the state of the item DayNight (“Day” or “Night”) whenever I need to test. In my case, as mentioned above, I use this to set the motion detection status of my security cameras and using a presence indicator to know if we are at home or not. Example of this final test is then in the following rule

rule "Presence Changed"
when
Item Presence changed
then
if (Presence.state.toString == “Home”) {
if (DayNight.state.toString == “Day”) {
postUpdate(SecCam_MDEnable, OFF)
}
}
if (Presence.state.toString == “Away”) {
postUpdate(SecCam_MDEnable, ON)
}
end

Hope that helps you in your quest.

Rex