Bulb.co.uk SMETS2 Smart Meters to openHAB

Overview

I am using Bulb.co.uk as my energy supplier, and I have Smart Meters that use the SMETS2 protocol.

Whilst there is no native way to get the information from this, we can piggy-back through SmartThings, who have partnered with Bulb to provide a way of getting almost live information from the meter.

SmartThings Setup

You do not need a SmartThings hub - this is all done via the online services alone.

The following guide (at time of writing) shows you how to set up the SmartThings Energy Control (STEC) with Bulb.

Once you’ve done this, and you’ve got your SmartThings account and app running, and you can see the data coming from your Smart Meters, it’s time to grant access to this data so it’s usable in openHAB.

Providing Access and Verification

  1. Go to https://account.smartthings.com/tokens and create an access token. The Access Token allows you to access your account details without providing a username/password. This is unique to you and your account, so do not share it!
    I’m not sure what is the minimum level of access you can provide, I just gave all permissions to this token!
    If anyone can verify the minimum level of permissions that are needed, I will amend this accordingly!

  2. We then need to get the Device ID of your meters. We can use curl from the command-line of your openHAB server to do this:
    curl -s -H "Authorization: Bearer API_TOKEN" "https://api.smartthings.com/v1/devices"

  3. This should return a JSON string containing a list of all of your devices.
    To get the details of the meters, we can do:
    curl -s -H "Authorization: Bearer API_TOKEN" "https://api.smartthings.com/v1/devices/{device id}/components/main/status"
    This is another JSON string containing lots of information - we’ll be extracting the specific bits we want later

openHAB Setup

Once we’ve verified this works and your openHAB machine can reach the SmartThings API, it’s time to set up openHAB to use this information.

I’m still on 2.5.x, so this is how I did it using the old HTTP Binding. openHAB 3.x has a new HTTP Binding, so the configuration may look a little different, but the basic idea is the same.

http.cfg

smartthingsbulb.url=https://api.smartthings.com/v1/devices/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/components/main/status{Authorization=Bearer xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx}
smartthingsbulb.updateInterval=30000

The Header (within { and }) is case sensitive!

This will cache the result every 30 seconds, so when we’re using multiple items, we’re only hitting the API once per interval, rather than once per item.

.items

Number Total_Electricity_Used "Value: [%.3f kWh]" { http="<[smartthingsbulb:30000:JSONPATH($.energyMeter.energy.value)]" }
Number Total_Gas_Used "Value: [%.3f kWh]" { http="<[smartthingsbulb:30000:JSONPATH($.gasMeter.gasMeter.value)]" }

You’ll also need the JSONPath Transformation Service add-on installed

This will give you the total gas and electricity usage value, in kWh. There is some extra information in the JSON, such as the date/time of the update - I am also noting this as additional items:

DateTime Electricity_LastUpdate <energy> { http="<[smartthingsbulb:30000:JSONPATH($.energyMeter.energy.timestamp)]" }
DateTime Gas_LastUpdate <fire> { http="<[smartthingsbulb:30000:JSONPATH($.gasMeter.gasMeter.timestamp)]" }

Graphing

I use JDBC MariaDB (aka MySQL) as my Persistence service, and Grafana as a viewing tool.

I have written a small piece of custom SQL to show the difference between each update - for electricity, this is generally every minute (although, it does skip a couple of minutes here and there), and for gas, every 30 minutes or so.

SELECT v.time as "time",
       (select v.value - v2.value
        from item0099 v2
        where v2.time < v.time
        order by v2.time desc
        limit 1
       ) as "Electricity"
FROM item0099 v
WHERE
  $__timeFilter(time)
ORDER BY time ASC

You will of course need to replace both instances of item0099 with the correct persisted item

Basically, this gets the values that fall within the current time-range, and then in a sub-query will retrieve the value of the previous entry, and subtract the two values. This gives the consumption between two data points.

Where the update spans more than an update period (a minute for electricity), unfortunately this appears as spikes in the graph - however, generally, it is giving a good rough estimation as to the power consumption on a per-minute basis for electricity, and per 30 minutes for gas.

I hope this helps someone else out!

3 Likes

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.