Successfully Integrating Tibber Energy Data into OpenHAB2 with MQTT

I’m thrilled to share my successful journey of integrating Tibber energy data into OpenHAB2 using MQTT! :tada: After some trial and error, I managed to get real-time energy prices and trends up and running. Here’s a quick rundown of how I did it, in case you’re looking to do something similar!

Step 1: Activate Tibber API
First off, I needed to get my Tibber API token. If you’re not sure how, Tibber’s developer docs are a great starting point. Once I had the token, I was ready to fetch the data.

Step 2: Create a Bash Script
I wrote a simple bash script to pull the energy price data from Tibber’s API and publish it to MQTT. The script uses curl to communicate with Tibber and mosquitto_pub to send the data to my MQTT broker. Here’s a snippet of the script:

bash

Fetch Tibber data

data=$(curl -H “Authorization: Bearer YOUR_API_TOKEN” -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)

Publish to MQTT

mosquitto_pub -t /TibberData -m “$data”

Step 3: Add a New Thing in OpenHAB2
In OpenHAB2, I added a new Generic MQTT Thing. I configured it to connect to my MQTT broker and set up channels to receive the Tibber data. I used JSONPATH transformations to extract the relevant values from the JSON payload. For example:

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

Step 4: Link to Items and Visualize
Finally, I linked the MQTT channels to OpenHAB2 items and set up a simple trend graph in Grafana. It’s super satisfying to see the energy prices update in real-time!

If you’re looking to track your energy costs or optimize your usage, I highly recommend giving this a try! Let me know if you have any questions or need help along the way. Happy automating! :rocket: