Kiosk mode dashboard on Raspberry Pi 4 running Raspberry Pi OS

Outline

When we switch on the dinner table lights, our playlist starts playing on a media player in the dining room. My wife often asks “what is that song?” and I don’t know. So I built a small screen to display a “currently playing” media card in full screen.

The solution runs on a Raspberry Pi 4 running Raspberry Pi OS and Chromium to display the HA dashboard.

An extra feature is that HA will switch the display on and off as needed.

Dashboard

Create the dashboard in HA as you want it to be. If you want the top and side menu to go away, install Kiosk Mode through HACS. Then you can add “?kiosk” to any dashboard URL to remove everything but the dashboard content.

Pi4 and screen for running Dashboard

Find a Pi4 somewhere. Less will probably work, but it needs enough resources to run Chromium. Mine had 4GB and worked fine. Install latest version of Raspberry Pi OS. Enable SSH (needed) and VNC (ease of use). If you need to make HA do stuff like switching the display on and off (more on that below), make sure it has a static IP or DHCP reservation for HA to talk to it. Add it to DNS too if you are rocking that locally.

Get a screen from somewhere that fits your needs. I needed a smaller screen, so I got this: Aurevita 7" Mini HDMI Monitor Portable VGA with AV BNC 1024x600 LCD Display with Speakers 178° Viewing Angle IPS Multifunction: Amazon.de: Computer & Accessories. Mine doesn’t have touch, so there’s nothing about touch in the following. Ideally, you want a display that supports HDMI CEC so you can turn it off and on through the HDMI port of your Pi. Mine also supports USB power so it can be powered from the Pi requiring less wire clutter.

Kiosk Service

Create kiosk.sh in your home directory. Replace “/home/user/” with the path to your home directory. Replace the url to Home Assistant with your own, and replace the “your-dash-url” with the link to your own dashboard:

#!/bin/bash
 
# Change some Chromium prefs to clear out warning bars from displaying
sed -i 's/"exited_cleanly":false/"exited_cleanly":true/' /home/user/.config/chromium/Default/Preferences
sed -i 's/"exit_type":"Crashed"/"exit_type":"Normal"/' /home/user/.config/chromium/Default/Preferences
 
# Launch Chromium in Kiosk Mode
/usr/bin/chromium --noerrdialogs --disable-infobars --kiosk http://homeassistant.lan:8123/lovelace/your-dash-url?kiosk

Make it executable

chmod +x kiosk.sh

Add it as a service to the system. Start by creating a new file called /usr/lib/systemd/system/kiosk.service. Paste the stuff below, but remember to replace “user” below with your username.

[Unit]
Description=Chromium Kiosk
Wants=graphical.target
After=graphical.target

[Service]
Environment=DISPLAY=:0.0
Environment=XAUTHORITY=/home/user/.Xauthority
Type=simple
ExecStart=/bin/bash /home/user/kiosk.sh
Restart=on-abort
User=user
Group=user

[Install]
WantedBy=graphical.target

Enable the service:

sudo systemctl enable kiosk.service

You can reboot to see if it starts, or just start it manually:

sudo systemctl start kiosk.service

Hide mouse pointer in Wayland

This took a long time to figure out, since auto hiding the pointer is not a feature of labwc (the window compositor running under Wayland on Raspberry Pi OS version Trixie and later). The work-around is to configure a key kombination (Alt-Win-h) that hides the pointer and then tell labwc to press those keys when starting up.

Don’t worry about the “Win” key. You don’t need a keyboard with a Windows key for this to work.

Please note that you need to remove the openbox_config end tag from the second (long) line. It is shown below, so don’t just cut’n’paste the text!

Edit ~/.config/labwc/rc.xml:

<?xml version="1.0"?>
<openbox_config xmlns="http://openbox.org/3.4/rc"><theme><font place="ActiveWindow"><name>Nunito Sans</name><size>12</size><weight>Light</weight><slant>Normal</slant></font><font place="InactiveWindow"><name>Nunito Sans</name><size>12</size><weight>Light</weight><slant>Normal</slant></font><name>PiXonyx</name></theme> ### REMOVE OPENBOX END TAG FROM HERE ###

  <keyboard>
    <keybind key ="A-W-h">
      <action name="HideCursor"/>
    </keybind>
  </keyboard>

</openbox_config>

Put this in ~/.config/labwc/autostart. The file might not be there, so just create it:

wtype -M alt -M logo -P h

Install wtype to make it work:

sudo apt install wtype

Reboot and marvel at the missing mouse pointer.

Home Assistant to toggle display on and off

My specific use case requires the screen to be on while we’re seated at the dinner table, but off when we’re not in the room listening to music. So, when somebody turns on the light, the music player starts and the screen turns on. Conversely, when somebody turns off the light, the music stops and the screen turns off.

This requires configuration on the the Pi running the display and on Home Assistant. Follow closely.

On the Pi running the display

This is accomplished by a local script on the Pi running the dashboard. While it is possible to have an off and an on script, my display failed horribly if it got turned off twice in a row. Don’t know if it’s just mine or a general Wayland issue, but I opted for a toggle instead to make it more robust. So, whenever somebody interacts with the light switch, it toggles the display on or off.

Create display-toggle.sh in your home folder on the Pi running the dashboard:

#!/bin/bash
export WAYLAND_DISPLAY=wayland-0
export XDG_RUNTIME_DIR=/run/user/1000
/usr/bin/wlr-randr --output HDMI-A-1 --toggle

Clever people can figure out that there are also --on and --off switches to play with.

Make the file executable

chmod +x display-toggle.sh

Make sure this works before continuing. Executing the script should result in the display switching between on and off.

On Home Assistant

Over to Home Assistant. Install Terminal & SSH in Apps, if you don’t already have it. SSH into your home assistant and create a directory for shell commands. You’ll probably only have this there, but if you get adventurous you now have the structure in place.

mkdir /config/shell

Now for a tricky part. You need SSH key authentication from Home Assistant towards the Pi running the display so you (or rather HA) can log in and do stuff without supplying a password. It gets more complicated by the fact the the regular key store on HA is not persistent, so you need to place your keys elsewhere. This is documented in detail at Shell Command - Home Assistant. Check out the “Using SSH with shell_command” section. In summary:

Create your private and public key for HA to authenticate itself towards the Pi:

ssh-keygen -t rsa -f /config/.ssh/id_rsa -C "homeassistant"

Use the command below to copy the public key to your Dashboard Pi (you need to login with password for this to complete, but afterwards you’ll never need a the password again). Remember to replace “user@pi-address” with your username and IP address on the Pi:

ssh-copy-id -i /config/.ssh/id_rsa -o UserKnownHostsFile=/config/.ssh/known_hosts user@pi-address

Test it out by typing the below command and press enter:

ssh -i /config/.ssh/id_rsa -o UserKnownHostsFile=/config/.ssh/known_hosts user@pi-address

You should now be logged into the Pi. Type logout to log out and return to the HA session.

Create a new script to toggle the display on the Pi. Remember, we’re still working in SSH on Home Assistant. Call it something like dashboard_toggle.sh. I think you need to avoid using hyphens, so stick to underscores if you need to separate words.

Put this into your new script, replacing “user” and “pi-address” with your local values:

ssh -i /config/.ssh/id_rsa -o UserKnownHostsFile=/config/.ssh/known_hosts user@pi-address ./display-toggle.sh

Save it and make it executable

chmod +x ./display-toggle.sh

Try to run it:

./display-toggle.sh

If you did everything well, you should see the display attached to the Pi transition from on to off or the other way. Try it a few times just for the thrill of it.

To get this script into a Home Assistant automation, you need to add it to configuration.yaml:

 shell_command:
   dashboard_toggle: bash /config/shell/dashboard_toggle.sh

Restart Home Assistant and if all is well you now have an Action called “dashboard_toggle” that you can use within automations.

Have fun!

1 Like