Pages

Sunday, 13 December 2015

OpenEnergyMonitor, emonPi and openHAB


Using the emonPi in it's current default configuration works great to post data to Emoncms for logging and visualisation. However there may be times when you want some more flexibility and or ability to interface with other hardware or services 

In my last few blog posts I have been taking a look at making the emonPi the heart of a smart energy optimised home and how live energy monitoring data from the emonPi can easily be used to inform other services:   


Following on from my last post on controlling LightWaveRF devices from the emonPi using MQTT we now need a front end interface to handle the control. I have recently been taking a look at how openHAB can be configured to provide a front end interface to OpenEnergyMonitor hardware and control. 



We are working on releasing an updated pre-built SD card stack for the emonPi which will include Emoncms V9, MQTT, Node-RED and openHAB all pre-configured. I hope to post a beta image download to the forums in the next few days. 

Open Home Automation Bus (openHAB) is "a vendor and technology agnostic open source automation software for your home". OpenHAB can run on an emonPi (Raspberry Pi) and is very flexible and can be configured for just about any home automation task. This high level of configurability can also make it seem quite hard to understand to start with. I've have put together a skeleton config to setup openHAB with to OpenEnergyMonitor pulling data from the MQTT server running on an emonPi. 

Once setup you simply browse to http://emonpi:8080/openhab.app?sitemap=oem to view the openHAB user interface. openHAB also has a nice Android or IOS app which can be pointed at the same URL.  

openHAB OpenEnergyMonitor skeleton setup

openHAB Android app
openHAB LightWaveRF control using openHAB Android app





Installing OpenHAB

I have put together a guide from installing OpenHAB on an emonPi with the config shown above to work with OpenEnergyMonitor on the oem_openHAB GitHub

OpenHAB Configuration 

OpenHAB configuration is based around two files 'items and 'sitemap'. In my openHAB emonPi install instructions I recommend cloning the oem_openHAB to the home directory then soft linking the Items and Sitemap files to the relevant config locations in /etc/openhab/configurations/. This makes the files easier to edit.

It's possible to have multiple user interfaces defined by multiple sitemaps e.g oem.sitemap, kids.sitemap or admin.sitemap etc.

Items

The items file defines all the 'things' you might want to view or control in openHAB. Items can be bound to 'bindings' which are optional packages that used to extend functionality of openHAB. This example makes use of the MQTT and HTTP bindings. The items file is usually located in /etc/openhab/configurations/items.

For example the line in the items file to pull power 1 from emonhub MQTT is as follows:

Number emonpi_ct1 "Power 1 [%d W]" { mqtt="<[mosquitto:emonhub/rx/5/values:state:REGEX((.*),.*,.*,.*,.*,.*,.*,.*,.*,.*,.*)]" }


Notice the REGEX is used to tell openHAB that we want the first value in the 11 part CSV emonhub posts to MQTT to emonhub/rx/5/values.

and the items line to control LightWaveRF plug 1 (publish "1 1" to /lwrf mqtt topic) is as follows:

Switch lwrf1 "LWRF Socket 1" {mqtt=">[mosquitto:lwrf:command:ON:1 1],>[mosquitto:lwrf:command:OFF:1 0]"}

See the full items file used for this example on the open_openhab GitHub repo

Sitemap

The second openHAB config file of interest is 'sitemap'. Sitemaps are used to create elements of a user interface for making openHAB items accessible to the frontend interface.

For example the sitemap config line to produce the emonPi Power1 in the interface is as follows:

Text item=emonpi_ct1 icon="firstfloor"

And to produce the LightWaveRF control swithches:

Switch item=lwrf1 label="LWRF Plug 1"


See the full sitemap file used for this example on the open_openhab GitHub repo

I have intentionally kept this 'skeleton' example as simple as possible. To view the full capabilities of openHAB including using groups checkout the openHAB demo and corresponding demo items and sitemap files.

Demo


Here's a quick demo I made using openHAB to control LightWave RF plugs:


7 comments:

  1. Great post, thanks.

    It looks like there's been a formatting error with the display of the REGEX in the items excerpt. It looks like you're missing the question marks after the asterisks.

    See this post for clarification:


    https://community.openhab.org/t/is-anyone-using-the-open-energy-monitor-emonpi/4536/57?u=tinkerfailure

    ReplyDelete
  2. Hi Chris, thanks for your comment. I saw some threads on the openhab community forum. It was cool to see enonPi users had beaten me to it! Either:

    Number emonpi_ct1 "Power 1 [%d W]" { mqtt="<[mosquitto:emonhub/rx/5/values:state:REGEX((.*),.*,.*,.*,.*,.*,.*,.*,.*,.*,.*)]" }

    or

    Number emonpi_ct1 "Power 1 [%d W]" { mqtt="<[mosquitto:emonhub/rx/5/values:state:REGEX((.*?),.*?,.*?,.*?,.*?,.*?,.*?,.*?,.*?,.*?,.*?)]" }

    Seem to work fine for me. Either with or without the '?'. What effect does adding '?' have?

    ReplyDelete
  3. Hello Glyn,

    Most regex quantifiers (such as * or +) are "greedy". That is : they will look for the biggest string which matches the expression. The question mark make them "lazy"; looking for the shortest string.

    For instance, here is a sample string : "whatever". If you have the following regex /<.+>/, it will match the full string. If you use /<.+?>/, it will match "".

    In your case, it does not change much it terms of match but it may have a performance impact. I think using ? make it faster because it won't try to read the whole string to see if there's a longer match.

    You could also use /([^,]*),[^,]*,[^,]*,[^,]*,[^,]*,[^,]*,[^,]*,[^,]*,[^,]*,[^,]*,[^,]*/. This may be even better in terms of performance. [^,]* means any string not containing ",".

    But if you want to only retrieve the first field (and not checking there is exactly 11 fields, then you could use /^([^,]*)/ which will find the first substring not containing a ",".

    Hope this helps.
    JS

    ReplyDelete
  4. Hi JS,

    Thanks for your detailed reply. Yes, that makes sense. I think I will keep the example I cited using the full match as this is easier to understand and allows the user to easily customise which value is chosen.

    ReplyDelete
  5. Great post and excellent progress.
    Some questions:
    1. Can this version works with EmonBase v2?
    2. With the new OH2b1 release, this version already have openhab installed in it. How can I integrate it with my actual OH2b1 installation?

    Would like to understand how these parts are moving and what should I need do make it work in my environment.

    Best Regards!
    -BD

    ReplyDelete
  6. Hi Bruno,

    Thanks for your comment. Yes openHAB can be installed on emonBase. You will need to mount /var/lib/openhab as tmpfs since the root filesystem is in read-only by default. See emonPi example fstab https://github.com/openenergymonitor/emonpi/blob/master/fstab

    Sorry, what do you mean by OH2b1?

    ReplyDelete
  7. Really good! I use Openhab to view Emoncms data. My Openhab has also Emoncms's Raspberry data view and some commands (restart Raspberry, view Raspberry's temperature, memory use, CPU use, Wifi connectivity, etc).

    ReplyDelete

Note: only a member of this blog may post a comment.