Monitoring AC with HomeAssistant and a RPICT hat
We will guide throught to steps to monitor AC installations with a RPICT card and HomeAssistant.
This guide is for forwarding RPICT data to a HomeAssistant server. If you are looking to have the RPICT card hosted on the same Raspberry Pi as the HomeAssistant server than please use this guide instead. Homeassistant_and_RPICT_on_the_same_pi
Preliminaries
We assume that HomeAssistant is already installed. HomeAssisant documentation is here
The RPICT hat is installed on a Raspberry Pi with Raspberry Pi OS ready configured. See this guide here.
The RPICT card and the HomeAassistant server are on a different Raspberry Pi.
Install the basics for HomeAssistant
File Editor
You will have to manually edit the configuration.yaml
file. For the The File Editor is required.
To install it go to Settings -> Add-ons -> Add-on Store.
Search for File and install File Editor.
Make sure you start it once installed and tick Show in sidebar on.
Make sure you can see File editor on the left panel of HomeAssistant.
MQTT
HomeAssistant has its own MQTT broker that we are using here. To install it go to Settings -> Devices & services. Then search for MQTT and install the item shown below.
You can now follow the installation steps for MQTT.
We usually recommend to create a dedicated user for MQTT.
Go to Settings -> People -> ADD PERSON.
Give the new user a name and password. This would commonly be named mqtt-user or similar.
All MQTT configurations can be left as default.
Run the MQTT script
There is a script to run on the Raspberry Pi of the RPICT card to publish data to the HomeAssistant broker.
This script is given below.
MQTT_SERV = "192.168.1.130"
MQTT_PATH = "RPICT_main"
MQTT_USER = "mqtt-user"
MQTT_PASS = "passwd"
CHANNELS = ["NodeID", "P1", "S1", "Q1", "I1", "V1", "PF1", "F1"]
import paho.mqtt.client as mqtt
import serial
import json
ser = serial.Serial('/dev/ttyAMA0', 38400)
client = mqtt.Client("pi-mqtt")
client.username_pw_set(MQTT_USER, MQTT_PASS)
client.connect(MQTT_SERV)
try:
while 1:
# Read one line from the serial buffer
Z = ser.readline().decode().strip().split(' ')
J = json.dumps(dict(zip(CHANNELS, Z)))
# Print it for debug
print(J)
# Publish to the MQTT broker
client.publish(MQTT_PATH, J)
except KeyboardInterrupt:
client.disconnect()
ser.close()
You can directly get this script file using the command below
wget lechacal.com/RPICT/example/ha-mqtt.py
Modify it using nano
nano ha-mqtt.py
Edit the parameters at the top:
MQTT_SERV
is the HomeAssistant hostname or IP address.
MQTT_PATH
is the path on which we are publishing. Can be left as default.
MQTT_USER
is the user that we have created in earlier steps above.
MQTT_PASS
is the password defined for the MQTT user earlier.
CHANNELS
Enter here the list of channels from the RPICT card output.
Now install paho-mqtt with
sudo apt-get -y install python3-paho-mqtt
and run the script using
python3 ha-mqtt.py
Configure HomeAssistant yaml file
Go to File editor and open the file called configuration.yaml
.
Here you can add the lines below to define your MQTT configuration.
mqtt:
sensor:
- name: "Active_Power_1"
state_topic: "RPICT_main"
value_template: "{{ value_json.P1 | float }}"
unit_of_measurement: "W"
- name: "Apparent_Power_1"
state_topic: "RPICT_main"
value_template: "{{ value_json.S1 | float }}"
unit_of_measurement: "VA"
- name: "Reactive_Power_1"
state_topic: "RPICT_main"
value_template: "{{ value_json.Q1 | float }}"
unit_of_measurement: "VAR"
- name: "Current_1"
state_topic: "RPICT_main"
value_template: "{{ value_json.I1 | float }}"
unit_of_measurement: "A"
- name: "Voltage_1"
state_topic: "RPICT_main"
value_template: "{{ value_json.V1 | float }}"
unit_of_measurement: "V"
- name: "Power_Factor_1"
state_topic: "RPICT_main"
value_template: "{{ value_json.PF1 | float }}"
unit_of_measurement: ""
- name: "Frequency_1"
state_topic: "RPICT_main"
value_template: "{{ value_json.F1 | float }}"
unit_of_measurement: "Hz"
You must adapt this to match your RPICT configuration. For each entry you should change name
and value_template
to reflect your own settings.
state_topic
must be the same as MQTT_PATH
defined in the python script.
If you intend to monitor energy usage you can also add this below. This computes energy from a power channel.
sensor:
- platform: integration
source: sensor.Active_Power_1
name: energy_active_power_1
unit_prefix: k
round: 2
max_sub_interval:
minutes: 5
Modify source
to point to a power channel to be converted to energy.
Optionally you can also define icons for each sensor to make your dashboard look great. Add and modify this section below inside the yaml file.
homeassistant:
customize:
sensor.Active_Power_1:
icon: mdi:meter-electric
sensor.Apparent_Power_1:
icon: mdi:meter-electric
sensor.Reactive_Power_1:
icon: mdi:meter-electric
sensor.Current_1:
icon: mdi:current-ac
sensor.Voltage_1:
icon: mdi:flash
sensor.Power_Factor_1:
icon: mdi:math-cos
sensor.Frequency_1:
icon: mdi:sine-wave
Once all set save the file. Then go to Developer tools. Click on CHECK CONFIGURATION and make sure there are no errors here. Modify the configuration file if any before proceeding to what follows.
Then hit RESTART to restart HomeAssistant server and honour the new configuration.
In the Overview dashboard you can now see all sensors being updated:
Run the python script in background
To run the python script in the background you can use the screen
command.
Install Screen.
sudo apt-get install screen
Then run
screen -d -m python3 ha-mqtt.py
This will run the script and detach from the terminal.
To resume the control of the script just run screen
like this.
screen -r
Press ctrl ^C
to kill the program.
Press Ctrl ^A
then D
to let it run and detach from the screen session again.