MQTT 2.x Binding, Reading JSON data via MQTT Data from Tibber - MiniHowto using Paper-UI

Hello,

I had initially some issues with the MQTT 2.x Binding and Tibber. Actually I have spent quite alot of time reading howtos, trying, failing etc. Hopefully this little guide will be useful for you, if you:

  1. want to read Tibber Energy Price into openhab2
  2. you want to learn a generic approach to import JSON into MQTT into OpenHab2

My goal was to get electric energy price in OpenHab2 using MQTT and trend them using Grafana. I archieved this goal :slight_smile:

A little background about the stuff involved:
What is Tibber: (norwegian) Power Company that provides electric energy price for the next hour via API (…). Smart(…)
What is MQTT: Message broker that allows communication via “publish” and “subscribe” methods.

Prequisites:

  1. Have a Tibber Account
  2. Installed and working MQTT openhab2 Binding (2.x) - if not: should be able to follow my steps here: Trouble with MQTT Bindings 2.x - eventsTriggered - Item stale

Lets go:

  1. Activate Tibber API. You need the authorization token
    Google it or start here : https://developer.tibber.com/docs/guides/calling-api

  2. Create a script that communicates to API

Script that fetches energy price for the next hour and sends it to MQTT with subject "/TibberData"

#!/bin/bash
#Communication with Tibber
data=$(curl \-H "Authorization: Bearer OpenHabCommunityPostSendAllTheMoneyToChristianHehe" \-H "Content-Type: application/json" \-X POST \-d  '{ "query": "{viewer {homes {currentSubscription {priceInfo {current {total energy tax startsAt }}}}}}" }' https://api.tibber.com/v1-beta/gql)

tibberCurlErr=$?
if [ $tibberCurlErr -gt 0 ]; then
    echo "CURL failed to Tibber error code $tibberCurlErr" | logger
    exit 53
fi
echo "$data"
# Getting data to MQTT broker
mosquitto_pub -t /TibberData -m "$data"

Validation / Output example:

user@openhab:/usr/local/bin/openhab# ./tibber_hourly_price.sh
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   271  100   167  100   104    480    299 --:--:-- --:--:-- --:--:--   479
{"data":{"viewer":{"homes":[{"currentSubscription":{"priceInfo":{"current":{"total":0.6031,"energy":0.4825,"tax":0.1206,"startsAt":"2018-12-08T10:00:00+01:00"}}}}]}}}
  1. Add a new Thing for this channel.

Configuration > Things > (+) > MQTTBinding > manually add > Generic MQTT Thing
Details see screenshot, basically give it a name and select your broker.


Note: If there is no broker - go back to PREREQUISITES(2) and fix it :slight_smile:

  1. Configure Channels for your input data. Examples below

    As you can see I have created two channels - one for the “price” (TibberDataNumber) and the StartDate (usually the next hour).


The json transformation that I used is

JSONPATH:$.data.viewer.homes[0].currentSubscription.priceInfo.current.total

Here is the second channel for the start time of the Energy Price:

The json transformation that I used is:

JSONPATH:$.data.viewer.homes[0].currentSubscription.priceInfo.current.startsAt

I wasn’t so used to writing JSON path transformations, but I had great help by using jqplay. A website that lets you test the transformations. Link: https://jqplay.org/s/rBaLqpPLpU - note that there is a slight syntax difference in the start. :wink: (You don’t have to use this website - just as an example if you want to derive work off my little howto…)

.
Almost done - linking the MQTTBinding Channels to Items:

Example Item config:

If everything worked, you should be able to see something like this in Paper-UI Control:

image

Yay! You are done!!!

I use the data above to be trended in Grafana:

Sample Query config:

“If you found this post useful, please click the subscribe button…” :crazy_face:

Troubleshoot only: If you have something like “NAN” on your Items - then try this:
0) Example of my /var/log/openhab2/events.log

2018-12-08 06:00:18.207 [vent.ItemStateChangedEvent] - TibberHourlyEnergyPrice_TibberDataNumber changed from 0.5539 to 0.5666
  1. rerun your script that fetches data from Tibber (you see the JSON there- right?)
  2. Subscribe to MQTT yourself to validate that you see the data
mosquitto_sub -v -t '/TibberData' -d

Should show something like this:

Client mosqsub/13665-openhab received PUBLISH (d0, q0, r0, m0, '/TibberData', ... (166 bytes))
/TibberData {"data":{"viewer":{"homes":[{"currentSubscription":{"priceInfo":{"current":{"total":0.6031,"energy":0.4825,"tax":0.1206,"startsAt":"2018-12-08T10:00:00+01:00"}}}}]}}}
5 Likes

I normally use jsonpath.com
No syntax difference

Do you mean the like :heart: button?

Hehe… just tried to be funny :). But yes- the like button works for me ! :slight_smile:

1 Like

Sorry, no www

1 Like