Easy EmonCMS data with HTTP Binding

I have had a local ‘smart’ meter (this one) feeding data into an EmonCMS instance on the same RPi that I run openHAB on for a while, but had not pulled the EmonCMS data into OpenHAB. My main goal was to have a meter I could monitor remotely to provide readings to the electricity company in months that I’m away (their regular smart meter doesn’t get reception at my place unfortunately).

I looked around for examples of how to get EmonCMS data into OpenHAB, but didn’t see anything that matched my needs, so I thought I would write a short tutorial, as it’s super easy, but had some pitfalls that hopefully others can avoid!

What you’ll need:

  • An openHAB instance with the HTTP Binding installed
  • A locally hosted EmonCMS instance
  • Basic familiarity with creating items with .items files

Note that it doesn’t matter at all what type of meter you have, if the data is coming into EmonCMS, you can get it into OpenHAB this way.

Navigate to your EmonCMS instance: http://<your_emoncms_server_ip>/emoncms

Log in, and click on ‘Feeds’, then ‘Feed API help’ (top right corner):

Copy the ‘Read only:’ API key:

Click on ‘Feeds’ on the left again, and mouse over the channels you’d like to bring into OpenHAB, and make a note of their ‘ID’ (example - kWh for me is feed ID 24):

Create a new items file, or add to an existing .items file, using the following example for structure:

Number Electricity_Meter "Electricity Meter [%.1f kWh]" (any_group_you_like) { http="<[http://<your_emoncms_server_ip>/emoncms/feed/value.json?id=<id_you_picked_earlier>&apikey=<the_API_key_you_copied>:<Refresh_interval_in_ms>:REGEX((\\d*.\\d).*)]" }

This adds a kWh value with 1 decimal place.

A note on the REGEX expression (this is what I spent a bit of time getting right…):

REGEX((\\d*.\\d).*) //will turn 4567.123 into 4567.1, which is what we want

\\d* //matches any quantity of digits
.    //matches a .
\\d  //matches a single digit
.*   //matches whatever comes after in the result

//The expression returned is the one between the interior brackets ()

//With my meter, the voltage feed had no decimal place, so for that I used:
REGEX((\\d*).*)

Repeat for however many channels you want to add to OpenHAB!

I haven’t mentioned how to graph this info, because that will depend massively on your persistence setup, and because EmonCMS already has pretty decent charts etc, you may not even want to bother.

Hope this is helpful to someone - I spent maybe an hour or so messing with the HTTP item format and the REGEX query, this should mean you don’t need to!