Motion Sensor Setup in OpenHAB



  • 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 obvouisly have something wrong, any pointers would be much appreciated.

    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-Pitts First obvious thing is that the line "counter = counter +1" now placed after "when" should be placed after "then".
    Also your item, "hall-motion" is a number and defined as "state" and you check for "ON" or "OFF" in the rule. I doubt you can get these things to match because using "ON -OFF" would belong to a switch in Openhab (or do you send for example 0 and 1 and map these to ON/OFF maybe ?)

    I have not tried to figure out the "hallightoff" rule but for example the line "sendCommand(hall_motion,OFF" is a command which i guess should be sent to the sensor to set the "hall-motion" to off but your definition of "hall-motion" is (as above) a number and a state change (But maybe the mapping could work in that direction too, I have never tried it. Actually I do no not understand why this is needed, thought a motion sensor turns off by itself and that the logic should be to send that change from the sensor. But I have not used these with Arduino so have not thought about that. My motion sensor is on z-wave).

    Hope this can help you somewhat.

    Correction - I should have looked at the motion sketch before answering. It sends 0 and 1 so pls disregard that comment, The number will work to pick it up.



  • Thanks @mbj for your reply, I managed to get it working with this very basic rule and item

    Item entry:

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

    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
    

    If anyone has any better ways of writing a rule or suggestions for how to improve this one I would be grateful.

    Thanks Matt



  • @Matt-Pitts A very small simplification - in your first rule have you tried:

    if (hall_motion_raw.state == 1) sendCommand(Toggle_2,ON)

    I am not sure if it works but hall_motion-raw.state is a number so I think it should but it is always hard to predict how Openhab item states works in rules. I hate all these conversions like "as Decimaltype" needed in many places.

    How simple the rules can be made depends also on the settings of your sensor. My motion sensor (z-wave) sends ON when triggered and OFF after a set time after untriggered. So the rules become really simple.

    Without knowing how your sensor type and setup thus it is hard to comment much. From reading about the most common type of sensor it seems that If it has the jumper set to retriggering then the signal stays on until motion is no longer detected while set to non-retriggering the signal varies on/off as triggered. If your sensor works the "retriggering" way the rule for "hallLigtOff" may work with something like:

    rule "hallLightOff"
    when
    Item hall_motion_raw changed
    then
    if(hall_motion_raw.state ==0) sendCommand(Toggle_2,OFF)
    end

    Sorry for not being entirely sure about how this sensor works but I have not used it or experimented with it. I need to try it some day.

    UPDATE: I tested one of these sensors today (referring to settings described here http://www.mysensors.org/build/motion) :

    Jumper set to "auto reset"- sketch sends 1 and then a 0 comes when there is no motion and timer runs out.
    Jumper set to "no reset" - sketch sends 1 and then 0 comes after timer has run out, if motion still detected a 1 is sent again followed by a 0 when timer runs out and so on. So as long as motion is detected this goes on over and over again.

    When using "auto-reset" the rule above ought to work (if the "DecimalType conversion can be deleted, otherwise with this added)


Log in to reply
 

Suggested Topics

  • 22
  • 3
  • 135
  • 1
  • 19
  • 4

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts