State Listener for Home Assistant

Mar 26, 2021

In this tutorial you’ll be setting up a Home Assistent state listener with Python powered by the Home Asssistent websocket API. Each script contains of two parallel processes:

  • Logger: when configured states change value the logger starts outputting these.
  • Listener: the handler to listen and write states changes to memory.

Requirements

  • An environment able to run python(3) scripts.
  • Optional: For the log.phat.py you’ll need a Micro Dot pHAT on a Raspberry Pi (zero).

Installation

Install the script on your environment and make sure you’ve noted you’re Home Assistant profile and configuration details: Specify these in the script you’re about to run:

# check {host}}:{port}/profile to generate an Auth token
token = HASS_IO_AUTHORIZATION_TOKEN
port = HASS_IO_PORT
host = HASS_IO_HOSTNAME_OR_URL

Provide the states the listener should listen to. These will be added to the logger output.

entities = [
    "sensor.power_tariff",
    "sensor.power_consumption_watt",
    "sensor.gas_consumption",
    ...
]

Install the dependencies if not already on your environment:

pip install asyncio && pip3 install asyncws

Optional: in case of using the Micro Dot pHAT, install their library as well:

curl https://get.pimoroni.com/microdotphat | bash

Usage

Run logger

Screenshot of the log output

# Run logger or pHAT logger 
python3 log.py
python3 log.phat.py

# Output Logger
Start logger...
Start socket...
writing sensor.power_tariff to cache
writing sensor.gas_consumption to cache
writing sensor.power_consumption_watt to cache
low
6692.597 m3
372 W

Run pHAT logger after boot

Add a startup cronjob to the crontab of your system

# Open de crontab file
crontab -e

# Add to bottom:
@reboot sleep 30 && python3 /home/pi/log.phat.py & 2>&1 >> /home/pi/log.phat.log

Jeroen Boumans

Backend & App Developer