From bc3110b08b8deac5b03f2e33a9876ee395bbd99d Mon Sep 17 00:00:00 2001 From: David Wagner Date: Wed, 23 Oct 2024 22:15:02 +0200 Subject: [PATCH] Test sunrise/sunset notifications --- modules/push-notifications.nix | 22 ++++++++++++++++++++++ scripts/push-sunrise-sunset.sh | 24 ++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100755 scripts/push-sunrise-sunset.sh diff --git a/modules/push-notifications.nix b/modules/push-notifications.nix index 4d354fb..8d066d8 100644 --- a/modules/push-notifications.nix +++ b/modules/push-notifications.nix @@ -41,4 +41,26 @@ Unit = "send-room-humidity.service"; }; }; + + systemd.services."sunrise-sunset" = { + path = [ pkgs.sunwait pkgs.curl ]; + script = '' + set -eu + ${../scripts/push-sunrise-sunset.sh} + ''; + serviceConfig = { + Type = "oneshot"; + User = "ntfy"; + }; + }; + + systemd.timers."sunrise-sunset" = { + wantedBy = [ "timers.target" ]; + wants = [ "network-online.target" ]; + after = [ "network-online.target" ]; + timerConfig = { + OnCalendar = [ "*-*-* 5:30" "*-*-* 16:30" ]; + Unit = "sunrise-sunset.service"; + }; + }; } diff --git a/scripts/push-sunrise-sunset.sh b/scripts/push-sunrise-sunset.sh new file mode 100755 index 0000000..60e7c2c --- /dev/null +++ b/scripts/push-sunrise-sunset.sh @@ -0,0 +1,24 @@ +#!/bin/sh + +NTFY_ADDRESS=http://nuc:8080/home-thewagner-ec1 +LAT="46.519833N" +LON="6.6335E" + +now=$(sunwait poll $LAT $LON) + +if [ "$now" = "NIGHT" ]; then + echo "Waiting for sunrise" + HEADER="Sunrise!" + MESSAGE="Good morning! 🌅" +else + echo "Waiting for sunset" + HEADER="Sunset!" + MESSAGE="Good evening! 🌇" +fi + +sunwait wait $LAT $LON + +curl \ + -H "Title: $HEADER" \ + -d "$MESSAGE" \ + "$NTFY_ADDRESS"