Skip to content

Instantly share code, notes, and snippets.

@pkscout
Last active April 7, 2023 12:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pkscout/d35adeb3effdee6647fd4c5d3d422f94 to your computer and use it in GitHub Desktop.
Save pkscout/d35adeb3effdee6647fd4c5d3d422f94 to your computer and use it in GitHub Desktop.
Simple Weather Clock

Simple Weather Clock

Introduction

I have always wanted a clock that gives me a little weather info. The problem is that most weather stations want to give you 40 different pieces of information in little text that you have to be standing right next to read, and I wanted to be able to see this sitting on my couch. Thus was born the Simple Weather Clock. This gist documents the various pieces of hardware and software needed to construct it.

simple-weather-clock

Hardware

The core of the weather clock is a Raspberry Pi. All this should work with any Raspberry Pi model as long as it has network connectivity of some sort. If you want to do auto dimming of the display, you need the official Raspberry Pi 7" touchscreen and a Pi Camera (or I2C light sensor). Here's the full list of hardware I use:

Assembly

The easist thing to do is to follow the instructions provided with the case.

Prerequisites

This weather clock uses Home Assistant as both the backend for weather information and the front end display. This gist isn't meant to be a complete guide to Home Assistant. It is assumed you already have that running and understand how it works.

Home Assistant Setup

Except the auto login stuff, everything here should just need to be done once in Home Assistant so that the clocks can login and display the clock dashboard.

Additional Font

The clock interface uses a monospace font, IBM Plex Mono. You need to go to the Dashboard area of the Home Assistant settings, then Resources (currently in the three dot menu), then add the following CSS Stylesheet:

https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;700&display=swap

Required mods

The Lovelace clock interface uses a few mods, all of which can be installed using HACS.

Clock Configuration

There are instructions on configuring the Simple Weather Clock card at the link above.

Auto Login

The clock needs to be able to login to Home Assistant automatically. To do that you need to enable trusted network login by adding some information to your configuration.yaml file.

homeassistant:
  auth_providers:
    - type: trusted_networks
      trusted_networks:
        - 172.16.1.15/32
        - 172.16.1.16/32
        - 172.16.1.19/32
      trusted_users:
        172.16.1.15: 2c7683a997514012854057f0g1c2cacc
        172.16.1.16: bcaf233164354f988e489a5d1745c83f
        172.16.1.19: c02fc848d0c7418233e280e34609b517
      allow_bypass_login: true
    - type: homeassistant

When you add the above and reboot Home Assistant, you will notice when you login from other locations that you get a message saying you aren't logging in from a trusted network. There is an option to switch over to the regular Home Assistant login. If you do that you can login as normal. There isn't really a great way to work around that issue that I've found. It's just one extra login step for my other devices, so I live with it.

The above file allows three IPs to auto login using a specific user. that long string after the IP address in the trusted_users section is the unique ID for a user. After you create a user, you can get that in the user detail screen for the user. I have each clock login as it's own user so that the clock can get indoor temperature/humidity information for the room in which it lives. If you only have one set of indoor temperature information, you could have all the clocks you use login with the same user.

Raspian Setup

Once you've assembled the hardware and put Raspian on the SD card, plug it in to boot up. You should end up as the Raspian desktop screen. If you didn't use the Raspberry Pi imager setup options to get the device on the wireless network, you have to do that at a minimum. Some of the steps below assume you have SSH enabled on the device as well. Again, either enable that during imaging or use raspi-config to do it after your first boot. You should also do a quick upgrade to make sure everything is up to date:

sudo apt-get update
sudo apt-get full-upgrade

Finally, you should use a statically assigned DHCP address for the clock (or a manually assigned one). To do Home Assistant Auto Login you need the device to have the same IP every time it boots.

Setting Up for Auto Boot

You really don't want to have to launch the web browser by hand every time your clock reboots, so let's set things up so it'll launch automatically at boot and go right to the clock dashboard.

First, you need to install unclutter (which will hide the mouse pointer):

sudo apt-get install unclutter

Now create a script in the root of the pi user directory called kiosk.sh. The command line below to start Chromium starts it in kiosk mode and hides the scrollbar so that you get a true fullscreen look. You'll need to edit the script and put in the IP address or name of your Home Assistant web interface where it says <ha_ip>. The script also assumes you named your dashboard clock. If you didn't, you need to change the URL to point to the right dashboard. This is adapted from the article Raspberry Pi Kiosk using Chromium on PiMyLifeUp.

#!/bin/bash
xset s noblank
xset s off
xset -dpms

unclutter -idle 0.5 -root &

sed -i 's/"exited_cleanly":false/"exited_cleanly":true/' /home/pi/.config/chromium/Default/Preferences
sed -i 's/"exit_type":"Crashed"/"exit_type":"Normal"/' /home/pi/.config/chromium/Default/Preferences

/usr/bin/chromium-browser --noerrdialogs --enable-features=OverlayScrollbar --disable-infobars --kiosk http://<ha_ip>:8123/lovelace-clock/default_view

The last thing we need is a service that will run this script at startup. Create the needed file:

sudo nano /lib/systemd/system/kiosk.service

and here's the file:

[Unit]
Description=Chromium Kiosk
After=graphical.target
After=network-online.target

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

[Install]
WantedBy=graphical.target

This ensures both the GUI and the network are available before starting the service. There is also a 15 second delay after start. This was added because sometimes the screensaver seems to not get into a good place after a cold boot right away, and that causes the screensaver to blank the screen after the clock starts even though the script explicitly trys to turn it off.

Save it and then enable the service:

sudo chmod 644 /lib/systemd/system/kiosk.service
sudo systemctl daemon-reload
sudo systemctl enable kiosk.service

From now on the browser will launch at startup. To stop/start it manually you can use:

sudo systemctl stop kiosk.service
sudo systemctl start kiosk.service
sudo systemctl restart kiosk.service

Auto Dimming the Screen

To auto dim the screen based on light levels in the room, use my rpi.screencontrol Python script. All the instructions to set that up are in the read me. You can even have the script send light level and light information back to Home Assistant using MQTT so that you can use the light levels in automations.

Added Bonus

With the demise of the Home Assistant Bluetooth tracker, you can use something like room-assistant or my Presence Tracker Python script to turn your weather clock into a Bluetooth tracker as well. I mostly use room-assistant now. It's a little harder to get setup, but a better tool overall.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment