Skip to content

Instantly share code, notes, and snippets.

@bort9
Forked from themactep/dusk2dawn.sh
Last active July 31, 2024 07:36
Show Gist options
  • Select an option

  • Save bort9/f3517a12f25ade9b38dca000dc181f41 to your computer and use it in GitHub Desktop.

Select an option

Save bort9/f3517a12f25ade9b38dca000dc181f41 to your computer and use it in GitHub Desktop.
Generate cron job lines from sunrise and sunset times using Home Assistant sensor data
#!/bin/sh
#
# Set cron jobs by sunset and sunrise time using sensor data from Home Assistant (HASS)
# Original work by Paul Philippov <[email protected]> - https://gist.github.com/themactep/06f951da3fc05693871cc15aa243dff5
# Create separate entry in your crontab to run this script at midnight (or after DST clock changes) to update the times daily
#
# 2024.06.13.00 Initial version
# 2024.06.13.01 Using universal sun.sun entity from HASS, removed leading zero from printf (%02d) output to crontab
#
# HASS long lived token
# Create a long-lived access token in your HASS instance - https://developers.home-assistant.io/docs/auth_api/#long-lived-access-token
token="insert_your_generated_long_lived_token_here"
# HASS host IP
hass_host="insert_your_HASS_host_IP_here"
crontab_file="/etc/crontabs/root"
at_sunrise_marker="# at sunrise (autogenerated)"
at_sunset_marker="# at sunset (autogenerated)"
# Retrieve sun sensor data from HASS
# Making authenticated requests to your HASS instance - https://developers.home-assistant.io/docs/auth_api/#making-authenticated-requests
json=$(curl --silent -X GET http://$hass_host:8123/api/states/sun.sun -H "Authorization: Bearer $token")
# Sunrise time in UTC from HASS
sunrise_time=$(jsonfilter -s "$json" -e @.attributes.next_rising | awk -F[.] '{print $1}' | sed 's/T/ /')
sunset_time=$(jsonfilter -s "$json" -e @.attributes.next_setting | awk -F[.] '{print $1}' | sed 's/T/ /')
# Convert UTC time to local TZ
# TZ data based on variable set in web UI
sunrise_hour=$(TZ=$TZ date -d "@$(TZ=UTC date -d "$sunrise_time" +'%s')" +'%H:%M' | awk -F[:] '{print $1}' | sed 's/^0//')
sunrise_minute=$(TZ=$TZ date -d "@$(TZ=UTC date -d "$sunrise_time" +'%s')" +'%H:%M' | awk -F[:] '{print $2}' | sed 's/^0//')
sunset_hour=$(TZ=$TZ date -d "@$(TZ=UTC date -d "$sunset_time" +'%s')" +'%H:%M' | awk -F[:] '{print $1}' | sed 's/^0//')
sunset_minute=$(TZ=$TZ date -d "@$(TZ=UTC date -d "$sunset_time" +'%s')" +'%H:%M' | awk -F[:] '{print $2}' | sed 's/^0//')
# Create a working copy of crontab in ram
tmp_crontab_file=$(mktemp)
cp "$crontab_file" "$tmp_crontab_file"
# Delete old jobs
sed -i "/$at_sunrise_marker/,+1d" "$tmp_crontab_file"
sed -i "/$at_sunset_marker/,+1d" "$tmp_crontab_file"
# Create new marked jobs
{
echo "$at_sunset_marker"
printf "%d %d * * * daynight night\n" "$sunset_minute" "$sunset_hour"
echo "$at_sunrise_marker"
printf "%d %d * * * daynight day\n" "$sunrise_minute" "$sunrise_hour"
} >>"$tmp_crontab_file"
# Replace actual crontab file with the updated one
cp "$tmp_crontab_file" "$crontab_file"
echo "dusk2dawn. All done. Bye bye."
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment