This script is a variant on a theme of the Python script from the Domoticz Wiki which allows a virtual switch (or switches) to be updated in Domoticz based upon the output of a the Python script.
This variant uses systemd rather than cron and is (in my opinion) more flexible than the existing systemd variant referenced by the Wiki
- If Python isn't already installed on your system, install it. The script requires Python 2 rather than the newer Python 3 a present
sudo apt install python
- Clone this repository somewhere writable (e.g. /home/pi)
git https://github.com/DJBenson/domoticz-things.git /home/pi/domoticz-things
- Copy the check_device_online.py somewhere safe
cd /home/pi/domoticz-things/presence-systemd cp check_device_online.py /home/pi/domoticz/scripts
(the rest of the instructions assume you are still in /home/pi/domoticz-things/presence-systemd as the current directory)
- Edit the config file to meet your requirements. The file contains three parameters; IDX (the switch ID of the virtual switch in Domoticz), INTERVAL (how often the device should be pinged, default 10 seconds) and COOLDOWN (how long after going offline should that status be reported to Domoticz, default 120 seconds).
nano ./etc/default/presence@example
- Copy the config file to /etc/default (requires root priveleges). The bit after the '@' symbol is the device name/IP address so when you copy the file to its final destination, use something meaningful for this name. In this example I use 'iphone'. I recommend using your devices DNS hostname rather than its IP address so that if it gets a new IP, your address still works. On iOS, the device name in Settings > General > About > Name is the hostname. Android devices probably have a similar setting.
sudo cp ./etc/default/presence@example /etc/default/presence@iphone
- Edit the systemd unit file ensuring the script location is correct (change the path to whatever you set it to in step 3)
nano ./etc/systemd/system/[email protected]
[Unit]
Description=Monitor presence by arping %i
After=network.target
[Service]
Environment="LOCAL_ADDR=localhost"
EnvironmentFile=/etc/default/presence@%i
ExecStart=/usr/bin/python2 /home/pi/domoticz/scripts/check_device_online.py %i ${IDX} ${INTERVAL} ${COOLDOWN}
ExecStopPost=/bin/rm /home/pi/domoticz/scripts/check_device_online.py_%i.pid
RestartSec=5
Restart=always
Type=simple
MemoryHigh=10M
MemoryMax=15M
TasksMax=3
[Install]
WantedBy=multi-user.target
The "MemoryHigh", "MemoryMax" and "TasksMax" were put in place to stop the script running away with resources becasue it spawns additional shell processes which seemed to be consuming increasing amounts of memory. These defaults should keep each process to about 10 megabytes of RAM.
- Copy the systemd service file to its final destination
sudo cp ./etc/systemd/[email protected] /etc/systemd/system
- Start the service
sudo systemctrl start [email protected]
- Confirm the service is running
systemctrl status [email protected]
domoticz@raspberrypi:~$ systemctl status [email protected] ● [email protected] - Monitor presence by arping iphone Loaded: loaded (/etc/systemd/system/[email protected]; enabled; vendor preset: enabled) Active: active (running) since Fri 2020-02-07 00:35:31 GMT; 16h ago Main PID: 83293 (python2) Tasks: 1 (limit: 3) Memory: 9.8M (high: 10.0M max: 15.0M) CGroup: /system.slice/system-presence.slice/[email protected] └─83293 /usr/bin/python2 /home/pi/domoticz/scripts/check_device_online.py iphone 78 10 120
- Enable the service to start at boot
sudo systemctrl enable presence@iphone
Repeat the instructions from step 4 onwards for each device you wish to monitor. You will only ever have one service file in /etc/systemd/system but you will have multiple config files in /etc/default which will be used by the systemd service as an alias. Say for example you want to add a new device called 'android', simply repeat from step 4 creating a new file called presence@android in /etc/default then enable the service by issuing sudo systemctrl enable [email protected].