Read British Gas Hive Heating temperature

In this subforum you can show projects you have made, or you are busy with. Please create your own topic.

Moderator: leecollings

sach
Posts: 111
Joined: Wednesday 12 October 2016 14:33
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Read British Gas Hive Heating temperature

Post by sach »

SDISDI wrote:Hi. I'm wondering is something has changed at the server end. I have just noticed that mine stopped working around 1600 on 4th January. It has been fine since March last year. I can login to my.hivehome.com so it seems that the service is generally up.

If I run the script manually I get

Code: Select all

pi@raspberrypi:~/domoticz/scripts$ python hive2domo.py 
503
503
Traceback (most recent call last):
  File "hive2domo.py", line 76, in <module>
    jsonData = json.loads(body)
  File "/usr/lib/python2.7/json/__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
TypeError: expected string or buffer
Interestingly the hive_boost script still runs for me.
Exactly the same error at exactly the same time for me too.
I have however found a nice workaround which works for me....especially with my lack of scripting skills and therefore inability to troubleshoot problems like these.

I was basically using Hive as a boiler switch by changing the setpoint depending on whether heat was requested by thermostats located in most of the rooms in my house. I have managed to use IFTTT Maker to change the setpoint on Hive via a virtual switch. 23degrees for on and 15degrees to off. :-)
MikeF
Posts: 350
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.2
Location: UK
Contact:

Re: Read British Gas Hive Heating temperature

Post by MikeF »

sach wrote:I have the same issue and I'm using the old Version 5 scripts.
Hopefully its just a blip on Hive's end. Seems like it stopped working around 4pm today for me.
Unfortunately I've not been using the v5 API python script for a while now, as I changed over to the v6 API when I upgraded to Hive 2 - and wrote the bash scripts for this, so I'm afraid I no longer know what works on v5 any more.
MikeF
Posts: 350
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.2
Location: UK
Contact:

Re: Read British Gas Hive Heating temperature

Post by MikeF »

andy0887 wrote:I have been trying to setup the scripts in line with the post Tuesday 13 December 2016 16:04 but am getting the following errors:

Traceback (most recent call last):
File "<string>", line 1, in <module>
KeyError: 'temperature'
Traceback (most recent call last):
File "<string>", line 1, in <module>
KeyError: 'reportedValue'
Traceback (most recent call last):
File "<string>", line 1, in <module>
KeyError: 'stateHeatingRelay'

Are you able to help? When I run the script the devices move up the list in domoticz so it's doing something.
I think this is where my bash script is not too robust :? In the line where I define temp1 (line 21?), I have specified element 2 in the 'nodes' list (["nodes"][2]) as the location for 'temperature', 'reportedValue' and 'stateHeatingRelay'. Obviously this is hard-coded, and this may change from one Hive installation to another (I don't fully understand the Hive API!).

To check this out, you could insert the following around line 18:

Code: Select all

echo $nodes | python -m json.tool
- this will give you a formatted print of the 'nodes' variable, and you could then check to see if these variables do occur in element 2 (or a different one) - remember, list elements start counting from 0.

Alternatively, if you are using Firefox, you can 'Inspect Element' (right-click on your Hive web page) and look for a '200 GET nodes' entry; click on the little arrow ('Show request details'), then click on the Response tab - you'll get a better formatted list.
Also I normally have security enabled on my domoticz but in the script for Hive API v6 I can't see this?
Unfortunately I've never enabled security on Domoticz, so I've not implemented it in my script - can't help you here, I'm afraid.
MikeF
Posts: 350
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.2
Location: UK
Contact:

Re: Read British Gas Hive Heating temperature

Post by MikeF »

sach wrote: Exactly the same error at exactly the same time for me too.
[speculate]I'm wondering if BG have discontinued / changed the v5 API? [/speculate]
I have managed to use IFTTT Maker to change the setpoint on Hive via a virtual switch. 23degrees for on and 15degrees to off. :-)
I notice that Hive have launched an IFTTT channel, which enables you to create recipes, which may allow you to do the same thing? Haven't tried it though.
andy0887
Posts: 3
Joined: Tuesday 03 January 2017 23:40
Target OS: Raspberry Pi / ODroid
Domoticz version: v3.5877
Location: Halifax, UK
Contact:

Re: Read British Gas Hive Heating temperature

Post by andy0887 »

Hi Mike,

Thanks for the response I've gone through that and worked out my node is actually 4 and have changed the script and now have got alot further however I've come across another issue if you could help?

./hivethermo.sh: line 41: [: 21.0: unary operator expected
./hivethermo.sh: line 50: [: Off: unary operator expected

Is this possible the devices which I've set up wrong?

In response to security I'll scratch my head on that I'm sure someone will be running a script with security so I can just pinch parts from one of those once I've got this one fully working.

Thanks

Andy
MikeF
Posts: 350
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.2
Location: UK
Contact:

Re: Read British Gas Hive Heating temperature

Post by MikeF »

Hello Andy,

This was a real kludge on my part - in an attempt to avoid sending the same value many times to the Domoticz database for target and heating, when these haven't changed. I created 2 files, target.txt and heating.txt, containing the values 'Off' and '21.0' respectively (without the quotes) - these need to reside in the same directory as the script. Every time the script runs, it reads the last value from the file, checks the current value against it, and only executes the curl command to send data to Domoticz if different; it then writes out the current value to the file. (There's probably a much better way to do this!)

If you're not concerned about this, you can always delete lines 40-41, 43-44 and 48-50, 52-53 (assuming the ine numbers in the original script haven't changed).

HTH,

Mike
andy0887
Posts: 3
Joined: Tuesday 03 January 2017 23:40
Target OS: Raspberry Pi / ODroid
Domoticz version: v3.5877
Location: Halifax, UK
Contact:

Re: Read British Gas Hive Heating temperature

Post by andy0887 »

Hi Mike,

That's worked a dream and it's all working now at reading. My domoTemp and domoSetPoint are showing the same values but I'm sure that's me that's done something wrong so will work with that but the boiler switch is now showing correctly and the others are showing right which is excellent.

Thanks for your support.
SDISDI
Posts: 17
Joined: Wednesday 09 March 2016 9:18
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.4834
Contact:

Re: Read British Gas Hive Heating temperature

Post by SDISDI »

Hi Mike. For some reason I'd never got around to upgrading to your v6 scripts, so all done now, and along with the hints you gave to Andy I'm all working now. BG must had broken v5 API hence our scripts stopped working. Thanks again. I am using the Hive actual temperature to trigger the little heater in our Cat's bed which hasn't been working for a few days, so he is very grateful to you!!
RFXtrx433E
Hive 2 Active Heating
Owl CM180 Energy Monitor
Various HomeEasy Receivers
LightwaveRF Plug-in sockets
Calzor Suzay
Posts: 145
Joined: Tuesday 08 July 2014 15:10
Target OS: -
Domoticz version: 4.9700
Location: UK
Contact:

Re: Read British Gas Hive Heating temperature

Post by Calzor Suzay »

andy0887 wrote:My domoTemp and domoSetPoint are showing the same values but I'm sure that's me that's done something wrong so will work with that but the boiler switch is now showing correctly and the others are showing right which is excellent.
That's because there's a typo in the original code showing the same variable, make it like below with targetTemp

Code: Select all

domoTemp=$(curl -s "http://<DOMO URL>:<PORT>/json.htm?type=command&param=udevice&nvalue=0&idx=<CURR TEMP>&svalue=${temp}")
domoSetPoint=$(curl -s "http://<DOMO URL>:<PORT>/json.htm?type=command&param=udevice&nvalue=0&idx=<SETPOINT>&svalue=${targetTemp}")
It should then pick up the correct temp.

I had the same hard coded node issue so just changed mine to 0 then 1 at which point it picked the temperature up :idea:

Also for anyone else that runs their site as https only without a certificate the curl commands need a -k option for 'insecure'.
I'm working on the certificate bit myself so I don't have warning message anymore but all in good time :)

Otherwise excellent script cheers Mike :)
Calzor Suzay
Posts: 145
Joined: Tuesday 08 July 2014 15:10
Target OS: -
Domoticz version: 4.9700
Location: UK
Contact:

Re: Read British Gas Hive Heating temperature

Post by Calzor Suzay »

BTW in the app and website the outside temp is displayed, does anyone know if this is exposed in the API to be read in as well.
MikeF
Posts: 350
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.2
Location: UK
Contact:

Re: Read British Gas Hive Heating temperature

Post by MikeF »

Calzor Suzay wrote: That's because there's a typo in the original code showing the same variable...
I deliberately wrote it that way - I wanted the setpoint to display the current temp, but I agree, you can change it to show targetTemp instead.
Otherwise excellent script cheers Mike :)
You're welcome - many thanks! :D
MikeF
Posts: 350
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.2
Location: UK
Contact:

Re: Read British Gas Hive Heating temperature

Post by MikeF »

Calzor Suzay wrote:BTW in the app and website the outside temp is displayed, does anyone know if this is exposed in the API to be read in as well.
Using my old friend (!) 'Inspect Element' in Firefox, I find the following call:

Code: Select all

https://weather-prod.bgchprod.info/weather?postcode=XXXXXX
(replace with your postcode)

Type this in a browser...
...I'll leave the rest as an exercise for the reader!
MikeF
Posts: 350
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.2
Location: UK
Contact:

Re: Read British Gas Hive Heating temperature

Post by MikeF »

As a 'companion' to my Hive boost script (hive_boost.sh) posted earlier, I've created a boost cancel script:
Spoiler: show

Code: Select all

#!/bin/bash
# Hive Cancel Boost function

login=$(curl  -s -k --cookie-jar cookie.jar -g -H "Content-Type: application/vnd.alertme.zoo-6.1+json" \
	-H "Accept: application/vnd.alertme.zoo-6.2+json" -H "Content-Type: 'application/*+json'" \
	-H "X-AlertMe-Client: Hive Web Dashboard" \
	-d '{"sessions":[{"username":"<HIVE USERNAME>", "password":"<HIVE PASSWORD>"}]}' \
	https://api.prod.bgchprod.info:443/omnia/auth/sessions)

sessionId=$(echo $login | python -c 'import sys, json; print json.load(sys.stdin)["sessions"][0]["sessionId"]')

nodes=$(curl  -s -k --cookie-jar cookie.jar -g -H "Content-Type: application/vnd.alertme.zoo-6.2+json" \
	-H "Accept: application/vnd.alertme.zoo-6.2+json" -H "Content-Type: 'application/*+json'" \
	-H "X-AlertMe-Client: swagger" -H 'X-Omnia-Access-Token: '"$sessionId" \
	https://api.prod.bgchprod.info:443/omnia/nodes)

nodeId=$(echo $nodes | python -c 'import sys, json; print json.load(sys.stdin)["nodes"][2]["id"]')

cancel=$(curl  -s -k --cookie-jar cookie.jar -X PUT -H "Content-Type: application/vnd.alertme.zoo-6.2+json" \
	-H "Accept: application/vnd.alertme.zoo-6.2+json" -H "Content-Type: 'application/*+json'" \
	-H "X-AlertMe-Client: swagger" -H 'X-Omnia-Access-Token: '"$sessionId" \
	-d '{"nodes":[{"attributes":{"activeHeatCoolMode":{"targetValue":"HEAT"},"activeScheduleLock":{"targetValue":false}}}]}' \
	"https://api.prod.bgchprod.info:443/omnia/nodes/${nodeId}")

logout=$(curl -s -k -X DELETE --cookie-jar cookie.jar -g -H "Content-Type: application/vnd.alertme.zoo-6.1+json" \
	-H "Accept: application/vnd.alertme.zoo-6.2+json" -H "Content-Type: 'application/*+json'" \
	-H "X-AlertMe-Client: Hive Web Dashboard" -H 'X-Omnia-Access-Token: '"$sessionId" \
	"https://api.prod.bgchprod.info:443/omnia/auth/sessions/${sessionId}")
I've set the two scripts up on a Home Easy 2-button fob - hopefully, this will gain high WAF! :lol:
MikeF
Posts: 350
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.2
Location: UK
Contact:

Re: Read British Gas Hive Heating temperature

Post by MikeF »

While rootling through the Hive v6 API (using Firefox's Inspect Element), I discovered that the login URL has changed to https://beekeeper.hivehome.com/1.0/gateway/login, although the previous login still works. I managed to get the following to work:

Code: Select all

login=$(curl -s -k -g -H "Content-Type: application/json" \
	-H "Accept: application/json"  \
	-d '{"username":"<HIVE USERNAME>","password":"<HIVE PASSWORD>"}' \
	https://beekeeper.hivehome.com/1.0/gateway/login)
	
sessionId=$(echo $login | python -c 'import sys, json; print json.load(sys.stdin)["token"]')	
I tidied this up by removing (apparently) unnecessary headers.

I COULD NOT get this to work under OS X :x - I kept getting MALFORMED_REQUEST errors - but it worked without amendment under Raspbian on my RPi - clearly some inconsistency (feature? bug??) in OS X cURL.
Calzor Suzay
Posts: 145
Joined: Tuesday 08 July 2014 15:10
Target OS: -
Domoticz version: 4.9700
Location: UK
Contact:

Re: Read British Gas Hive Heating temperature

Post by Calzor Suzay »

Is it possible to put some sort of error checking in if it reads a 0° temp?
I get blips whereby I guess it couldn't communicate properly or times out or something.
I found a way to delete the blip but means I need to check it daily and correct viewtopic.php?f=28&t=15369
MikeF
Posts: 350
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.2
Location: UK
Contact:

Re: Read British Gas Hive Heating temperature

Post by MikeF »

I guess you could do something like:

Code: Select all

if [ $temp != "0" ]; then
	domoTemp=$(curl -s "http://<DOMO IP:PORT>/json.htm?type=command&param=udevice&nvalue=0&idx=<IDX>&svalue=${temp}")
fi
Haven't tried it though.
Calzor Suzay
Posts: 145
Joined: Tuesday 08 July 2014 15:10
Target OS: -
Domoticz version: 4.9700
Location: UK
Contact:

Re: Read British Gas Hive Heating temperature

Post by Calzor Suzay »

Just changed it so we'll see how it goes, cheers :)
mehtadone
Posts: 8
Joined: Sunday 15 January 2017 11:38
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Read British Gas Hive Heating temperature

Post by mehtadone »

Massive thanks to @mikeF. Everything is working like a charm. Below are my scripts for hot water and my home plugs which work in a similar method. The key to these was finding the node ID. I did this through firefox as @mikeF suggested. In the below scripts, I've added a <NODE> parameter which you will have to fill in after you have found out the node you want to control.

The water and heating nodes seem to use the same attributes on the API. This is how I found out the node for the water. For mine it was 2 for heating and 3 for the water. I just had to search for "zone" in the response to find this out.

https://www.dropbox.com/s/eq8wxv68hpwd3 ... 8.png?dl=0

For the security plugs, to identify the node name was slight different. It was the "name" of the node that allowed me to identify the node for the plug.

https://www.dropbox.com/s/muv7szrzehdrx ... 6.png?dl=0

Boost Water

Code: Select all

#!/bin/bash
# Hive Boost function for water

login=$(curl -s -k -g -H "Content-Type: application/json" \
   -H "Accept: application/json"  \
   -d '{"username":"<HIVE USERNAME>","password":"<HIVE PASSWORD>"}' \
   https://beekeeper.hivehome.com/1.0/gateway/login)

sessionId=$(echo $login | python -c 'import sys, json; print json.load(sys.stdin)["token"]')

nodes=$(curl  -s -k --cookie-jar cookie.jar -g -H "Content-Type: application/vnd.alertme.zoo-6.2+json" \
   -H "Accept: application/vnd.alertme.zoo-6.2+json" -H "Content-Type: 'application/*+json'" \
   -H "X-AlertMe-Client: swagger" -H 'X-Omnia-Access-Token: '"$sessionId" \
   https://api.prod.bgchprod.info:443/omnia/nodes)

nodeId=$(echo $nodes | python -c 'import sys, json; print json.load(sys.stdin)["nodes"][<NODE>]["id"]')

boost=$(curl -s -k "http://192.168.1.4:8080/json.htm?type=command&param=getuservariable&idx=2")
boostTime=$(echo $boost | python -c 'import sys, json; print json.load(sys.stdin)["result"][0]["Value"]')

boost=$(curl  -s -k --cookie-jar cookie.jar -X PUT -H "Content-Type: application/vnd.alertme.zoo-6.2+json" \
   -H "Accept: application/vnd.alertme.zoo-6.2+json" -H "Content-Type: 'application/*+json'" \
   -H "X-AlertMe-Client: swagger" -H 'X-Omnia-Access-Token: '"$sessionId" \
   -d '{"nodes":[{"attributes":{"activeHeatCoolMode":{"targetValue":"BOOST"},"scheduleLockDuration":{"targetValue":"'"$boostTime"'"}}}]}' \
   "https://api.prod.bgchprod.info:443/omnia/nodes/${nodeId}")

logout=$(curl -s -k -X DELETE --cookie-jar cookie.jar -g -H "Content-Type: application/vnd.alertme.zoo-6.1+json" \
   -H "Accept: application/vnd.alertme.zoo-6.2+json" -H "Content-Type: 'application/*+json'" \
   -H "X-AlertMe-Client: Hive Web Dashboard" -H 'X-Omnia-Access-Token: '"$sessionId" \
   "https://api.prod.bgchprod.info:443/omnia/auth/sessions/${sessionId}")
Boost water cancel is the same as the boost heating cancel script with a different node ID.

Home plug ON

Code: Select all

#!/bin/bash
# Hive Boost function (change duration and target temperature in boost curl)

login=$(curl -s -k -g -H "Content-Type: application/json" \
   -H "Accept: application/json"  \
   -d '{"username":"<HIVE USERNAME>","password":"<HIVE PASSWORD>"}' \
   https://beekeeper.hivehome.com/1.0/gateway/login)
sessionId=$(echo $login | python -c 'import sys, json; print json.load(sys.stdin)["token"]')

nodes=$(curl  -s -k --cookie-jar cookie.jar -g -H "Content-Type: application/vnd.alertme.zoo-6.2+json" \
   -H "Accept: application/vnd.alertme.zoo-6.2+json" -H "Content-Type: 'application/*+json'" \
   -H "X-AlertMe-Client: swagger" -H 'X-Omnia-Access-Token: '"$sessionId" \
   https://api.prod.bgchprod.info:443/omnia/nodes)

nodeId=$(echo $nodes | python -c 'import sys, json; print json.load(sys.stdin)["nodes"][<NODE>]["id"]')

boost=$(curl  -s -k --cookie-jar cookie.jar -X PUT -H "Content-Type: application/vnd.alertme.zoo-6.2+json" \
   -H "Accept: application/vnd.alertme.zoo-6.2+json" -H "Content-Type: 'application/*+json'" \
   -H "X-AlertMe-Client: swagger" -H 'X-Omnia-Access-Token: '"$sessionId" \
   -d '{"nodes":[{"attributes":{"state":{"targetValue":"ON"}}}]}' \
   "https://api.prod.bgchprod.info:443/omnia/nodes/${nodeId}")

logout=$(curl -s -k -X DELETE --cookie-jar cookie.jar -g -H "Content-Type: application/vnd.alertme.zoo-6.1+json" \
   -H "Accept: application/vnd.alertme.zoo-6.2+json" -H "Content-Type: 'application/*+json'" \
   -H "X-AlertMe-Client: Hive Web Dashboard" -H 'X-Omnia-Access-Token: '"$sessionId" \
   "https://api.prod.bgchprod.info:443/omnia/auth/sessions/${sessionId}")
Home plug off:

Code: Select all

#!/bin/bash
# Hive Boost function (change duration and target temperature in boost curl)

login=$(curl -s -k -g -H "Content-Type: application/json" \
   -H "Accept: application/json"  \
   -d '{"username":"<HIVE USERNAME>","password":"<HIVE PASSWORD>"}' \
   https://beekeeper.hivehome.com/1.0/gateway/login)

sessionId=$(echo $login | python -c 'import sys, json; print json.load(sys.stdin)["token"]')

nodes=$(curl  -s -k --cookie-jar cookie.jar -g -H "Content-Type: application/vnd.alertme.zoo-6.2+json" \
   -H "Accept: application/vnd.alertme.zoo-6.2+json" -H "Content-Type: 'application/*+json'" \
   -H "X-AlertMe-Client: swagger" -H 'X-Omnia-Access-Token: '"$sessionId" \
   https://api.prod.bgchprod.info:443/omnia/nodes)

nodeId=$(echo $nodes | python -c 'import sys, json; print json.load(sys.stdin)["nodes"][<NODE>]["id"]')

boost=$(curl  -s -k --cookie-jar cookie.jar -X PUT -H "Content-Type: application/vnd.alertme.zoo-6.2+json" \
   -H "Accept: application/vnd.alertme.zoo-6.2+json" -H "Content-Type: 'application/*+json'" \
   -H "X-AlertMe-Client: swagger" -H 'X-Omnia-Access-Token: '"$sessionId" \
   -d '{"nodes":[{"attributes":{"state":{"targetValue":"OFF"}}}]}' \
   "https://api.prod.bgchprod.info:443/omnia/nodes/${nodeId}")

logout=$(curl -s -k -X DELETE --cookie-jar cookie.jar -g -H "Content-Type: application/vnd.alertme.zoo-6.1+json" \
   -H "Accept: application/vnd.alertme.zoo-6.2+json" -H "Content-Type: 'application/*+json'" \
   -H "X-AlertMe-Client: Hive Web Dashboard" -H 'X-Omnia-Access-Token: '"$sessionId" \
   "https://api.prod.bgchprod.info:443/omnia/auth/sessions/${sessionId}")
Again, huge thanks to Mike for these scripts.
MikeF
Posts: 350
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.2
Location: UK
Contact:

Re: Read British Gas Hive Heating temperature

Post by MikeF »

mehtadone wrote:Massive thanks to @mikeF.
You're welcome - glad my efforts have been useful! :D :D

I haven't yet worked out how to 'automatically' determine the node number - this can change, if you add more Hive devices (happened to me, when I added a Hive light).
MikeF
Posts: 350
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.2
Location: UK
Contact:

Re: Read British Gas Hive Heating temperature

Post by MikeF »

I'm interested to hear from other users of these scripts how often you run them?

I currently run then every 5 minutes (via crontab), but I would like to make them more 'responsive' - e.g., when heating status changes, there can be a delay of up to 5 minutes before Domoticz is updated.

I've thought of running them every minute, and only sending values to Domoticz if they've changed, but this means logging on & off to Hive every minute. I'm also rewriting them in Python, using requests, and I thought about using sleep, but this would mean staying logged on to Hive continuously.

What do you do / think?
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest