A gateway to publish Current Cost energy monitor values to an MQTT broker.
First of all I have to say Thanks to lolouk44 and Robin Wilson for the code my program is based on.
At first time I used the lolouk44 home assistant custom component to integrate my Current Cost Envi energy monitor in home assistant but I had some issues when I restart Home Assistant and I had to restart it several times until the energy values are correctly showed. Appart of it I have interest to move the Envi far from the server where Home Assistant runs so I started to look for other possibilities. In any case thanks to lolouk44 for the great support he given me.
My home automation system is mainly based on ZWave technology and recently I moved from the ZWave integration of Home Assistant to a Zwave to MQTT gateway running on a Raspberry Pi. It allows me to improve the performance of the ZWave network because I moved my Zwave stick to a better location, so I started thinking to move the Current Cost monitor to the Raspberry Pi for send its values through the MQTT broker to Home Assistant.
-
First, update your distribution.
sudo apt update sudo apt upgrade
-
Install Python pip and some dependencies.
sudo apt install python3-pip pip3 install pyserial pip3 install untangle pip3 install paho-mqtt
-
Install git and clone the repository.
sudo apt install git cd~ git clone https://github.com/homected/CurrentCost2Mqtt.git currentcost2mqtt
-
Set your own configuration parameters editing the program file.
cd currentcost2mqtt sudo nano currentcost2mqtt.py
You have to replace the text between quotes with the correct values for your configuration:
- COM_PORT: Something like /dev/ttyUSB0 or COM1 or /dev/serial/by-id/usb-Prolific_Technology_Inc._USB-Serial_Controller-if00-port0;
- MQTT_Host: The IP Address of the MQTT broker;
- MQTT_Port: Port of the MQTT broker, for example 1883;
- MQTT_User: Username to authenticate into the MQTT broker;
- MQTT_Password: Password to authenticate into the MQTT broker;
- MQTT_Topic: The topic of the MQTT broker where publish the data;
- MQTT_QoS: Quality of Service level (0, 1 or 2);
- MQTT_Retain: True for retain MQTT messages of False for not retain;
Save and close the file with: Control+O, Enter, Control+X
-
Optionally, you can set this program runs automatically when the raspberry boots with these commands:
sudo cp currentcost2mqtt.service /lib/systemd/system/currentcost2mqtt.service sudo chmod 644 /lib/systemd/system/currentcost2mqtt.service sudo systemctl daemon-reload sudo systemctl enable currentcost2mqtt.service
-
For start manually the program you can use this command:
python3 currentcost2mqtt.py
If you set the program to run after boots, reboot the raspberry with this command:
sudo reboot
After boot, you can control the process with these commands:
sudo systemctl start currentcost2mqtt sudo systemctl status currentcost2mqtt sudo systemctl stop currentcost2mqtt
The values of the energy monitor will be published under the topic set in MQTT_Topic inside the program file and these are the published endpoints:
- Monitor temperature: MQTT_Topic/CurrentCost/Temperature
- Total power: MQTT_Topic/CurrentCost/Power/Total/chX, where X is the channel number (1, 2 or 3).
- Appliance power: MQTT_Topic/CurrentCost/Power/ApplianceY/ChX, where Y is the appliance number (1 to 9) and X is the channel number (1, 2 or 3).
- Impulse sensor: MQTT_Topic/CurrentCost/Meter/SensorY, where Y is the impulse meter sensor number (1 to 9).
To get the values of the energy monitor in Home Assistant, enter a sensor entry for each monitored value in your configuration.yaml file:
sensor:
- platform: mqtt
state_topic: "MQTT_Topic/CurrentCost/Temperature"
name: "currentcost_temperature"
unit_of_measurement: "°C"
value_template: '{{ value_json.state }}'
- platform: mqtt
state_topic: "MQTT_Topic/CurrentCost/Power/Total/Ch1"
name: "currentcost_totalpower"
unit_of_measurement: "W"
value_template: '{{ value_json.state }}'
- platform: mqtt
state_topic: "MQTT_Topic/CurrentCost/Power/Appliance1/Ch1"
name: "currentcost_power1"
unit_of_measurement: "W"
value_template: '{{ value_json.state }}'