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.
Calzor Suzay
Posts: 112
Joined: Tuesday 08 July 2014 15:10
Target OS: Raspberry Pi
Domoticz version: 3.5877
Location: UK
Contact:

Re: Read British Gas Hive Heating temperature

Post by Calzor Suzay » Sunday 15 January 2017 17:07

Every five minutes is good for me as I just use it as a reporting tool, making it a continuous feed would obviously benefit people who want to act upon it via Domoticz. The only thing I can think might be an issue would BGas feel their servers are being spammed and limit connections over time.
Not sure as you can leave the web page open and that updates itself as it goes :)

Calzor Suzay
Posts: 112
Joined: Tuesday 08 July 2014 15:10
Target OS: Raspberry Pi
Domoticz version: 3.5877
Location: UK
Contact:

Re: Read British Gas Hive Heating temperature

Post by Calzor Suzay » Monday 16 January 2017 17:18

MikeF wrote: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.
I can see the logic in the code but I still get the 0° blips so not sure how it's getting them.

aturi83
Posts: 5
Joined: Tuesday 07 November 2017 17:27
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Read British Gas Hive Heating temperature

Post by aturi83 » Tuesday 07 November 2017 17:32

Hi just installed this into my new setup. When running the python script i get the following error:

Code: Select all

Traceback (most recent call last):
  File "/home/pi/domoticz/scripts/hive2domo.py", line 70, in <module>
    makeRequest(url,payload)
  File "/home/pi/domoticz/scripts/hive2domo.py", line 41, in makeRequest
    print e.code
AttributeError: 'URLError' object has no attribute 'code'
I think some of the URL's have changed from the original ones posted. Any tips on where to start? I have Hive mk2 (the shiny one!)

MikeF
Posts: 130
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi
Domoticz version: V3.5877
Location: UK
Contact:

Re: Read British Gas Hive Heating temperature

Post by MikeF » Wednesday 08 November 2017 0:42

Yep, Hive API / URLs changed a while back.

Try this (replace <XXXX>'s with your values):
Spoiler: show

Code: Select all

#!/usr/bin/env python

'''
Python script to send data from Hive to Domoticz.

Replace XXX's with your values.
'''

import requests
import json

# Replace with your Domoticz idx's
insideIdx = '<XX>'
targetIdx = '<XX>'
thermIdx = '<XX>'
heatingIdx = '<XX>'

# Replace with your Domoticz URL & port
DomoReadURL   = 'http://<XXXX:XXXX>/json.htm?type=devices&rid='
DomoWriteURL  = 'http://<XXXX:XXXX>/json.htm?type=command&param=udevice&nvalue=0&idx='
DomoSwitchURL = 'http://<XXXX:XXXX>/json.htm?type=command&param=switchlight&idx='

requests.packages.urllib3.disable_warnings()

def domoticzread(idx, var):
   url = DomoReadURL + idx
   response = requests.get(url)
   jsonData = json.loads(response.text)
   result = jsonData['result'][0][var]
   return result;

# Hive

# log on to Hive
# Replace with your Hive user id and password
payload = {'username':'<XXXX>', 'password':'<XXXX>'}
headers = {'Content-Type': 'application/json', 'Accept': 'application/json'}
url = 'https://beekeeper-uk.hivehome.com/1.0/gateway/login'
r = requests.post(url, headers=headers, data=json.dumps(payload), verify=False)

#print(json.dumps(r.json(), indent=2))

sessionId = r.json()["token"]
#print sessionId

headers = {'Content-Type': 'application/vnd.alertme.zoo-6.2+json', 'Accept': 'application/vnd.alertme.zoo-6.2+json', \
	'X-AlertMe-Client': 'swagger', 'X-Omnia-Access-Token': sessionId}
url = 'https://api.prod.bgchprod.info:443/omnia/nodes'
r = requests.get(url, headers=headers, verify=False)

#print(json.dumps(r.json()["nodes"][2], indent=2))

# read node (Note: node number may be different / may change if new Hive devices added)
nodeId=r.json()["nodes"][2]["id"]
#print nodeId

# get values from Hive
temp = r.json()["nodes"][2]["attributes"]["temperature"]["reportedValue"]
targetTemp = r.json()["nodes"][2]["attributes"]["targetHeatTemperature"]["reportedValue"]
heating = r.json()["nodes"][2]["attributes"]["stateHeatingRelay"]["reportedValue"]

# log out from Hive
headers = {'Content-Type': 'application/vnd.alertme.zoo-6.1+json', 'Accept': 'application/vnd.alertme.zoo-6.2+json', \
	'X-AlertMe-Client': 'Hive Web Dashboard', 'X-Omnia-Access-Token': sessionId}
url = 'https://api.prod.bgchprod.info:443/omnia/auth/sessions/' + sessionId
r = requests.delete(url, headers=headers, verify=False)

# Domoticz

# get last values from Domoticz
last_temp = domoticzread(insideIdx, 'Temp')
last_target = domoticzread(targetIdx, 'Temp')
last_heating = domoticzread(heatingIdx, 'Status')

# change some values
temp = round(temp, 1)

if targetTemp < 7.0: targetTemp = 7.0

if heating == "ON":
	heating="On"
else:
	heating="Off"

print 'new','old'
print temp, last_temp
print targetTemp, last_target
print heating, last_heating

# send values to Domoticz (only send some if changed)
url = DomoWriteURL + insideIdx + '&svalue=' + str(temp)
r = requests.get(url)

if targetTemp != last_target:
	url = DomoWriteURL + targetIdx + '&svalue=' + str(targetTemp)
	r = requests.get(url)

	url = DomoWriteURL + thermIdx + '&svalue=' + str(targetTemp)
	r = requests.get(url)

if heating != last_heating:
	url = DomoSwitchURL + heatingIdx + '&switchcmd=' + heating
	r = requests.get(url)
	

aturi83
Posts: 5
Joined: Tuesday 07 November 2017 17:27
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Read British Gas Hive Heating temperature

Post by aturi83 » Sunday 12 November 2017 23:00

MikeF wrote:
Wednesday 08 November 2017 0:42
Yep, Hive API / URLs changed a while back.

Try this (replace <XXXX>'s with your values):
I must have some kind of bash config problem. Added the configs into it and now have:

Code: Select all

-bash: $'\nPython script to send data from Hive to Domoticz.\n\nReplace XXXs with your values.\n': command not found
-bash: import: command not found
-bash: import: command not found
-bash: insideIdx: command not found
-bash: targetIdx: command not found
-bash: thermIdx: command not found
-bash: heatingIdx: command not found
-bash: DomoReadURL: command not found
-bash: DomoWriteURL: command not found
-bash: DomoSwitchURL: command not found
-bash: hive.bash: line 25: syntax error near unexpected token `def'
-bash: hive.bash: line 25: `def domoticzread(idx, var):''
Made a hello world script, which executes correctly. Not sure whats going on. Sorry to be a pain.

aturi83
Posts: 5
Joined: Tuesday 07 November 2017 17:27
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Read British Gas Hive Heating temperature

Post by aturi83 » Monday 13 November 2017 20:58

Ok after working through some things - had to install module "requests" I now have the following error

Code: Select all

Traceback (most recent call last):
  File "./hive.sh", line 58, in <module>
    temp = r.json()["nodes"][2]["attributes"]["temperature"]["reportedValue"]
KeyError: 'temperature'

MikeF
Posts: 130
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi
Domoticz version: V3.5877
Location: UK
Contact:

Re: Read British Gas Hive Heating temperature

Post by MikeF » Monday 13 November 2017 21:36

Hi @aturi83,

The problem with this script is that I never worked out how to detect which node corresponds to the Hive receiver. For me, it's node 2, but it depends on what else is on your Hive system.

In the 3 lines following '# get values from Hive', try changing the number [2] (e.g., to [1] or [3]) - it's a little bit hit and miss, I'm afraid. :?

aturi83
Posts: 5
Joined: Tuesday 07 November 2017 17:27
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Read British Gas Hive Heating temperature

Post by aturi83 » Monday 13 November 2017 23:43

Took a few attempts but managed to track down the correct ID. Dont make it easy this stuff do they...

A new set of error messages at the end. However got lots more data now, including schedule, frost protection temp settings etc.

Code: Select all

Traceback (most recent call last):
  File "./hive.sh", line 71, in <module>
    last_temp = domoticzread(insideIdx, 'Temp')
  File "./hive.sh", line 28, in domoticzread
    jsonData = json.loads(response.text)
  File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

MikeF
Posts: 130
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi
Domoticz version: V3.5877
Location: UK
Contact:

Re: Read British Gas Hive Heating temperature

Post by MikeF » Tuesday 14 November 2017 20:59

I think the problem here may be that you have not set up dummy devices in Domoticz to receive the values from this script - see lines 13 to 16. The first two are temperature devices, the third is a thermostat / setpoint, and the last is an on/off switch. (I may have mentioned this originally - it was a while ago when I first published this script.)

aturi83
Posts: 5
Joined: Tuesday 07 November 2017 17:27
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Read British Gas Hive Heating temperature

Post by aturi83 » Wednesday 15 November 2017 0:43

MikeF wrote:
Tuesday 14 November 2017 20:59
I think the problem here may be that you have not set up dummy devices in Domoticz to receive the values from this script - see lines 13 to 16. The first two are temperature devices, the third is a thermostat / setpoint, and the last is an on/off switch. (I may have mentioned this originally - it was a while ago when I first published this script.)
Setup as described, in fact i removed the dummy device and set it up from scratch.

The data above the error is correct

Code: Select all

    "targetHeatTemperature": {
      "targetExpiryTime": 1510697424984, 
      "reportReceivedTime": 1510698605897, 
      "propertyStatus": "COMPLETE", 
      "targetSetTXId": "e56e60fa-0d45-4c52-9f76-7029377109ae", 
      "reportedValue": 11.5, 
      "targetSetTime": 1510697124984, 
      "displayValue": 11.5, 
      "targetValue": 11.5, 
      "reportChangedTime": 1510697125488
    }, 
    "minHeatTemperature": {
      "displayValue": 5.0, 
      "reportReceivedTime": 1510698605897, 
      "reportChangedTime": 1508372857836, 
      "reportedValue": 5.0
    }, 
    "holidayMode": {
      "displayValue": {
        "targetHeatTemperature": 1.0
      }, 
      "reportReceivedTime": 1510698605897, 
      "reportChangedTime": 1509790193862, 
      "reportedValue": {
        "targetHeatTemperature": 1.0
      }
    }, 
    "failureStatus": {
      "displayValue": "NORMAL", 
      "reportReceivedTime": 1510698605897, 
      "reportChangedTime": 1509633035417, 
      "reportedValue": "NORMAL"
    }, 
    "nodeType": {
      "displayValue": "http://alertme.com/schema/json/node.class.thermostat.json#", 
      "reportReceivedTime": 1510699203908, 
      "reportChangedTime": 1508372853584, 
      "reportedValue": "http://alertme.com/schema/json/node.class.thermostat.json#"
    }, 
    "activeScheduleLock": {
      "targetExpiryTime": 1510605381449, 
      "reportReceivedTime": 1510698605897, 
      "propertyStatus": "COMPLETE", 
      "targetSetTXId": "7cbb4270-0380-4963-b7ed-2fdef9de06ea", 
      "reportedValue": true, 
      "targetSetTime": 1510605081449, 
      "displayValue": true, 
      "targetValue": true, 
      "reportChangedTime": 1510656121698
    }, 
    "activeHeatCoolMode": {
      "targetExpiryTime": 1510605381449, 
      "reportReceivedTime": 1510698605897, 
      "propertyStatus": "COMPLETE", 
      "targetSetTXId": "7cbb4270-0380-4963-b7ed-2fdef9de06ea", 
      "reportedValue": "HEAT", 
      "targetSetTime": 1510605081449, 
      "displayValue": "HEAT", 
      "targetValue": "BOOST", 
      "reportChangedTime": 1510608681517
    }, 
    "frostProtectTemperature": {
      "displayValue": 7.0, 
      "reportReceivedTime": 1510698605897, 
      "reportChangedTime": 1508372857836, 
      "reportedValue": 7.0
    }
Its not getting passed into domoticz though. I'll keep playing :D

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests