Pir, mq6, mq2, dht11, ldr sensor interfacing and on state change RULES

Hello everyone!
I am trying to make a project on home automation with senors which has the following functionality

  1. Turn on led when motionsensor is high
  2. Turn on led when ldr sensor is low
  3. Turn on fan when humidity is high
  4. Turn on buzzer when mq6 is rising
  5. Turn on buzzer when mq2 is rising

I would like to know what will the code be for the above mentioned functioanlity.
Please send me the items, sitemaps and rules code for above, ehichever is available, asap.
Thanks for the help in advance

I applaud your entry into doing a home automation project but in regards to your request:

  1. This is impossible without more information. How are these devices connected? What technologies do they use? Are they connected to the same server running OH?

  2. We are far more likely to help with specific questions and problems. “Do my homework for me” is unlikely to be productive for you.

A good place to start is the Beginner’s Tutorial and then the binding documentation for what ever technology you are using.

If you have specific problems we will be happy to help. We are unlikely to do this project for you though.


Yes, as Rich said: do your home work, then feel free to come back with specific questions.

While your project is clear to me, and I have built one of these last week, my solution, may be entirely different to what you want to or achieve.

In a nutshell:

  • you have to find a microcontroller (Arduino) to connect the sensors to
  • write the C code to get values from the sensors
  • decide on a communication method (MQTT) to talk to OH
  • create items, sitemap and rules

Once you’ve done all that, feel free come back and ask a question.

Also, while jumping to a solution to a problem, you may want oto spend some time thinking on what you want to achieve overall, how you see the system grow, and what you can do to ‘architecture’ the solution, so that it can be expanded in the future. Unless, it is just a proof-of-concept – then you can hack together whatever works.

This forum is about openHAB.
The Arduino forums out there will give you info on Arduino and sensors.
You’ll find info on MQTT on Google, some here, some on the Arduino forums.

In any case: good luck with it all.

@rlkoshak @Max_G Thank you for the response.

I have been working on this project for weeks now but was stuck with the problem of port forwarding for the custom app that i was creating earlier. After a lot of research and trying out various web services i decided to give up on the idea of my own custom app and came across openhab 4-5 days ago which i found serves my problemstatement and has been designed wonderfully. I got around successfully with controlling led’s via sitemaps. But since my submission deadline is in two days and i haven’t found any article on openhab which would guide me on how to control the sensors i decided to ask for help over here.
I have been doing my homework myself but with this short amount of time i would really appreciate if you could guide me how to get along with it.

@rlkoshak i was hoping that i could connect PIR and LDR dirrctly to raspberry pi and write rules for it to turn on led when PIR senses motion.
@Max_G is there a way to communicate with arduino using MQTT without the need of any external device and recieve input for sensors?

Also i would like to mention that i have already tried controlling PIR as:
Contact MotionSensor “PIR” {gpio=“pin:18”}
Switch Led “LED” {gpio="pin:12}

Rule "PIR"
When (pIR. State==ON)
Led. SendCommand(ON)

But it didnt work for me

I sense your frustration, but this is a different story now :slight_smile:

I am – despite using OH v1 for almost a year (on and off) while building a house; I am still a novice. :slight_smile:

When I say architecture, Iuse my rPi for OH and MQTT and that’s it (self-contained, defined function).
The Arduinos is a platform I use to interface with whatever ‘thing’ I need to interface; windows, HVAC, lights, sensors, etc. These act as sensor aggregators, and protocol converts (if you will) from I2C, SPI to MQTT.

The Arduino is a micro-controller board with a bunch of analog, digital, and other inputs/outputs. programmed in C via the Arduino IDE (or sloeber).

Also meaning, I have not done any OH <-> rPi GPIO set-up or integration; I am sure you can look up the GPIO binding and maybe other chime in.

I agree in principle though, that the the ON|OFF items, like PIR and LED can be driven via GPIO (be mindful of power draw, and the tPis capability / specs in this space).

Your rules:
Rule "PIR"
When (pIR. State==ON)
Led. SendCommand(ON)

should rather look like this (watch spaces, upper / lower case, rule construction):

rule "PIR"
  MotionSensor changed to ON

Well, I wouldn’t dare to figure out OH in 3 days – I am still trying :slight_smile:
Also, are you using OH v1 or v2?

I m using OH v 2
I was able to successfully recieve imput from sensors connected to arduino via a python script running on rpi.
But since i am using openhab2 now i wonder if somehow i could recieve sensor’s input in openhab2 (with or without arduino )and control led’s based on the input recieved.
I was thinking of running a python script parallely with openhab2 on rpi but i dont think they will collaborate with each other, Which is why i am hoping that somehow i could recieve sensor input directly in openhab again with/without arduino.

Look at the gpio binding to get oh to talk to the sensors and actuators directly.

Look at the exec binding if you want to have oh just call the python scripts you already have working.

Look at my sensorReporter script on GitHub for a couple of ways (http and mqtt) to have a python script send and receive messages to/from openHAB.

Given the time constraints I would probably use exec binding and your already working python scripts.


Thanks this is really helpful