I have been trying to create a room control screen so that I can control the smart elements of each of my rooms.
The basis for the control screen is a raspberry pi with touchscreen that was donated to me.
To create the control screen as follows:
-
I created a raspberry pi in kiosk mode based on these instructions: https://desertbot.io/blog/raspberry-pi-touchscreen-kiosk-setup
-
KIOSK_URL can be to your Lovelace or in my case it is to my TileBoard - New dashboard for Homeassistant
-
I created a new script and saved it to /home/pi/touch-screen-power.py. The script is as follows:
#!/usr/bin/python
import paho.mqtt.client as mqtt
import sys
import signal
import os
from subprocess import call
import time
def sigint_handler(signal, frame):
print("Exiting")
sys.exit(0)
signal.signal(signal.SIGINT, sigint_handler)
def monitor_on():
print("Switch on")
call('XAUTHORITY=~pi/.Xauthority DISPLAY=:0 xset dpms force on',shell=True)
client.publish("touchscreen/state", "ON")
def monitor_off():
print("Switch off")
call('XAUTHORITY=~pi/.Xauthority DISPLAY=:0 xset dpms force off',shell=True)
client.publish("touchscreen/state", "OFF")
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe("touchscreen/cmd/#")
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload.decode("utf-8")))
if msg.topic == 'touchscreen/cmd':
if str(msg.payload.decode("utf-8")) == 'ON':
print("Monitor On")
monitor_on()
if str(msg.payload.decode("utf-8")) == 'OFF':
print("Monitor Off")
monitor_off()
# Set up and connect to MQTT broker
client = mqtt.Client(client_id="<client_id>")
client.username_pw_set("<username>", password="<password>")
client.on_connect = on_connect
client.on_message = on_message
client.connect("<IP>", 1883, 60)
print("Running...")
client.publish("touchscreen/MQTTclient/state", "ON")
client.loop_start()
def status_on():
print("Switch check on")
client.publish("touchscreen/state", "ON")
def status_off():
print("Switch check off")
client.publish("touchscreen/state", "OFF")
while True:
print("Switch check")
p = subprocess.Popen('vcgencmd display_power', shell=True, stdout=subprocess.PIPE).communicate()[0]
r=p.rstrip()
check=r.decode("utf-8")
if check == "display_power=0":
status_off()
if check == "display_power=1":
status_on()
client.disconnect()
client.loop_stop()
-
Started the Mosquitto broker Add-on on Home Assistant.
-
Installed python and paho on the raspberry pi:
sudo apt-get install python3
pip3 install paho-mqtt
- Installed Mosquitto broker on the raspberry pi:
sudo apt-get install mosquitto clients
- Open openbox autostart:
sudo nano /etc/xdg/openbox/autostart
-
Add the python script to openbox autostart by adding:
python3 /home/pi/touch-screen-power.py
abovechromium-browser --noerrdialogs --disable-infobars --kiosk $KIOSK_URL
-
I then added the following to my configuration.yaml:
switch:
- platform: mqtt
unique_id: touch_screen
name: "Touch Screen Power"
state_topic: "touchscreen/state"
command_topic: "touchscreen/cmd"
payload_on: "ON"
payload_off: "OFF"
state_on: "ON"
state_off: "OFF"
All of this gives me a screen I can turn on and off from home assistant.
My questions, as this is the first time using MQTT and Python:
-
My script seems to work but it was copy and pasted from a few other sources, are there any obvious flaws (we all have to start somewhere and I am still learning)?
-
Do I need mosquitto clients on the pi if I have paho?
-
Due to the
while True:
in the python code I get hundreds of MQTT messages. This seems useful as my switch is always in the correct state, even if I control it manually. Is this a problem? Will it reduce performance? etc.
Thanks all.