Hi all, been searching for some time to see if this is possible but can’t figure it out. The CurrentCost CC128 is a home energy monitor that gives out an XML string every 6 seconds or so containing information on how much electrical energy is being used by the house. I previously used a RPi connected to the serial / USB port of the CC128 and wrote a Python program to read this data and present it to a web page. I thought it would be a nice idea to get this into HASS instead but can’t figure out how.
HASS is running on a Raspberry Pi which could be connected to the CC128 but I can’t figure out how to get the data into HASS from the serial port.
Can anyone provide any information to point mew in the right direction please?
If he already has a Python program to read the data, it’s just a matter of changing it to publish data to an MQTT sensor created in HASS (you can even curl HASS API to call service mqtt.publish - allowing you to keep the connection to CC128 to be kept in separate RPi if you wish).
I was looking at it the wrong way(!), expecting there to be a plug in or similar to read the serial port on RPi. I will look at creating some sort of Serial / XML to MQTT converter, but using a RPi for this seems a bit overkill.
Ideally I did not want to have multiple RPi’s running different things (Hence the reason for trying to get it onto HASS in the first place!)
It might however give me the opportunity to play and see if this can be done with an ESP8266, depending on exactly how the serial port works on the CC128. I suspect that it is logic level RS232 on the CC128 as the USB cable has a small box in it that I guess contains a USB chip of some kind.
Another thread popped up yesterday talking about using an EmonPi and MQTT to get the data into HA, which is why I was pretty confident it would work.
I have been waiting for a cheap product to come out that will integrate with HA but at this point I am probably going to just get a 100A current sensor and grab an old arduino board, download the Emonlib library and create a simple sketch to push it to HA with mqtt.
Just got to figure out where to store all the data. I dont want a gigantic HA database and I currently purge mine after 2 days.
I see what you mean about the database!
The CC128 provides power data every 6 seconds or so.
The ESP12F I have used for conversion works quite well so far and is publishing this data every 6 seconds to the MQTT broker which HASS IO then reads. Good little project for learning, but long term not really helpful since the HASS IO history can only view everything, not just the Power Demand sensor (as far as I know).
I need to read up more about the history on HASS IO
I also use domotiga for home automation (started using that before HA).
Domotiga has CurrentCost support via the USB cable and works well. I’m wondering whether there would be a way to “reverse engineer” the process for HA?
See https://github.com/DomotiGa/DomotiGa/blob/master/DomotiGa3/.src/CCurrentCost.class for domotiga code
I’m not good enough a developer to reverse engineer and add to HA but I hope someone else can do?
I’m more than happy to help troubleshoot and test though
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”
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
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
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…
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.