CurrentCost Component for Home Assistant

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

Thanks for the feedback. Glad to know it’s now working :wink:

To answer your previous question to how did I install the current cost component, it was originally manually installed then updated using HACS.
Can you tell me why I have channel 1,2 & 3
Appliance 1 & Channel 1 are the Envi main screen which is actually appliance 0
Then I have 2 IAMS setup on the Envi appliance 1 & 2

From Developer Tools
sensor.current_cost
Temperature: 19.9 ºC
Appliance 0: 208 W
Appliance 1: 10 W
Appliance 2: 5 W
Appliance 3: null
Appliance 4: null
Appliance 5: null
Appliance 6: null
Appliance 7: null
Appliance 8: null
Appliance 9: null
Channel 1: 208 W
Channel 2: 0 W
Channel 3: 0 W
unit_of_measurement: W
friendly_name: Current Cost
icon: mdi:flash-circle
device_class: power

From Configuration.yaml

  - platform: currentcost
    serial_port: /dev/serial/by-id/usb-Prolific_Technology_Inc._USB-Serial_Controller-if00-port0
    name: Current Cost
    baudrate: 57600
    devices:
      - 0
      - 1
      - 2
      - 3
      - 4
      - 5
      - 6
      - 7
      - 8
      - 9
  - platform: template
    sensors:
      currentcost_temperature:
        entity_id: sensor.current_cost
        unit_of_measurement: '°C'
        value_template: '{{ state_attr("sensor.current_cost", "Temperature")[:-3] | float }}'
        friendly_name: CurrentCost Temperature
      currentcost_power:
        entity_id: sensor.current_cost
        unit_of_measurement: 'w'
        value_template: '{{ state_attr("sensor.current_cost", "Appliance 0")[:-2] | int }}'
        friendly_name: CurrentCost Power
      currentcost_1:
        entity_id: sensor.current_cost
        unit_of_measurement: 'w'
        value_template: '{{ state_attr("sensor.current_cost", "Appliance 1")[:-2] | int }}'
        friendly_name: CurrentCost 1
      currentcost_2:
        entity_id: sensor.current_cost
        unit_of_measurement: 'w'
        value_template: '{{ state_attr("sensor.current_cost", "Appliance 2")[:-2] | int }}'
        friendly_name: CurrentCost 2````

You have 3 possible channels on your EnviR.
You’re currently only using Channel 1, to 208W in total
Appliance 0 is the total consumption, so also 208W
You then have you 2 additional IAMs on Channel 1, Appliance 1 and Channel 1, Appliance 2 respectively.
Hope this makes sense

To sound rather THICK, No!
I don’t get the Channel 1, Appliance 1 & 2
I’ve used this equipment for many years, long before HA and was only ever aware of Appliances 0 - 9 and I could pair any transmitter to any appliance
Is this something you have implemented as you have developed the component or something I have missed from day 1?
Ah!, I think I might have just grasped it, is it to do with single phase power or 3 phase power and the 3 lightning symbols on the Envi display

That would probably be it.
I also only have Channel 1 in use, and to be honest not tried to plug the clamp in a different socket (there are 3).
The channel info comes from the data on the serial port connection so I’m just displaying it…

You must be using the older / larger transmitter that has 3 sockets (D cell batteries) myself & my daughter are using the smaller transmitters that have 1 RJ connector socket (C cell batteries) I do have some of the type you have but they are currently unused.
On my system I’m not using current clamps, I’m using 2 OptiSmarts (1 for solar & 1 for electric) & 1 GaSmart (gas)
I only have a current clamp on the electric at my daughters house. They do have 2 smart meters but I’m not sure on compatibility of equipment with them for HA. I would love to get there gas into HA so I can make a direct comparison to my own household usage.
It seems impossible to get hold of the OptiSmart & GaSmart sensors anymore but Envi’s, EnviR’s & current clamps are regularly available on ebay. Even IAM’s come up for sale now & again.

Hi this project is going great but I found a bit of an issue with the device itself. I went looking for a leak and I found one. I’ve recently bought some Gosund metering plugs and I found I was using more power than they calculated I basically cut out all but two outlets being monitored by the gosund (they are flashed to tasmota) At this point they measured 75 watts and the current cost was measuring 150 watts Part of the reason is the VA which was 125VA for 75 watts which gives a powerfactor of 0.6 ok so that explained 50 watts of difference. but still left 25 watts then I found this manual http://www.currentcost.com/download/CurrentCost%20EnviR%20Man-Singles.pdf The clamp measures current in the meter tail but to calculate watts it multiplys current by 240 volts to get watts. Currently my mains supply seems to be 228Volts it was 230 Volts earlier a bit of math and the difference in power on a 150 watts is about 10 watts. The clamp meter can be tuned according to this manual from 200 - 260 volts in 10 volt steps.

I was thinking it might be better to do a fix in software maybe (power / 240) x 230 = actual VA or the current supply voltage. Maybe this could be done in the current cost integration. Using a constant or a metered voltage.

Looks like quite a lot of devices have poor power factors , I don’t know who wins the consumer or the electricity board.

I just found this review of the current cost meter on Amazon and it really sums up the issues with this meter or maybe indicator is more apt.
strictly speaking it measures current and multiplys by voltage to get a value which is not watts but VA W= VA * pf when I went hunting for missing Watts I calibrated as best I could my Gosund power metering sockets with an incandescent bulb of 52 watts with this load the power factor was 1 and my socket meter agreed it giving WVA pf frequrency and power used. This is how I found most of my loads were switched mode PSU’s with little power correction. I went to the breaker box and got down to two circuits with a gsund measuring each circuit the Watts added up to 75 the VA was 125 and Power factor 0.6 CurrentCost was steady at 150 watts I was still short 25 watts. I then found in an old manual that the current cost meter is set at 240 volts by default (it can be tuned to give between 200 - 260 in 10 volt steps) with Ireland being based on 230 volt that gave maybe another 10 watts and I think 15 watts can be metering error , voltage and frequency variation. Not bad really.

The current cost meter was designed to indicate how much power you were using, relatively. I think it achieves that goal, it doesn’t tell you figures that allow you to calculate your bill but the figures are going to be high so not more than you expect. In the context of home assistant it does give you an idea of when you want to run your more demanding loads e.g when fossil fuel is low and so are CO2 emmissions Anyway 10 years ago AW had these meters sussed out.



A. W.

3.0 out of 5 stars Power Factor Problems

Reviewed in the United Kingdom on 29 September 2011

Verified Purchase

The unit is easy to install, and will quickly show you how much apparent power your house is using, but unfortunately that’s not what you’re billed for in the UK.

The included software is terrible, and the statistical aggregation on the unit doesn’t allow for Generation, so it’s not terribly useful for solar either (though it will at least log it as an appliance, appearing as usage rather than generation).

Because of power factor issues, it will accumulate usage that simply does not exist. For instance, my solar inverter appears to use 80W at night, and there’s no way to exclude this. The unit streams data out on a data cable, which is good, and dumps it’s history on every odd hour of the day. However, it does include the phantom current.

The samples in the unit are timestamped, but there is no date, and the clock stops when you are in any setup menus, or downloading on demand.

Individual appliance monitors report real power instead of apparent power, which lets you get a measure of how much it’s really costing for an appliance, but the whole house reading is always too high (unless everything in your house has a power factor of 1.0, which it won’t).

Take the results with a pinch of salt. This and most other clamp meters will behave the same way. For example, dimming lights on a standard dimmer switch makes your meter move slower, but this unit will not show any significant decrease power consumption when you do so.

That said, the unit does let you see when someone’s left their hair straighteners or PC on, so it may save you some money in the long run.

In addition, compared to the Bluetooth unit for my solar PV inverter, the entire system (this, 1 appliance unit, and 3 clamps) is still cheaper than that option.

Thanks. I agree with the above and although I recently updated the component to make it chinoiseries with the new energy feature in HA as well as extract some of the historical data, I do take the readings with a pinch of salt. Indeed clamp meters week never be 100% accurate.
I’ve already ordered parts to build an home assistant glow and read off the flashing led on the meter. This not only should be more accurate but it will also allow me to track gas usage.

It is in proportion to your electricity usage thou it just needs some conditioning.

There are 2 sources for error the voltage and the power factor and that can be calculated from a power monitoring plug so if i have va of 125 and w of 75 then pf = 0.6 and voltage of 230 instead of 240 about .96 then that gives 0.576 so the current cost figure of 150va is actually more like 86.4 real watts It’s not perfect but it is close.

With the power readings corrected my power usage seems to vary about a couple of units a day or about 30 cents so even with inaccuracy its still going to be accurate to within 10 euro a month. So really all thats needed is a figure to convert the current cost value of VA into a real watts value. this could be a helper float or measured from a socket sensor value or even aggregate values .

I just realised that there is a problem with this solution unmeasured resistive loads e.g an electric shower would be resistive and a large proportion of the load. which kinda means splitting the measured loads from the unmeasured but thats math really.