Skip to content

Instantly share code, notes, and snippets.

@geekman
Created January 20, 2023 12:52
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 geekman/4f0d7036716d12309fe4f7eb1a384a20 to your computer and use it in GitHub Desktop.
Save geekman/4f0d7036716d12309fe4f7eb1a384a20 to your computer and use it in GitHub Desktop.
systemd timers for vacation lights via zigbee2mqtt

This setup relies on having Zigbee switches, connected to zigbee2mqtt. systemd timers just publish messages to the MQTT broker and z2m acts on them.

Setup the systemd unit files, service and timers. Remember to daemon-reload afterwards. (The names are a leftover from my 433MHz RF switch)

# head -100 /etc/systemd/system/lightswitch*
==> /etc/systemd/system/lightswitch-off.timer <==
[Unit]
Description=sends RF switch signal OFF

[Install]
WantedBy=timers.target

[Timer]
OnCalendar=*-*-* 23:45:00
RandomizedDelaySec=15min
Unit=lightswitch@OFF.service


==> /etc/systemd/system/lightswitch-on.timer <==
[Unit]
Description=sends RF switch signal ON

[Install]
WantedBy=timers.target

[Timer]
OnCalendar=*-*-* 19:08:00
RandomizedDelaySec=8min
Unit=lightswitch@ON.service


==> /etc/systemd/system/lightswitch@.service <==
[Unit]
Description=sends RF switch signal

[Service]
Type=oneshot
Environment=MQTT_USER=bob
Environment=MQTT_PASS=s3cr3tPassw0rd
Environment=MQTT_TOPIC=zigbee2mqtt/0x54ef111000441133/set
ExecStart=mosquitto_pub -i systemd_timer -t "${MQTT_TOPIC}" -u ${MQTT_USER} -P ${MQTT_PASS} -m '{"state_right":"%i"}'

Enable the timers:

# systemctl enable --now lightswitch-on.timer lightswitch-off.timer
Created symlink /etc/systemd/system/timers.target.wants/lightswitch-on.timer → /etc/systemd/system/lightswitch-on.timer.
Created symlink /etc/systemd/system/timers.target.wants/lightswitch-off.timer → /etc/systemd/system/lightswitch-off.timer.

Check the status of the timers:

# systemctl status lightswitch-{on,off}.timer
● lightswitch-on.timer - sends RF switch signal ON
   Loaded: loaded (/etc/systemd/system/lightswitch-on.timer; enabled; vendor preset: disabled)
   Active: active (waiting) since Fri 2023-01-20 18:31:05 +08; 4s ago
  Trigger: Fri 2023-01-20 19:09:45 +08; 38min left

● lightswitch-off.timer - sends RF switch signal OFF
   Loaded: loaded (/etc/systemd/system/lightswitch-off.timer; enabled; vendor preset: disabled)
   Active: active (waiting) since Fri 2023-01-20 18:31:05 +08; 4s ago
  Trigger: Fri 2023-01-20 23:54:32 +08; 5h 23min left

You can also manually trigger them by typing:

# systemctl start lightswitch@ON
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment