Page 12 of 16

Re: Read British Gas Hive Heating temperature

Posted: Thursday 21 February 2019 0:04
by Calzor Suzay
With Beta 4.10445 with v1 it installs and runs.
Need to check stability but i still see errors

This gets repeated often though:-
2019-02-20 22:59:21.387 Error: CConnection_connect, connect request from 'Hive1' ignored. Transport is connected.
2019-02-20 22:59:22.771 Error: (Hive1): Async Secure Read Exception: 1, stream truncated

Re: Read British Gas Hive Heating temperature

Posted: Thursday 21 February 2019 19:20
by mark.sellwood
Keep an eye on memory usage.

Re: Read British Gas Hive Heating temperature

Posted: Thursday 04 April 2019 20:34
by sach
Started receiving these errors today:

2019-04-04 19:09:24.915 Error: (Hive) 'onMessage' failed 'KeyError'.
2019-04-04 19:09:24.915 Error: (Hive) ----> Line 860 in '/home/pi/domoticz/plugins/Hive/plugin.py', function onMessage
2019-04-04 19:09:24.915 Error: (Hive) ----> Line 155 in '/home/pi/domoticz/plugins/Hive/plugin.py', function onMessage
2019-04-04 19:09:24.915 Error: (Hive) ----> Line 496 in '/home/pi/domoticz/plugins/Hive/plugin.py', function UpdateDeviceState


Anyone else or just me?

Re: Read British Gas Hive Heating temperature

Posted: Tuesday 09 April 2019 8:20
by sach
Unfortunately the plugin is unusable for me now. :-(
Worked fine for months and nothing changed but all of a sudden it causes domoticz to crash and misses schedules.
I have zwave TRV's and temperature sensors in every room and Hive is used simply as a relay to switch the boiler on.
Will have to look for another solution.

Re: Read British Gas Hive Heating temperature

Posted: Tuesday 09 April 2019 8:29
by mark.sellwood
Same with me, I had to stop using it as it consumes all the memory over a few days then crashes domoticz.
I think something must have changed at the Hive end.

Re: Read British Gas Hive Heating temperature

Posted: Tuesday 09 April 2019 18:44
by MikeF
As the OP of this thread,I've produced python scripts to read Hive current and target temperatures, and display these in Domoticz. I installed the Hive plugin a while ago, but I experienced a number of errors, so I stopped using it. I've been using the python script happily (i.e., my RPi is happy!) for several years - I don't have Hive smart plugs or lights, etc., so I've not extended the script to these.

I've posted my latest version below, which may be of use if all you want to do is monitor your Hive heating - it uses the more recent Hive API (https://beekeeper.hivehome.com). To use it, create 2 dummy temperature devices (current, target), and a dummy switch (heating status), and enter the idx's in the script; also enter your Domoticz url and port, and Hive username and password.

ImageImage
Spoiler: show

Code: Select all

#!/usr/bin/env python

import requests
import json
import urllib3
from time import sleep, strftime
from datetime import datetime, timedelta

urllib3.disable_warnings()

# substitute Domoticx idx's below

insideIdx  = 'xxx'	# Hive current temperature
targetIdx  = 'xxx'	# Hive target temperature
heatingIdx = 'xxx'	# Heating status - on / off

# substitute Domoticz url and port below

DomoReadURL   = 'http://<url:port>/json.htm?type=devices&rid='
DomoWriteURL  = 'http://<url:port>/json.htm?type=command&param=udevice&nvalue=0&idx='
DomoSwitchURL = 'http://<url:port>/json.htm?type=command&param=switchlight&idx='

# Hive username and password

username = '<xxx>'
password = '<xxx>'

###

false = False
true = True
timedout = 2880 # minutes: 2 days
debug = True

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

def sensorTimedOut(idx):
	datestring = domoticzRead(targetIdx, 'LastUpdate')
	
	def LastUpdate(datestring):
		dateformat = "%Y-%m-%d %H:%M:%S"
		# the below try/except is meant to address an intermittent python bug in some embedded systems
		try:
			result = datetime.strptime(datestring, dateformat)
		except TypeError:
			result = datetime(*(time.strptime(datestring, dateformat)[0:6]))
		return result
		
	return LastUpdate(datestring) + timedelta(minutes=timedout) < datetime.now()

# Hive

# log on to Hive
payload = {'username':username, 'password':password, 'devices':true, 'products':true}
headers = {'Content-Type': 'application/json', 'Accept': 'application/json'}
url = 'https://beekeeper.hivehome.com:443/1.0/global/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

data = r.json()["products"]
len = len(data)

# find which node has heating data
key = "heating"
heating = False
for i in range(len):
	name = data[i]["state"]["name"]
	type = data[i]["type"]
	if key == type:
		nodeId = data[i]["id"]
		thermostat = i

# get values from Hive
target 	= r.json()["products"][thermostat]["state"]["target"]
temp   	= r.json()["products"][thermostat]["props"]["temperature"]
	
stateHeatingRelay = target > temp
if stateHeatingRelay: 
	heating = "On"
else:
	heating = "Off"

# log out from Hive
headers = {'Content-Type': 'application.json', 'Accept': 'application.json', \
	'X-AlertMe-Client': 'Hive Web Dashboard', 'Authorization': sessionId}
url = 'https://beekeeper-uk.hivehome.com/1.0/auth/logout'
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 target < 7.0: target = 7.0

if debug:
	print '       ', 'new','old'
	print 'temp   ', temp, last_temp
	print 'target ', target, last_target
	print 'heating', heating, last_heating

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

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

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

(Incidentally, this runs under Python 2.7; I haven't tried it under Python 3.x, so I don't know what - if any - changes are needed.)

Re: Read British Gas Hive Heating temperature

Posted: Tuesday 09 April 2019 18:53
by MikeF
I've just come across what I believe is a bug in the Hive API - looking at the json output, the target value

Code: Select all

target = r.json()["products"][thermostat]["state"]["target"]
is changing one hour later than it should (compared to the schedule). I suspect this bug was introduced on the recent change to BST.

I'll investigate a workaround.

Re: Read British Gas Hive Heating temperature

Posted: Tuesday 09 April 2019 23:06
by sach
Thanks Mike, I will give it a shot.

Don't suppose you could code in hot water support :-)

Re: Read British Gas Hive Heating temperature

Posted: Wednesday 10 April 2019 11:07
by sach
I think i may have solved the errors with the Python plugin by removing the bulbs and just leaving the heating.
I run Zigbee2MQTT and switched one of the bulbs over to it some time ago, but got lazy and left the other bulb on the Hive hub.
I just removed the 2nd bulb so I only have heating and hot water left and the error have stopped appearing in the logs.

This is on the 0.7 version of the plugin.
Will see how it goes for a while but its looking promising :-)

Re: Read British Gas Hive Heating temperature

Posted: Wednesday 10 April 2019 14:14
by MikeF
I've amended my script to include support for hot water - there are various places in the code where it says 'uncomment for hot water'. Note that I haven't tested this, as i now only have a (combi) heating system (so hot water is on demand), although I still have the Hive 2-channel receiver.

Also, I've included code to get the target temperature by parsing the 'schedule' object, to get round the bug I reported earlier.
Spoiler: show

Code: Select all

#!/usr/bin/env python

import requests
import json
import urllib3
from time import sleep, strftime
from datetime import datetime, timedelta

urllib3.disable_warnings()

# substitute Domoticx idx's below

insideIdx  = 'xxx'	# Hive current temperature
targetIdx  = 'xxx'	# Hive target temperature
heatingIdx = 'xxx'	# Heating status - on / off
# uncomment for hot water
#hotwaterIdx= 'xxx'  # Hot water status - on / off

# substitute Domoticz url and port below

DomoReadURL   = 'http://<url:port>/json.htm?type=devices&rid='
DomoWriteURL  = 'http://<url:port>/json.htm?type=command&param=udevice&nvalue=0&idx='
DomoSwitchURL = 'http://<url:port>/json.htm?type=command&param=switchlight&idx='

# Hive username and password

username = '<xxx>'
password = '<xxx>'

###

false = False
true = True
timedout = 2880 # minutes: 2 days
debug = True

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

def sensorTimedOut(idx):
	datestring = domoticzRead(targetIdx, 'LastUpdate')
	
	def LastUpdate(datestring):
		dateformat = "%Y-%m-%d %H:%M:%S"
		# the below try/except is meant to address an intermittent python bug in some embedded systems
		try:
			result = datetime.strptime(datestring, dateformat)
		except TypeError:
			result = datetime(*(time.strptime(datestring, dateformat)[0:6]))
		return result
		
	return LastUpdate(datestring) + timedelta(minutes=timedout) < datetime.now()

# Hive

# log on to Hive
payload = {'username':username, 'password':password, 'devices':true, 'products':true}
headers = {'Content-Type': 'application/json', 'Accept': 'application/json'}
url = 'https://beekeeper.hivehome.com:443/1.0/global/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

data = r.json()["products"]
len_ = len(data)

# find which node has heating data
key = "heating"
heating = False
for i in range(len_):
	type = data[i]["type"]
	if key == type:
		thermostat = i

# uncomment for hot water
'''
# find which node has hot water data
key = "hotwater"
hotwater = False
for i in range(len_):
	type = data[i]["type"]
	if key == type:
		hotwater = i
'''

# get target from schedule (workaround for current bug)
dow = datetime.today().strftime("%A").lower()
schedule = r.json()["products"][thermostat]["state"]["schedule"][dow]
len_ = len(schedule)
t = datetime.now().strftime("%H:%M")
tm = int(t.split(":")[0]) * 60 + int(t.split(":")[1])
for i in reversed(range(len_)):
	start = schedule[i]["start"]
	if tm >= start:
		target = schedule[i]["value"]["target"]
		break

# get values from Hive
#target 	= r.json()["products"][thermostat]["state"]["target"]
temp   	= r.json()["products"][thermostat]["props"]["temperature"]
# uncomment for hot water
#hotw	= r.json()["products"][hotwater]["state"]["status"].title() # changes 'OFF' to 'Off', etc.

stateHeatingRelay = target > temp
if stateHeatingRelay: 
	heating = "On"
else:
	heating = "Off"

# log out from Hive
headers = {'Content-Type': 'application.json', 'Accept': 'application.json', \
	'X-AlertMe-Client': 'Hive Web Dashboard', 'Authorization': sessionId}
url = 'https://beekeeper-uk.hivehome.com/1.0/auth/logout'
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')
# uncomment for hot water
#last_hotw	= domoticzRead(hotwaterIdx, 'Status')

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

if target < 7.0: target = 7.0

if debug:
	print '       ', 'new','old'
	print 'temp   ', temp, last_temp
	print 'target ', target, last_target
	print 'heating', heating, last_heating

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

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

if heating != last_heating:
	url = DomoSwitchURL + heatingIdx + '&switchcmd=' + heating
	r = requests.get(url)
	
# uncomment for hot water
#if hotw != last_hotw:
#	url = DomoSwitchURL + hotwaterIdx + '&switchcmd=' + hotw
#	r = requests.get(url)



Re: Read British Gas Hive Heating temperature

Posted: Thursday 02 May 2019 14:28
by Calzor Suzay
MikeF wrote: Tuesday 09 April 2019 18:44 As the OP of this thread,I've produced python scripts to read Hive current and target temperatures, and display these in Domoticz.
Thanks for the update Mike, is the install procedure the same as your original first post?

Re: Read British Gas Hive Heating temperature

Posted: Monday 13 May 2019 18:15
by MikeF
I think the instructions are pretty well given in my post of 9 April, but here goes:

1. Create 2 dummy temperature devices (one for inside, and one for target), and note their IDx's
2. Create 1 dummy switch for heating status, and note its IDx
3. Copy the script from my post of 9 April, and edit the python file with your own values: Hive and Domoticz logins, Domoticz url and IDx's
4. Place the file in the Domoticz script folder
5. Create a crontab which runs the script every x minutes (I run mine every minute); my crontab now looks like this:

Code: Select all

*/1 * * * * sudo python /home/pi/domoticz/scripts/hive2domo.py > /dev/null 2>&1 &
(Sorry for the delay - I've been away.)

Re: Read British Gas Hive Heating temperature

Posted: Monday 13 May 2019 22:30
by Calzor Suzay
Should of said I had a crack is from old to new and got it working.
Water bit didn't work at the start then realized there was more to un comment throughout the script not just at the top.

All looks good, thanks :)

Re: Read British Gas Hive Heating temperature

Posted: Wednesday 26 June 2019 22:59
by kevster
I ended up using @MikeF 's script. Works perfectly for me for now.

I would of liked to use Hive plugin but dont have any intention of using their bulbs etc. The TRV's look tempting however but it shouldnt take much to add to the scripts as and when. I suspect they will report back a current temp, target and if they are on/off.

Thanks for all the work on this folks !

Re: Read British Gas Hive Heating temperature

Posted: Thursday 04 July 2019 0:15
by MikeF
I have now changed over to the Drayton Wiser heating system
- see this thread: viewtopic.php?f=34&t=28585

I’m developing similar scripts there.

Re: Read British Gas Hive Heating temperature

Posted: Tuesday 24 September 2019 22:10
by sach
Anyone still using the Python plugin?

Re: Read British Gas Hive Heating temperature

Posted: Tuesday 24 September 2019 22:15
by imcfarla
Yes but I have redone my heating with Tado thermostats and most of my lights are now lightwaverf. Just a few hive bulbs and an active plug left...

Re: Read British Gas Hive Heating temperature

Posted: Wednesday 25 September 2019 20:04
by sach
That's a shame as the plugin does work really well, but I'm nervous in case it ever stops working and we have no devs working on it.
I'm at a crossroads where I have installed home assistant to try out where Hive works great, in addition to the Ring plugin which Domoticz is missing, however I have spent a lot of time and effort in getting domoticz to where it does everything I need.
I may just be better off getting a zwave boiler switch and sticking with Domoticz. Food for thought...

Re: Read British Gas Hive Heating temperature

Posted: Saturday 23 November 2019 11:29
by gyunda
I'll answer my own question - looks like domoticz does not send the servername so any site using SNI will fail.

Helena

Re: Read British Gas Hive Heating temperature

Posted: Thursday 17 September 2020 17:22
by imcfarla
I have just updated the plugin to work with a few more types of bulbs.