Getting CC128 Home energy monitor data into HASS

Ok, thank you. For now I will learn more about HA and I will came back when I know more.

Thanks for the help.

No worries. I’m guessing the cable connected to your CC will be the usb to serial one so you should be able to connect it to the computer running ha and collect the data from there. What are you running ha on?

One Raspberry PI

Then I would have thought you can connect the cc to a USB port on your pi and run the script. Does the cable out of your cc look like usb one end and rj45 the other end?

looks like standard RJ45 both sides to me, you’ll need one of these:
https://www.ebay.co.uk/sch/i.html?_odkw=currentcost+cable&_osacat=0&_from=R40&_trksid=p2045573.m570.l1313.TR0.TRC0.H0.Xcurrentcost+data+cable.TRS0&_nkw=currentcost+data+cable&_sacat=0

OK, then I will connect CC to Pi. What should I do then? If you can explain in a very easy way I appreciate. I don’t understand coding !

Thank you very much

Ciao Ricardo,

If you’re free today, I have some free time. if you already have the right cable, it might be easier to move to discord where we can have a conversation? My discord tag is #7180

Done! Created my account!

It’s been a pleasure to meet you. Thank you for all the patience that you had with me. If weren’t you I will never get this to work. You been fantastic!

Thank you very much, from the bottom of my heart!

1 Like

Following some hard work ( :slight_smile: ) with @rfdmota below is the revised code that will work with python3

#!/usr/bin/python
# -*- coding: utf-8 -*-

import untangle
import serial
import json, requests, simplejson

def get_data(port='/dev/ttyUSB1', verbose=False):
	"""port: the port that the CurrentCost meter is attached to. Something like /dev/ttyUSB0 or COM1
	Returns:
	(sensor, temperature, usage), with sensor as the number of the interface a device is assigned to, temperature in degrees C, and usage in Watts
	"""
	ser = serial.Serial(port, 57600)
	xmldata = ser.readline()
	if verbose:
		print(xmldata)
	ser.close()
	p = untangle.parse(xmldata)
	temperature = float(p.msg.tmpr.cdata)
	watts = int(p.msg.ch1.watts.cdata)
	sensor = int(p.msg.sensor.cdata)
	return [sensor, watts, temperature]

def HA_API_State(Device_Id, Value, Unit):
	#https://home-assistant.io/developers/rest_api/#post-apistatesltentity_id
	url = "http://192.168.0.24:8123/api/states/" + str(Device_Id) + "?api_password=SECRET" 
	data={"state":"" + str(Value) + "", "attributes": {"unit_of_measurement": "" + str(Unit) + ""}}
	headers = {'Content-type':'application/json', 'Accept-Encoding': 'text/plain', 'Accept': 'text/plain'}
	r = requests.post(url, data=json.dumps(data), headers=headers)
	c = r.content
	result = simplejson.loads(c)

Temperature = 0
Sky = 0
Dehumidifier = 0
Total = 0

while(True):
	try:
		Temp = get_data()
		if (Temperature != Temp[2]):
			print ("Temperature: %s" % Temp[2])
			Temperature = Temp[2]
			HA_API_State("sensor.CurrentCost_Temperature", Temperature, "°C")
		if (Temp[0] == 1):
			if (Total != Temp[1]):
				print ("Total: %s" % Temp[1])
				Total = Temp[1]
				HA_API_State("sensor.CurrentCost_Power", Total, "Wh")
		if (Temp[0] == 2):
			if (Dehumidifier != Temp[1]):
				print ("Dehumidifier: %s" % Temp[1])
				Dehumidifier = Temp[1]
				HA_API_State("sensor.Dehumidifier_Power", Dehumidifier, "Wh")
		if [Temp[0] == 9]:
			if (Sky != Temp[1]):
				print ("Sky: %s" % Temp[1])
				Sky = Temp[1]
				HA_API_State("sensor.CurrentCost_Sky", Sky, "Wh")
			
	except:
		pass

Note you will probably need to install additional libraries
sudo pip install untangle (same method with request, pyserial, simplejson)
and if it still doesn’t work you may have to edit /usr/local/lib/python3.5/dist-packages/untangle.py:

  1. Replace parser.parse(StringIO(filename)) with parser.parse(StringIO(filename.decode('utf-8')))
  2. Replace return string.startswith('http://') or string.startswith('https://') with return string.startswith(b'http://') or string.startswith(b'https://')
1 Like

Hi, I’ve made an Arduino library to read messages from CurrentCost EnviR devices using an esp8266 as it is mounted on https://blog.nexusuk.org/2017/10/esp8266-currentcost-wifi-gateway.html.

You can find it on GitHub https://github.com/gmag11/CurrentCostLib

You can use it to read the values and send them to any platform, now that Pachube/Xively is down.

Hi, sorry to drag this back up! I’ve been trying for a couple of days to get this to work. If I try the mqtt version from the page linked above I get stuck because I don’t know the SU password so the homeassistant user cannot run the python as root.

So I’ve had a go at the version above. However I’m not sure how to set up my configuration.yaml file for this version. I assumed (haha) that it was going to be something like adding
shell_command:
getcurrentcost: python3 /home/homeassistant/.homeassistant/CurrentCost.py

But then I’m stuck on how to add the sensors (I’ve still got the mqtt platform stuff in there).

Any help greatly appreciated!

Thanks

I have a similar CurrentCost unit that transmits on 433MHz, does yours? If so, it’s very simple to get the information into HA. I use an SDR dongle and a package called RTL_433 that can decode the CurrentCost data stream and output it in a JSON format.
I can give you more info if you are interested.

as far as I know you will need to run the python script as root / su, regardless of whether you use the API or MQTT.
What are you running HA on? Is it hass.io? If so there is no password for the default user name (root)
so (I think) you could simply call the script with sudo:

shell_command:
getcurrentcost: sudo python3 /home/homeassistant/.homeassistant/CurrentCost.py

I’m interested :slight_smile:

No, mine only transmits over the RJ45. It’s quite an old model - the original Envi. I think the key part will be to get the “platform” part of the config file correct. If I run the script in verbose mode I can see the XML coming in happily. I’m a relative HA noob. Thanks for the offer though.

Hi, thanks. No, I’m using Hassbian. Could you give me an idea of what to try in the configuration.yaml file for the sensors?

Thanks

That’s why I run the script with my normal user as SU, then all comms go via MQTT (advantage is messages are retained so when HA restarts states are still valid)
What happens if you use the MQTT version and try to run it manually?

How would I test if it was working? I can give it a go. So do you mean running it as the pi user with sudo? Note - I am not even remotely up to speed with mqtt :slight_smile: