Getting CC128 Home energy monitor data into HASS

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

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.