My openHab Setup

I thought I’d document some stuff here on my openHAB setup, to help future guys, and also get some tips or suggestions from the community.
My name is Carel. I live in South Africa. I’m really interested in IoT and home automation, and thus I’m slowly automating and smartifying my existing house as I go.

So I’m running openHAB on a Raspberry Pi 2. I used the Openhabian installer, and it was a fairly straight forward installation.
For sensors I use Xiaomi Smarthome devices. Although they are very chinese, the sensors are of high quality, and I’ve not yet had issues with them.
For smart lights I’m using Xiaomi Phillips Bulbs.

I actually started out my beginner journey with smart home devices with a Xiaomi sensor set. It consisted of a Gateway device, which doubles as a small light, a smart plug, a motion sensor, an open/close sensor and a wireless switch.
This was my first real step into an actual smart home, and I had lots of fun playing around with the motion sensors, the wireless switch and the smart plug, turning things on and off.

I found openHAB when I started searching for a more customizable control centre. The Xiaomi devices does come with a surprisingly well-designed app on my devices, and the connecting of devices works well. But there were many commands, such as changing colors or brightness on the gateway light, or other timer-based events that I couldn’t do on the automation part of the Mi App.

I chose openHAB, because it already have working integration with the Xiaomi devices I had.

When I started using openHAB, I found it super nice to have a way to manually program rules. I am a programmer by profession, so having rules items set out in code actually makes it almost easier for me to understand what my system does.

Xiaomi and Aqara Smart Home
I won’t lie. I’m a big fan of Xiaomi’s home products. They are very well designed, looks very professional, and works out of the box as expected on most fronts. It feels a bit like a cheap version of Apple. All the packaging they use are very fancy, and it really is great to receive your set of sensors in a nice fancy box, everything packed neatly, with small instruction manuals (in chinese. =P) for each.

Since my first buy, I’ve bought several motion sensors, switches, door sensors, and another Gateway to cover more of my house. I haven’t regretted it, it really is cheap vs the prices of similar sensors that I can buy here in my country.

How everything works together

My setup:

  • Rasberry Pi 2 running openHAB 2.
  • 2 Xiaomi Smart Home Gateways, both in the hallway of my house on two opposite ends, to cover most of the house.
  • Many motion sensors, for both automatic lights, as well as alarm triggers.
  • A few door and window sensors, mostly at doors and windows we open often.
  • A few switches to control the smart lights
  • One smartplug for my ‘clock radio’.
  • 5 Xiaomi Phillips smart lights at this point.

Smart Stuff

  • My hallway pretty much connects all the rooms in the house together, so the lights are always on at night until I go to bed. So I have two smart lights in hallway, that currently turns on at a specific time in the early evening.
  • I have a smart light in my bathroom, and two motion sensors to cover the shower, as well as the main part of the bathroom. At night, if motion is detected, the light automatically turns on, and stays on as long as it detects motion. Numerous test found this to work flawlessly, and I haven’t had to switch the light on manually since. It detects me in the shower, and also if I just use the sink to wash my hands or something.
  • A smart light in my toilet (which is separate from the bathroom). It is a small room, so one sensor easily detects motion, and keeps the light on until no motion is detected.
  • My living room, which is essentially a ‘recreation room’ now, has a smart light as well. Motion sensors detect motion in the room, and turns on the light when motion is detected after a certain time in the evening. They then stay on until turned off. So if nobody enters the room at night, they stay off, saving me some power.
  • I have a radio in my bedroom, that’s on a smart plug. It turns off at night when I go to bed, and turns on in the morning to wake me up. Its a over-complicated clock radio!
  • Our 2 cats are kept inside at night, because otherwise they fight with other cats in our garden. To that end, we sometimes forget a window or a door open, and they escape again. Using open close sensors on doors and windows that we often open, I have a timed script that checks in the early evening if any doors are open, and alerts me on my phone which ones are open.
  • Although not smart in itself, I have currently a wireless IP cam that points to my gate. I use the sitemap to display this camera in openHAB, allowing me access to the view of the camera through the app, saving me the trouble of having to use a different app in conjunction.
  • I have a old tablet, which sits against my wall in a mount. It is permanently open on the openHAB app’s sitemap, essentially giving me control of my house and stuff even if my phone isn’t with me.
  • I also have a single Google Home Mini device. Recently openHAB has been integrated directly with Google Home, allowing me now to control the lights in my house by voice, which is pretty awesome. Later I’ll have it arm and disarm my alarm, and other nifty stuff.

Stuff that isn’t working right
So there is a few stuff I’m trying that isn’t working correctly yet, and I’m working on it. =P

  • I have ‘presence detection’ for me and other people that live and visit my house, using static ip addresses on my wifi using our phones, and the ping binding on openHAB. But sadly, this isn’t working as intended. My phone often disconnects from WiFi, presumably to save battery, which causes false positives to occur on presence detection. So I’m still trying to figure out how to reliably tell openHAB if specific people are present in the house.
  • The Xiaomi motion sensors have a 1 minute delay after detecting motion to save on battery. This delay means that is the minimum amount of time a light needs to stay on, because I only know after one minute whether there is still someone in the bathroom. This delay is causing me some false positives, causing the light to stay on for 2 minutes+ even when I just enter to wash my hands, and exit within 30 seconds. I’m still working on some form of timer work-around for it.
  • Logging persistance in charts. I can’t seem to get it to work, so still need to do some research…

Future Stuff
These stuff is what I’m working on, as I buy more sensors and stuff each month.

  • I found that using IFTTT and openhab’s mail feature, I can ‘take a photo’ from my ip camera, and essentially display it as a notification on my phone. My idea is to use a Xiaomi switch or other wireless doorbell to trigger a rule on openHAB that rings the bell on my Xiaomi Gateway’s speakers, as well as send me a notification of someone at the gate, with a picture of who it is. This of course means I can see people ringing my bell, even when I’m not there, which is pretty cool.
  • My country’s crime rate is pretty high, so my next plan is to add many more motion sensors throughout the house, as well as more door and window sensors, and create a home alarm.
  • When armed, the alarm will trigger on any movement or changes in the sensors, When triggered, I’ll have the alarm sounds from the gateway speakers (and hopefully later on an external speaker of some kind), turn on all the lights in the house, and also send a notification to my phone and all other devices connected in the house that indicates where the alarm triggered. I think this will be very useful in determining where the threat is, and act accordingly. Turning on the lights and sounding the alarm will hopefully scare off more adamant would-be robbers.
  • Later I can also add some indoor cameras, and send a photo with the trigger notification to my phone.
  • In South Africa, we generally have high walls and fences, as crime is somewhat of an issue here. Our gate is automated, and generally secure, but there are ways to lift it over the motor or open it by force. I’m thinking of adding a open sensor to the gate as well, that also triggers the alarm. This is much earlier than robber already being inside the house, and should give me ample time to react to any threat, if I am already alerted when they’ve just entered the gate.

Other fancy future possibilities:

  • An automated voice intercom AI, that responds to people that rings the bell, asking them what their business is, and relaying the information to me via my phone or maybe through the Gateway Speakers or Google Home mini speaker.
  • Add the ability for me to open my automated garage and gate through openHAB.
  • Use image recognition software and a small camera on my gate, to allow me to ‘recognize’ number plates and notify me when specific owners of the vehicles stop at my gate.
  • Use a smart mini weather station to give me realtime info about the weather, and send alerts if it is cold enough for a jacket, or if it is raining hard at my house.

I’m sure there are tons of other cool stuff one could do, I’m always reading up on what other people do in their homes.

Coming soon…

Thanks for reading… =P


Nice write up, thanks :+1:

You may have a look at FIND.

Take a look at the expire binding. It will expire the ON state to OFF (or OPEN to CLOSED) after the time you set it to. But still you won’t detect any new motion because of the default sensor dead time.

Example: { channel="mihome:sensor_magnet:158d0001a984ba:isOpen", expire="5s,state=CLOSED" }

1 Like

Wow, that is really useful info. Thanks! I will take a look at those.

You can use the gateway speakers with the build in sounds or load your own files in the gateway. May be to create voice messages.

In addition to FIND, you can look at BT based sensors like reelyActive reelyActive Smart Spaces Revisited or GitHub - rkoshak/sensorReporter: A python based service that receives sensor inputs and publishes them in various ways..

The arping configuration on the Network binding should mitigate the wifi issues you are experiencing, though it takes a little bit of tuning to get it into asteady state. There are also options where people run scripts on their routers to detect and report presence of phones on the network.

Usually, one needs to run a combination of sensors to reliably determine presence. Generic Presence Detection will help you consolidate the sensors.

There is a weather binding and other ways to get weather info without having your own weather station, to at least get you started.

Good writeup.Thanks for posting.

You can kill two birds with one stone using cameras and something like EyeSpy, ZoneMinder, or Shinobi. These CCTV software systems can report to OH when motion is detected.

1 Like

Awesome, thanks, will read up on those too!
Should have done this long ago haha.

So, after some experimenting, I’ve found a few interesting things about the Xiaomi Motion sensors.
Firstly, they have a timeout value that can be specified from 5 seconds to a few minutes.
This means I can use that to have a ‘off’ timer with a trigger. Because the motion sensors waits 1 minute after detecting motion before checking again, I can set the timeout for the state of motion (ON or OFF) to for example 70 seconds.
This means that if I go into the room, and exit within a minute, it will pass the check, at one minute, without changing the status. After another 10 seconds, the motion sensors will automatically change motion status to OFF, triggering the rule, and allowing me to turn off the light.
If there is movement after one minute (if I stay longer in the room), it actually refreshes the timeout automatically. So that means it will then wait another 70 seconds before turning off the motion state.
So far this solution seems to be working very well.

I also found an issue with the sensors. The Aqara motion sensors have a built in light sensor that reports illumination in lux. I placed a sensor outside, under a roof, to allow me to check the light outside and use that to ‘globally’ know whether it is dark enough outside to trigger various light-related events (the auto lights, and the hallway lights). Sometimes, if it is cloudy, it becomes darker much earlier, which means I have to turn on the lights earlier than the timer.

The only problem is that I’ve discovered that the light sensors seems to only update their lux value when motion is detected. For the purposes I want to use it, this isn’t feasible, as there won’t necessarily be movement wherever I place the sensor to check for the light condition.

So I think I’ll have to look for a different cheap light sensor that is wireless, so that I can integrate it with openHAB and have a global ‘sunlight sensor’ if you will.

Next weekend I’m going to set up rules and more sensors, and start testing out an alarm function for the house.

Or use the astro binding and its sunrise/sunset range triggers (no need for a physical sensor at all);

rule "example trigger rule"
    Channel 'astro:sun:home:rise#event' triggered START 
1 Like

lol, there’s literally a binding for everything. Thanks, I’ll check it out, that looks exactly like what I need.

but astro binding is unable to determine if the outside is dark enough. I often have dark sky even from 15 minutes before sunset because of clouds.

That is true. Generally in the case of clouds I sometimes need the lights more than an hour earlier than my timer. But I just tell Google to turn on some lights, so although it would be great to eventually automate it, it will still be useful for my openHab installation to know when it is officially night and day, to trigger various stuff and be able to react correctly.

I’ll probably still eventually get a better lumination sensor for future.

Great write-up. I also live in SA, love OH, Xiaomi and their sensors and Yeelights. That’s pretty much the staple of my Smarthouse too, although I have a couple of Sonoff switches too (for example, one on my borehole pump). I still have to load custom firmware so that I can control it from OH.

I’ve been thinking about your thought on the electric gate for quite a while. WE sometimes accidentally trigger our remotes and open the gate, so I really want a way to know if the gate is open or not.

Problem is the Xioami sensors’s reception is not good enough, so I’m actually looking at either another gateway in my garage to solve the reception, or hacking a Sonoff SV (12v) to run directly off the gate’s circuit board.

Second problem with the Xiaomi sensors are that they are not really suitable for out-doors, so you’d have to get a better reed switch and magnet and then break open a Xiaomi door sensor and solder the wires to the existing reed switch (there are youtube videos where they do this).

The latter actually also made me think that you could use the Xiaomi door sensors as wireless transmitters for other outside PIR sensors and beams, meaning you could link those weather/water/catproof sensors to your existing setup.

On that note, do your cats trigger the motion sensors in your house?

Greetings fellow countryman!

That’s one issue I’m still working on. I actually realized the other day that the Xiaomi open sensor won’t work effectively, because of the space between the gate and gate motor (which is where I was going to play my sensor to prevent weather damage).
Perhaps modifying the sensor could work, and I’m already investigating that avenue.

Coincidentally I had a similar issue with the distance to the gateway, but luckily the kitchen is very close to the front gate, so tests revealed that it can connect to the sensor on the gate via Zigbee at that distance by putting a third gateway in the kitchen.I still need some research and then choices to make about this, but sadly I’m out of extra money this month, so I’ll have to continue next month!

Well yes.
But, the sensors seem to lose motion detection ability rapidly after about 2-3 meters though, so creative positioning of my sensors have helped prevent false detection caused by cats.
I’m actually next week going to implement my alarm in a ‘test’ phase, by having it log and also notify me when any of my current motion sensors are triggered. Then I’ll run it for a week, and check how many false positives I get to determine if the ranges are effective in preventing cat-detections.

On that note, I’ve actually found weird stuff with my ‘toilet’ motion sensor. The toilet is a separate small room, and I have a sensor pointing 45 degrees downwards directly at the toilet. So when you enter through the door you walk right under it and it triggers. What is weird is that very often if I just walk by the toilet door, the sensors seems to detect motion and triggers the light.
Its weird, cause the only motion it might detect is the toilet door moving slightly because movement of air when I walk passed? (the door is slightly ajar usually).

I always thought these were IR motion sensors, but now I’m wondering…

Thanks for the comments guys, so far very helpful. I’ll post some photos this weekend if I get a chance.

You could combine the astro binding and the weather binding with the cloud cover value in a rule.

This could work, but the nearest weather station is about 15km distance, so many false alarms for me. Thank you for the enlightenment. I never thought of it before

Just for an example for those who do have a weather station close enough.

Switch vIsCloudy "Cloud Conditions" <iscloudy> (gChart)

iscloudy is a custom icon I downloaded. gChart makes this Item be persisted for charting. See Design Pattern: Group Based Persistence.

I’ll not show the Weather Items but you can find them all here: Comprehensive Wunderground using HTTP Binding Example

rule "Is it cloudy outside?"
        Item vWeather_Conditions changed
        logInfo(logName, "New weather conditions: " + vWeather_Conditions.state.toString)

        val isCloudy = transform("MAP", "", vWeather_Conditions.state.toString)
        val newState = if(isCloudy === null || isCloudy == "false") OFF else ON

        if(newState != vIsCloudy.state) logInfo(logName, "Setting isCloudy to " + newState.toString)

end has a mapping from all the conditions that Wunderground supports to true or false to indicate whether the condition indicates it is cloudy.

# Cloudy or not
Light\ Drizzle=true
Heavy\ Drizzle=true

Light\ Rain=true
Heavy\ Rain=true

Light\ Snow=true
Heavy\ Snow=true

Light\ Snow\ Grains=true
Heavy\ Snow\ Grains=true
Snow\ Grains=true

Light\ Ice\ Crystals=true
Heavy\ Ice\ Crystals=true
Ice\ Crystals=true

Light\ Ice\ Pellets=true
Heavy\ Ice\ Pellets=true
Ice\ Pellets=true

Light\ Hail=true
Heavy\ Hail=true

Light\ Mist=true
Heavy\ Mist=true

Light\ Fog=true
Heavy\ Fog=true

Light\ Fog\ Patches=true
Heavy\ Fog\ Patches=true
Fog\ Patches=true

Light\ Smoke=false
Heavy\ Smoke=true

Light\ Volcanic\ Ash=false
Heavy\ Volcanic\ Ash=true
Volcanic\ Ash=true

Light\ Widespread\ Dust=false
Heavy\ Widespread\ Dust=true
Widespread\ Dust=true

Light\ Sand=false
Heavy\ Sand=true

Light\ Haze=false
Heavy\ Haze=true

Light\ Spray=false
Heavy\ Spray=false

Light\ Dust\ Whirls=false
Heavy\ Dust\ Whirls=true
Dust\ Whirls=false

Light\ Sandstorm=false
Heavy\ Sandstorm=true

Light\ Low\ Drifting\ Snow=true
Heavy\ Low\ Drifting\ Snow=true
Low\ Drifting\ Snow= true

Light\ Low\ Drifting\ Widespread\ Dust=true
Heavy\ Low\ Drifting\ Widespread\ Dust=true
Low\ Drifting\ Widespread\ Dust=true

Light\ Low\ Drifting\ Sand=true
Heavy\ Low\ Drifting\ Sand=true
Low\ Drifting\ Sand=true

Light\ Blowing\ Snow=true
Heavy\ Blowing\ Snow=true
Blowing\ Snow=true

Light\ Blowing\ Widespread\ Dust=true
Heavy\ Blowing\ Widespread\ Dust=true
Blowing\ Widespread\ Dust=true

Light\ Blowing\ Sand=true
Heavy\ Blowing\ Sand=true
Blowing\ Sand=true

Light\ Rain\ Mist=true
Heavy\ Rain\ Mist=true
Rain\ Mist=true

Light\ Rain\ Showers=true
Heavy\ Rain\ Showers=true
Rain\ Showers=true

Light\ Snow\ Showers=true
Heavy\ Snow\ Showers=true
Snow\ Showers=true

Light\ Snow\ Blowing\ Snow\ Mist=true
Heavy\ Snow\ Blowing\ Snow\ Mist=true
Snow\ Blowing\ Snow\ Mist=true

Light\ Ice\ Pellet\ Showers=true
Heavy\ Ice\ Pellet\ Showers=true
Ice\ Pellet\ Showers=true

Light\ Hail\ Showers=true
Heavy\ Hail\ Showers=true
Hail\ Showers=true

Light\ Small\ Hail\ Showers=true
Heavy\ Small\ Hail\ Showers=true
Small\ Hail\ Showers=true

Light\ Thunderstorm=true
Heavy\ Thunderstorm=true

Light\ Thunderstorms\ and\ Rain=true
Heavy\ Thunderstorms\ and\ Rain=true
Thunderstorms\ and\ Rain=true

Light\ Thunderstorms\ and\ Snow=true
Heavy\ Thunderstorms\ and\ Snow=true
Thunderstorms\ and\ Snow=true

Light\ Thunderstorms\ and\ Ice\ Pellets=true
Heavy\ Thunderstorms\ and\ Ice\ Pellets=true
Thunderstorms\ and\ Ice\ Pellets=true

Light\ Thunderstorms\ with\ Hail=true
Heavy\ Thunderstorms\ with\ Hail=true
Thunderstorms\ with\ Hail=true

Light\ Thunderstorms\ with\ Small\ Hail=true
Heavy\ Thunderstorms\ with\ Small\ Hail=true
Thunderstorms\ with\ Small\ Hail=true

Light\ Freezing\ Drizzle=true
Heavy\ Freezing\ Drizzle=true
Freezing\ Drizzle=true

Light\ Freezing\ Rain=true
Heavy\ Freezing\ Rain=true
Freezing\ Rain=true

Light\ Freezing\ Fog=true
Heavy\ Freezing\ Fog=true
Freezing\ Fog=true

Patches\ of\ Fog=true
Shallow\ Fog=true
Partial\ Fog=true
Partly\ Cloudy=true
Mostly\ Cloudy=true
Scattered\ Clouds=false
Small\ Hail=true
Funnel\ Cloud=true
Unknown\ Precipitation=true

Well, more a list for myself, but here is my near future todo:

  • Install and configure the astro binding as well as the weather binding to somewhat accurately predict when it will be dark enough for lights.
  • Research some cool stuff to do in combination with weather binding, such as changing the color temp of lights to fit the outside (warmer if its cold outside, colder if its warm outside, etc).
  • Continue to test the FIND app, and test its implementation in my house. Its actually pretty accurate, I’m impressed thus far.
  • Write the rules for running my alarm to start testing false positives
  • Maybe move over to using HabPanel, it actually looks pretty nifty.

And more to come.
I wanted to post pictures, but to be honest it wouldn’t look like much, its mostly small sensors and lights…

So this weekend I installed and configured both the Astro and the Weather (wunderground) plugins. Both work amazing.
Using the Astro Binding I can now effectively turn the lights on and off as needed. I think I’ll need to set an offset though, as it is somewhat dark already 5-10 minutes before sundown inside my house.

I also found that someone has a Weather Station ~3km from me, so I can get a somewhat accurate reading of the weather using that, and it works great.

I now need to still implement rules to turn on the lights earlier if it is close to sunset but cloudy.

I had an issue as well this weekend, where the astro binding didn’t want to initialize after a reboot. When I tried to uninstall it, my entire Openhab appeared to crash, and no rules would activate. Any installation gave me errors about the app list/manifest or such. (Can’t remember the error).
I used openhabian-config to update my Openhab which was still on 2.1, and through the process it appears I chose an option wrong somewhere, because my Taillog stopped logging anything.
A little bit of searching on the forums here showed me a file to replace which fixed that.
So my system is up and running again, and somehow the astro binding started working again.


So, small update.

I bought another Gateway, as well as 2 open sensors.
I also bought 2 Sonoff wireless switches to experiment with them.

I placed my open-close sensors on my garage door and my gate, and the gateway is close to them.

But, I ran into a problem here that I’m not yet able to solve. In the Xiaomi App, the gate and the garage door sensors work as intended, and registered open and closed statusses.

However, after adding the Gateway to Openhab (took me a while, Xiaomi moved the “about” menu of the gateways, so had to search to find it), and adding the sensors, I’ve found that they don’t send updates to Openhab. At all. And I can’t figure out why. I’ve restarted the openhab, the Gateway, open closed the sensors, nothing. They still register on the app, but Openhab doesn’t even show anything.

I then tried to change the logging to try and see what the issue is, but struggled to get the logging to send my Debug level messages, and eventually the whole logging just stopped working. I restored the files I changed, and my system is working again, however I still can’t get updates from those sensors…

I also installed the sonoff switches but I don’t have the right connector to flash them to use MQTT yet. So I tried using IFTTT, but its excruciatingly slow, so its pretty much useless without having direct control.
I’ll look for a connector locally, and then try and troubleshoot those sensors next weekend.

If maybe someone reads here.

I now found that the new Gateway I added doesn’t seem to have correct functionality with openhab. Unlike the other Gateways, I can’t send any commands to the new one, and I in fact get errors. This would also explain why I’m not receiving updates from my gate and garage door sensors.
I see someone else had similar issues on the Xioami Mihome binding thread, but there was never a solution to the issue.

I tried updating to the newest version of the Mihome snapshot. But someone it seems I broke my whole openHab installation.
I do have most of the config files stored separately, so I am close to just reinstalling everything from scratch, but it might turn out to be a lot of work.

After a few hours of reinstalling, apt-get updates, and a lot of googling, I switched to the snapshot version of openhab via the openhabian-config tool, got loads of errors, but overwrote all of the files it asks.
Then I switched back to the stable one, and FINALLY my system is running again at least, albeit with all kinds of errors in the log…

One in particular is weird. When I refresh the rules files, it appears to execute the rules as it should somewhat, but I don’t see my log messages that I run on the startup rule.

Instead I see this error:

2018-04-16 21:26:46.836 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'test.rules'
2018-04-16 21:27:01.796 [WARN ] [mmon.WrappedScheduledExecutorService] - Scheduled runnable ended with an exception
java.lang.NullPointerException: null
	at org.eclipse.smarthome.model.rule.runtime.internal.engine.RuleEngineImpl.lambda$1( ~[?:?]
	at java.util.concurrent.Executors$ [?:?]
	at [?:?]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201( [?:?]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ [?:?]
	at java.util.concurrent.ThreadPoolExecutor.runWorker( [?:?]
	at java.util.concurrent.ThreadPoolExecutor$ [?:?]
	at [?:?]

I’m not sure what that error means. I can see there’s some kind of null pointer error somewhere, but I’m not familiar enough with openHab’s code to know what could be wrong.

:Sidenote: My rules do fire, so I can still control everything in my house.

Any ideas on what I can do?
If this persists, I might just format the SD card, and start over, though I don’t really want to do that…

Anyways, on to tinkering with this stupid gateway that’s not working.