Getting CurrentCost data into HA

Thanks - Yes, I think I might take that path as well and run as a crontab

1 Like

Super helpful, thank you!

Any ideas how one might set up a sensor for the cumulative values - ie: kWh used today?

Hi,

you must use 2 sensors. One with the current value and another reading from a mqtt topic that stores the sum of all values.

Sensor:

  - platform: mqtt
    name: "cpd daily"
    state_topic: "cpd/switch/daily"
    unit_of_measurement: kW

Automation example for 1m reading and storing in KW:

- id: cpd_counter
  initial_state: on
  alias: cpd counter
  trigger:
    platform: time
    minutes: '/1'
    seconds: 0
  action:
  - service: mqtt.publish
    data_template:
      topic: 'cpd/switch/daily'
      payload: "{{ ( states.sensor.cpd_daily.state | float + (states.sensor.cpd_current.state | float / 60000)) | round (6) }}"
      retain: true

After, each day at 00:00:00 you reset the counter topic

- id: cpd_reset_daily
  initial_state: on  
  alias: cpd reset daily
  trigger:
    platform: time
    at: '00:00:00'
  action:
  - service: mqtt.publish
    data_template:
      topic: 'cpd/switch/daily'
      payload: "0"

Many thanks for the scripts and follow on information here but I am struggling to get it to work. Can someone point me in the right direction for debugging the serial to MQTT messages please.

I am running Home Assistant using the built in MQTT server on an Armbian Android box. I have a current cost Envir plugged in with the USB serial cable. Terminal shows the data coming in correctly on /dev/ttyUSB0 every 6 seconds. I had most of the errors related to python 3.5 which are now clear, thanks to suggestions above.

However, I have MQTTfx subscribed to CurrentCost/Temperature and nothing is getting published. Just to add I am running the script from the command line in case there are HA issues.

How can I check what is being read and parsed in the script? I am not exactly proficient with Linux. Any help greatly appreciated as I am fairly stuck.

Hi, I’m struggling to understand the use of MQTT server. I have current cost Envi connected directly to Raspberry Pi USB port and can read date a using python script in standalone IDE and pass to file however I think this caused my Pi to reboot when running overnight. The same Raspberry Pi runs HA. What do I need to pass data from the Envi to HA? Do I need MQTT?

Thanks in advance.

MQTT is just a communication protocol that’s lightweight so ideal for IoT communications
Because you connect to the Envi via serial port, it’s always a pain as not closing correctly means it’s difficult to use it again without reboot.
I therefore use a small script to send data to an MQTT server. This way the script works and sends its data, regardless of whether HA is up or down.
HA picks up data from the MQTT server, regardless of whether the script is up or down (of course script not working means no data, but that won’t stop HA from running)

Hi, thanks for the quick reply and explanation. Assuming I need to add the MQTT integration where does the scripting need to be added?

Look at my script on top post. Can be placed anywhere. Just needs the correct mqtt connection details then needs to be launched, it’ll send data to mqtt.
Then create an mqtt sensor in HA to retrieve the data

I’m also looking to connect my CurrentCost monitor to HA, but I am approaching it a slightly different way. Rather than using the USB-Serial adapter, I have constructed a super barebones ESP07 connected to a RJ45 jack, running esp-link firmware to make the serial port available over the network. I also upgraded the power supply internals of the CurrentCost receiver to be able to power both the receiver itself and the ESP07. So now I can telnet to the IP address of my ESP and get the XML returned to me.
My intention is to use the native MQTT functionality of esp-link to simply package up each line of the output and submit that as the value of a JSON payload to my broker, and have HA take care of extracting the values from the XML. I may need to change the esp-link parsing from SLIP encoding to line-delimited, but that should not be a huge problem.
The question I have that relates to HA is how can I parse the XML out of the JSON message when I receive it, in order to update my sensors?
And perhaps more fundamentally, how can I update 3 or more different sensors based on a single MQTT message? In my case the CC is reporting consumption on three phases, as well as a temperature reading, etc.

if you can read the xml then refer to the code I posted above, it’ll help you understand how to get the value of each channel.
I’ve not used ESP07 so no idea what type of code it needs or if it can handle python, so you may need to do some adjustments and find equivalent libraries…
Sorry I can’t be of more help

Finding the value for each channel is not the problem I am trying to solve. I can see the XML structure, and where each value sits.
I’m looking for a technical answer on how to extract the value of an XML tag using the HA templating support. Other examples have made use of the scraping module which has specific yaml clauses for referencing an XPATH expression to extract that specific node, but that appears to be specific to the Scrape module retrieving XML from an URL, whereas my XML will arrive via MQTT.
Is there an equivalent to to_json and from_json for xml? Should there be?

Can you not move the logic to the ESP07 and then send the data as json on the correct topic instead?
The CurrentCost only provides data once a sec at best so that should give the ESP07 plenty time to do the calculations… Or am I missing something?
I’m not sure jinja2 (language used bu HA for templating) can handle and parse xml

The CurrentCost only sends a string every 10 seconds, if I recall correctly, so yes, lots of time to do the processing. But, I was trying to avoid parsing XML using C :grimacing:

It does seem, though, that the best place to do it is indeed in the ESP, before it gets injected into MQTT.

So, I have an initial proof of concept to get the data to HA in JSON format:

nc currentcost.lan 23 | while read line; do
printf "%s\n" $line | ./xml2json | mosquitto_pub -t 'tele/currentcost/SENSOR' -s
done

(The above should be easily translated for those using the serial port adapter to read from the serial port at 57600 baud instead of connecting to a telnet port. e.g. stty -F 57600 /dev/ttyUSB0 and cat /dev/ttyUSB0 | while read ... )

xml2json is a header-only implementation of an XML to JSON converter, which should be fairly straightforward to compile for the ESP, so I’ll probably make a custom Tasmota build to solve this long term.

And I can see the data arriving in HA, looking like:

{
    "msg": {
        "src": "CC128-v1.29",
        "dsb": "01915",
        "time": "22:59:09",
        "tmpr": "21.9",
        "sensor": "0",
        "id": "02639",
        "type": "1",
        "ch1": {
            "watts": "00047"
        },
        "ch2": {
            "watts": "00231"
        },
        "ch3": {
            "watts": "00466"
        }
    }
}

I am using the following sensor to extract the different channel values as attributes, as well as a combined ch1+ch2+ch3 value as the sensor “state”.

- platform: mqtt
  name: "Electricity Use"
  state_topic: "tele/currentcost/SENSOR"
  value_template: " {{ value_json.msg.ch1.watts | int + value_json.msg.ch2.watts | int + value_json.msg.ch3.watts | int }} "
  unit_of_measurement: "Watt"
  json_attributes_topic: "tele/currentcost/SENSOR"
  json_attributes_template: " {{ {'temp': value_json.msg.tmpr | float, 'ch1':value_json.msg.ch1.watts | int, 'ch2':value_json.msg.ch2.watts | int, 'ch3':value_json.msg.ch3.watts | int } | tojson}} "

And I also got the integration: plugin configured, based on the combined sum, which is where my next question comes in. For some reason, even though I have the prefix configured to k, there is no division of the resulting value, meaning that my resulting consumption in kWh is off by a factor of 1000. Any suggestions on how I can fix the calculation of the integral? And any suggestion how I can graph the individual phases, or should I simply make a template sensor that references the individual attributes?

- platform: integration
  source: sensor.electricity_use
  name: "Energy Consumed"
  unit_prefix: k
  round: 2

See attached image.
Screenshot from 2020-02-27 13-47-26

For those interested, I’ve created a custom component for CurrentCost:

2 Likes

I created an ESPHome/ESP32-based thing to simplify reading the values from the CurrentCost, and reporting them to HA.

You can check out the important parts in this gist: https://gist.github.com/RoganDawes/190cafcd8e2545ccd93628f2f9bfa82c

It parses the XML in the ESP32, and reports individual values for the main sensor (3 phases). It could likely be changed easily enough to report other sensors as well if desired.

If like me your CurrentCost (CC) main control unit has failed then this method is unsuitable.

I very nearly threw out all my CC devices until I realised they operated at 433mHz.

There is an excellent repo at

https://community.home-assistant.io/t/home-assistant-add-on-rtl-433-with-mqtt-auto-discovery/260665

this will enable you to use the CC devices directly without the CC control unit or having to pair the devices. Devices appear as sensors immediately in HA without any configuration.

It will also allow you to read some weatherstations, taking data straight from the sensor array.

You will need an RTL SDR 433mhz dongle.

Highly recommended

1 Like