CurrentCost Component for Home Assistant

Thanks. I changed to code and tried to list appliances that do not exist to try and reproduce, but I would not get the error message.
I still think it’ll handle this better so will try and publish my code later today or this week

Repo updated with the updated error handling…

While I’m at it I’ve updated the repo to make it HACS Compliant

Can you confirm your device is actually an Envi (aks cc128) and not an EnviR?
If so I’ll add it to the list of supported devices.

Yes, my device is a CC128

1 Like

Thanks for the update of the component, I tested and it works, but sometimes when restart home assistant some error messages appears in the log, there is another error from a different component between them:

‘’’
2020-09-08 17:30:07 ERROR (MainThread) [custom_components.currentcost.sensor] Error parsing data from serial port. Are all defined appliances connected? (line: CC128-v0.110288006:21:2824.61
2020-09-08 17:30:08 ERROR (MainThread) [homeassistant.components.unifi] Error connecting to the UniFi controller at 192.168.3.4
2020-09-08 17:30:09 ERROR (MainThread) [custom_components.currentcost.sensor] Error parsing data from serial port. Are all defined appliances connected? (line: /CC128-v0.110288006:22:3324.6301279100310
‘’’
after these errors no more errors are present in the log.

Another suggestion from my part is to show the custom cumponent in the integrations page and show the current cost product as a device with each individual sensor as entities.

Thanks for the feedback.
Can you confirm view you’ve setup your insurance of HA (hass.io, core, other)?
It looks as though your device sends a different message at startup…

It would not be a custom component then and I would not need to host this on my github.
At some stage I might try and make an official component but never done that yet, lots to do and learn

That would be nice bud not figured out how to split into separate entities yet.

Unfortunately my work is keeping me busy right now so not much time to invest in this just yet.

I’m glad I could help others take advantage of their current cost though :blush:

I’m using HA core (installed in a FreeBSD jail running in a FREENAS distribution).

Here you have some lines of the log of today when I restarted HA, I see in my previous reply that the xml content is not shown, so for this case I added a space after the start of each xml tag:

2020-09-09 17:54:04 ERROR (MainThread) [custom_components.currentcost.sensor] Error parsing data from serial port. Are all defined appliances connected? (line: ime>< tmpr>24.6< /tmpr>< sensor>0< /sensor>< id>00077< /id>< type>1< /type>< ch1>< watts>01689< /watts>< /ch1>< /msg>
2020-09-09 17:54:04 ERROR (MainThread) [custom_components.currentcost.sensor] Error parsing data from serial port. Are all defined appliances connected? (line: < msg>< src>CC128-v0.11< /src>< dsb>02881< /dsb>< time>06:26:22< /time>< tmpr>24.6< /tmpr>< sens
2020-09-09 17:54:05 ERROR (MainThread) [custom_components.currentcost.sensor] Error parsing data from serial port. Are all defined appliances connected? (line: r< msg>< src>CC128-v0.11< /src>< dsb>02881< /dsb>< time>06:46:31< /time>< tmpr>24.6< /tmpr>< sensor>4< /sensor>< id>00448< /id>< type>1< /type>< ch1>< watts>00000< /watts>< /ch1>< /msg>

It seems that at the beginning of the component the message is not complete, something like there was some bytes from a previous message in the serial port buffer.

Not sure indeed it’s odd. I don’t have this at all

For those interested, the current cost custom component is now available in the default HACS integrations

This is driving me crazt it maybe something to do with serial asyncio

I had this working on an android tv box running debian but have switched home assistant to run on a raspberry Pi 4B

I’m getting these errors on startup

2020-10-12 02:12:04 DEBUG (MainThread) [custom_components.currentcost.sensor] devices: [0, 2]
2020-10-12 02:12:09 ERROR (MainThread) [homeassistant] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
File “/home/john/homeassistant/lib/python3.8/site-packages/homeassistant/components/template/template_entity.py”, line 261, in _async_template_startup
result_info = async_track_template_result(
File “/home/john/homeassistant/lib/python3.8/site-packages/homeassistant/helpers/event.py”, line 933, in async_track_template_result
tracker.async_setup(raise_on_template_error)
File “/home/john/homeassistant/lib/python3.8/site-packages/homeassistant/helpers/event.py”, line 740, in async_setup
self._info[template] = template.async_render_to_info(variables)
File “/home/john/homeassistant/lib/python3.8/site-packages/homeassistant/helpers/template.py”, line 401, in async_render_to_info
render_info._result = self.async_render(variables, **kwargs)
File “/home/john/homeassistant/lib/python3.8/site-packages/homeassistant/helpers/template.py”, line 332, in async_render
return compiled.render(kwargs).strip()
File “/home/john/homeassistant/lib/python3.8/site-packages/jinja2/environment.py”, line 1090, in render
self.environment.handle_exception()
File “/home/john/homeassistant/lib/python3.8/site-packages/jinja2/environment.py”, line 832, in handle_exception
reraise(*rewrite_traceback_stack(source=source))
File “/home/john/homeassistant/lib/python3.8/site-packages/jinja2/_compat.py”, line 28, in reraise
raise value.with_traceback(tb)
File “”, line 1, in top-level template code
TypeError: ‘NoneType’ object is not subscriptable
2020-10-12 02:12:09 ERROR (MainThread) [homeassistant] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
File “/home/john/homeassistant/lib/python3.8/site-packages/homeassistant/components/template/template_entity.py”, line 261, in _async_template_startup
result_info = async_track_template_result(
File “/home/john/homeassistant/lib/python3.8/site-packages/homeassistant/helpers/event.py”, line 933, in async_track_template_result
tracker.async_setup(raise_on_template_error)
File “/home/john/homeassistant/lib/python3.8/site-packages/homeassistant/helpers/event.py”, line 740, in async_setup
self._info[template] = template.async_render_to_info(variables)
File “/home/john/homeassistant/lib/python3.8/site-packages/homeassistant/helpers/template.py”, line 401, in async_render_to_info
render_info._result = self.async_render(variables, **kwargs)
File “/home/john/homeassistant/lib/python3.8/site-packages/homeassistant/helpers/template.py”, line 332, in async_render
return compiled.render(kwargs).strip()
File “/home/john/homeassistant/lib/python3.8/site-packages/jinja2/environment.py”, line 1090, in render
self.environment.handle_exception()
File “/home/john/homeassistant/lib/python3.8/site-packages/jinja2/environment.py”, line 832, in handle_exception
reraise(*rewrite_traceback_stack(source=source))
File “/home/john/homeassistant/lib/python3.8/site-packages/jinja2/_compat.py”, line 28, in reraise
raise value.with_traceback(tb)
File “”, line 1, in top-level template code
TypeError: ‘NoneType’ object is not subscriptable

I honestly have no idea why this no longer works
can you help please?

It was a permissions problem :slight_smile:

I just needed to add myuser to the dialout group

sudo adduser myuser dialout

Then logout and reconnect the usb device and now it works! 

Lol I just logged in to answer and say “I have no idea why”
It’s good to know :+1:

Man it has driven me up the wall :slight_smile: I just kind of got the idea to cat /dev/usb0 and was denied but sudo cat /dev/usb0 worked and the big difference was i had been using the android tv box for ansible and my user for that has root access enabled to be able to configure the system.

t’s been like a day banging away at this … still this should be useful for someone else :slight_smile:

1 Like

I have created an ESPHome+ESP32-based device to read the serial port from the CurrentCost, parse the XML, then report the channel values to HA using a lambda (ESPHome config snippet follows):

  - platform: custom
    lambda: |-
      auto currentcost = new CurrentCostSensor(id(uart_bus));
      App.register_component(currentcost);
      return {currentcost->ch1, currentcost->ch2, currentcost->ch3};
    sensors:
      - id: "ch1"
        name: "currentcost_ch1"
        unit_of_measurement: W
        accuracy_decimals: 0
      - id: "ch2"
        name: "currentcost_ch2"
        unit_of_measurement: W
        accuracy_decimals: 0
      - id: "ch3"
        name: "currentcost_ch3"
        unit_of_measurement: W
        accuracy_decimals: 0

For the moment I am only reporting the main channel (channel 0) because I only have the three phase whole-house sensor connected. There is not much to prevent parsing the rest of the XML stanza, though, should it be needed.

Happy to share the rest of the setup if anyone is interested. EDIT: Here’s a gist with the main parts: https://gist.github.com/RoganDawes/190cafcd8e2545ccd93628f2f9bfa82c

I do have a question, though. Having used an MQTT-based approach for some time, and collected several tens of thousands of data points, I’m not sure how to move over to my new ESPHome-based approach, because the sensor names will change, and there will be a discontinuity of data. I can make a template sensor that simply copies the new sensor value to the old value, but that seems wasteful, because I’d just be doubling the number of data points being recorded. Is this something that I would need to do some deep database diving to fix, to rename the old datapoints to reflect the new sensor name?

1 Like

here’s one way:
ALERT: Back up the files first !

  1. Shut HA down
  2. edit the core.entity_registry file in the .storage folder, remove the block where your old Current Cost entities are
    1… if you were using MQTT Discover, delete the Current Cost Discovery entries (you can use MQTTExplorer, the default discovery topic is homeassistant)
  3. call your ESPHome sensors the same as the previous ones
  4. Start HA and you should be good.

May I also suggest InfluxDB / Grafana to store your data for long term. It’s more efficient than HA. You can follow this thread for information on how to get set up

That’s awesome, thanks so much!

Will definitely give it a go.

I’ve setup a Current Cost Envi at my daughters house to monitor the household electricity consumption
The setup is almost identical to mine
Raspberry Pi 3 for HA
Current Cost Envi (I have a EnviR)
I can see the temperature & energy consumption on the Envi screen but HA is only seeing the temperature.
I’ve spent the last few hours trying to figure out why but without success
I even tried changing the energy consumption from the main screen (appliance 0) to appliance 1 then reconfigured HA but still nothing
HA actually indicates that it is communicating with the Envi but displays 0 (ZERO)
Anyone have ANY ideas

How do you get the data out of the Currentcost device? Are you using my custom_component?
If so how is it installed? Manually or via HACS? And can you post your config as well as set the logs to debug for it and post what you get?

Yes I’m using the my current cost custom component.
Thanks for showing interest.
I was at my daughters again yesterday and repaired / setup the current cost Envi & current clamp with C2 transmitter and after a few attempts it starting working but unfortunately I do not know what was causing the problem