Skip to content

Instantly share code, notes, and snippets.

@SqyD
Last active September 8, 2024 15:57
Show Gist options
  • Save SqyD/98e00b8f709e0e87f3ec9be08e72568c to your computer and use it in GitHub Desktop.
Save SqyD/98e00b8f709e0e87f3ec9be08e72568c to your computer and use it in GitHub Desktop.
Trafasi: Laundry monitor

Trafasi

Simple power monitor

Hardware:

  • Raspberry Pi Zero
  • lechacal RPIZ CT3T1 power monitor hat for Raspberry Pi Zero
  • Two sct-013-000 clamps to measure the Washer and Dryer power consumption

Software:

  • Using Raspberry OS lite
  • To update and install dependencies:
sudo apt update
sudo apt upgrade
sudo apt install python3-serial git
git clone https://github.com/SqyD/hapy
  • Then make sure the serial port of the GPIO works following these instructions
  • Change the url for your Home Assistant instance in the "secrets" variable.
  • Go to your user profile page in Home Assistant by clicking on your username in the bottom left panel of the HA page and at the bottom of the page hit the Create Token to generate a long lived token. Add the token to the secrets section of trafasi.py file.
  • Create a file trafasi.service in /etc/systemd/system/ using your favorite text editor.
  • Ensure the path to the trafasi.py file is correct. (I just run it from my primary users homedir.)
  • run sudo systemctl daemon-reload && sudo systemctl enable trafasi && sudo systemctl start trafasi

You should be able to check Home Assistant for updates on the device entities

#!/usr/bin/python3
# Simple power monitor for HA
# Found at https://gist.github.com/SqyD/98e00b8f709e0e87f3ec9be08e72568c
# Settings. Change these during setup:
config = {
'tty': '/dev/ttyAMA0',
'update_freq': 60,
'devices': {
'washer': {
'sensor': 3,
'attributes': {
'state_class': 'measurement',
'device_class': 'power',
'unit_of_measurement': 'W'}
},
'dryer': {
'sensor': 2,
'attributes': {
'state_class': 'measurement',
'device_class': 'power',
'unit_of_measurement': 'W'}
},
},
}
secrets = {
'url': 'http://yourhomeassistanthost_or_ip.local:8123',
'access_token': 'changeme',
}
import serial, time
from hapy import hapy
class Device():
def __init__(self, id, config):
self.id = id
self.config = config
self.power_hist = list()
def read(self, raw_data):
power = int(float(raw_data[self.config['sensor']]))
self.power_hist.insert(0, power)
def update(self):
power = int(sum(self.power_hist) / len(self.power_hist))
state_data = dict()
state_data['attributes'] = self.config['attributes']
state_data['state'] = power
ha.entity_set('sensor.' + self.id + '_power', state_data)
self.power_hist = list()
def read_data():
# Read one line from the serial buffer
line = ser.readline().decode().strip()
# Create an array of the data
raw_data = line.split(' ')
return raw_data
## Main routine
ser = serial.Serial(config['tty'], 38400)
ha = hapy.HaPyRest(secrets)
devices = {}
for id, dev_config in config['devices'].items():
devices[id] = Device(id, dev_config)
last_update = time.time()
while True:
raw_data = read_data()
update = (time.time() - last_update) > config['update_freq']
if update:
last_update = time.time()
for device in devices:
devices[device].read(raw_data)
if update:
devices[device].update()
[Service]
Type=simple
ExecStart=/usr/bin/python3 /home/paul/trafasi.py
[Install]
WantedBy=multi-user.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment