Getting CC128 Home energy monitor data into HASS

Hi @rcnutter, would you be kind enough to share your python script?
it’s the only device that HA doesn’t support from previous home automation software before I “switch it off”

I use INFLUXDB to store HASS historical data. With the Grafana I can view graphs as well.

Hi.
I didn’t use the python code in the end for HomeAssistant, partly because I could not find the old code, partly because I did not want the CC128 near the Pi running HomeAssistant and partly because I moved to HASSIO and could not figure out how to do any serial port access to it.

I used an ESP12E and powered it from the CC128 directly (but needed a more powerful 3.3V PSU).
I then used an Arduino sketch to use the ESP soft serial port to read the XML from the CC128, extract the bits I want and then send them via WIFI to an MQTT broker.

I then use MQTT to read the value into HASSIO.

It was all a lot of effort for not much really! Just one sensor which shows the Total electrical power demand of the house. Good learning process though.

Have to say it is not perfect. The soft serial library does not seem to play well, and I occasionally get bad data from the MQTT broker - this is then ignored by HASSIO.

The data is fine from the CC128 but the software serial lib corrupts the original string, and because of this I get corrupt data sent to the MQTT broker. The CC128 sends data every 6 seconds or so and when I see this problem it will continue for about 10 to 15 readings and then go away. Very strange. Think it might be related to the processor speed on the ESP12.

I also had to build in a little self reboot mechanism into it. For some reason the device occasionally loses contact with the MQTT broker so if it cannot make a connection after 5 retries it reboots.
This is a problem that I also get with Sonoff devices too.

The history on HASSIO is still painfully slow to load, so I actually now use Node-Red to gather data from the MQTT broker and store it in a Mongo DB. This data is then viewed with highcharts.js in a UI Template in Node-Red. Even this can be quite slow when pulling data for 24 hours that is recorded every 6 seconds.

A bit of a shame really - would have preferred to do it all from HASSIO.

I ended up recreating a python script for those who may be interested as even with the new addition of the serial component to HA, there is no option to set the baud speed and there would still need some scripting behind to extract the data.

Here is the script I ‘wrote’ (‘wrote’ in quotes as I largely copied Robin’s one)

#!/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()
		#print Temp[0]
		if (Temperature <> Temp[2]):
			print "Temperature: %s" % Temp[2]
			Temperature = Temp[2]
			HASS_API_State("sensor.CurrentCost_Temperature", Temperature, "°C")
		if (Temp[0] == 1):
			if (Total <> Temp[1]):
				print "Total: %s" % Temp[1]
				Total = Temp[1]
				HASS_API_State("sensor.CurrentCost_Power", Total, "Wh")
		if (Temp[0] == 2):
			if (Dehumidifier <> Temp[1]):
				print "Dehumidifier: %s" % Temp[1]
				Dehumidifier = Temp[1]
				HASS_API_State("sensor.Dehumidifier_Power", Dehumidifier, "Wh")
		if [Temp[0] == "9"]:
			if (Sky <> Temp[1]):
				print "Sky: %s" % Temp[1]
				Sky = Temp[1]
				HASS_API_State("sensor.CurrentCost_Sky", Sky, "Wh")
			
	except:
		pass

Glad I couldn’t find my code now - that is way more elegant than mine was…

1 Like

I added a “Share your Projects” post with more details on how I did it, in case it helps someone else:

Am not so familiar with scripting, but is it correct to assume this script will also work when you have Currentcost IAM’s installed? Those will generate additional sensors.

Yes I designed my code so it would also pick up additional monitors, though you have to amend the code. I commented it to explain what needs doing. Have a go at it, if you struggle let me know

1 Like

Hi

I’m new on this world.

I read this topic and I have a CC connect by RJ45 to the internet. (CC website)

It’s possible to connect to HA? (I don’t know python or arduino)

Greetings from Portugal

Where is your CC connected to? You’ll need to connect your CC to a Computer/Server running Linux (not sure if it’ll work under Windows as I never tested it)
I’ve amended my version to now work over MQTT, check this post for more info:

There are also more details on how the script works…

Hi lolouk44

Thank you for the reply. I don’t know how to do what it’s explained in the article.

I have my CC connected to a Brigde (http://www.currentcost.com/product-bridge-specification.html) that connects my CC to my network Ip, and since it’s connected to internet, connects to the CC website (that it’s not working right now).

Since have it’s own IP address , I thought that it’s possible to send the data to Home assistant, but I’m a newbie and I don’t know how to do it.

Thank you very much for listening!

Thanks. I have no idea how the bridge works so doubt you’ll be able to use it.
You early need a computer to connect your CC to if you want to collect the data and send it to HA.

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