Post

PIR Motion Sensor for Pi

mrpir

Python service to support a PIR sensor on SBC’s and publish over MQTT with Home Assistant discovery support

The following environment variables are used and should be stored in an “.env” file:

1
2
3
4
5
6
7
8
9
10
MQTT_USER_NAME=xyz
MQTT_PASSWORD=xyz
MQTT_DEVICE=xyz
MQTT_CLIENT_ID=xyz
MQTT_PORT=1883 or other if configured differently
MQTT_BROKER=servername or IP of the MQTT broker
PIR_PIN=signal pin connected to the presense sensor
MQTT_LOGGING=0 or 1 
XSCREENSAVER_SUPPORT=1
LOGGING_LEVEL=1

Make sure the python dependencies are installed. Also, using python env is recommended

1
    python -m venv ~/mrpir

Now move into the mrpir directory

1
2
    cd ~/mrpir
    sudo pip3 install PyYAML paho-mqtt python-decouple sd-notify

Don’t forget to pull the latest from the repo

Configure a raspberry pi as a KIOSK for Home Assistant

Create a startup script

  1. Edit your crontab list by typing:
    1
    
     sudo crontab -e
    

    You can launch crontab without entering sudo, but if you do, you won’t be able to run scripts that require admin privileges. In fact, you get a different list of crontabs if you don’t use sudo so don’t forget to keep using it or not using it.

  2. Add a line at the end of the file that reads like this. Replace “pi” with your username
    1
    
     @reboot python3 /home/pi/startup.sh
    
  3. Create the file: /home/pi/startup.sh
  4. Run
    1
    
     nano ~/startup.sh
    
  5. Add two lines:
    1
    2
    
     echo "${usb_flag}" | sudo tee /sys/devices/platform/soc/3f980000.usb/buspower >/dev/null
     sudo tvservice --off
    
  6. make the startup file executable:
    1
    
     sudo chmod a+x ~/startup.sh
    

    Remove the cursor on the PI screen

    1
    
    sudo sed -i -- "s/#xserver-command=X/xserver-command=X -nocursor/" /etc/lightdm/lightdm.conf
    

    also see: how to permanently hide mouse pointer or cursor on raspberry pi

Or go the app route

1
sudo apt install xdotool unclutter

Automatically enter the kiosk mode and launch homeassistant url

  1. Create a file called mrscreen.desktop (or something else .desktop) in the /etc/xdg/autostart/ directory.
    1
    
    sudo nano /etc/xdg/autostart/mrscreen1.desktop
    
  2. Use the following layout in the myapp.desktop file.
    1
    2
    
    [Desktop Entry]
    Exec=chromium-browser --noerrdialogs --disable-infobars --ignore-certificate-errors --kiosk https://homeassistant.mjsquared.net
    

Add xscreensaver

1
2
3
sudo apt update
apt-cache search xscreensaver*
sudo apt install xscreensaver*

If you have issues with the service controlling the screen, Run from nvc or on machine, not ssh

1
xhost +local:pi (this will let the mrpir services interact with the screensaver)

Test xscreensarver

1
2
xscreensaver-command -display ":0.0" -activate
xscreensaver-command -display ":0.0" -deactivate

Start mrpir:

1
2
sudo nano ~/.config/systemd/user/mrpir.service (for local user install)
sudo nano /lib/systemd/system/mrpir.service (for global system install)

Past in the following (or just pull from the repo):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[Unit]
Description=Presense sensor
After=multi-user.target
Wants=network-online.target systemd-networkd-wait-online.service

StartLimitIntervalSec=500
StartLimitBurst=15

[Service]
Type=idle
Restart=on-failure
RestartSec=5s

ExecStart=/usr/bin/sudo /usr/bin/python3 /home/pi/mrpir/mrpir.py (Or path to where you installed the service)
User=pi (or another username)

[Install]
WantedBy=multi-user.target

Configure the service file with the right permissions

1
2
sudo chmod 644 ~/.config/systemd/user/mrpir.service (for local user install)
sudo chmod 644 /lib/systemd/system/mrpir.service (for global system install)

Link to the right direcotry for global system install

1
ln -s “$(pwd)/mrpir.service” /lib/systemd/system/mrpir.service

Reload and enable the service for local user install. Also, allow the user account systemd instance to start without an active login with loginctl.

1
2
3
sudo systemctl --user daemon-reload
sudo systemctl --user enable mrpir.service
sudo loginctl enable-linger $USER

Reload and enable the service for global system install

1
2
sudo systemctl daemon-reload
sudo systemctl enable mrpir.service

Reboot just to ensure we are all set sudo reboot

Checking the log

1
journalctl -u mrpir.service -n 100

Exit kiosk mode from ssh

1
pkill chromium
This post is licensed under CC BY 4.0 by the author.