Last active
March 10, 2024 01:27
-
-
Save ivan/f25ff3a85fd94b1a7fb3260b95f810de to your computer and use it in GitHub Desktop.
Fridge software thermostat using Home Assistant + Zooz ZSE44 + Zooz ZEN15 800LR
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env zsh | |
# We use zsh instead of bash so that decimals work in $(( )) | |
set -eu -o pipefail | |
ha="http://192.168.10.2:8123" | |
# Expires 2034 | |
token="..." | |
healthchecks_endpoint="https://hc-ping.com/..." | |
fridge_power_on_temp=4.7 | |
fridge_power_off_temp=2.3 | |
zmodload zsh/zselect | |
sleep() { | |
local t=$(($1 * 100)) | |
t=${t/.*/} | |
zselect -t $t || true | |
} | |
curl-with-args() { | |
curl -s \ | |
-H "Content-Type: application/json" \ | |
-H "Authorization: Bearer $token" \ | |
"$@" | |
} | |
get-sensor-temperature() { | |
curl-with-args "$ha/api/states/sensor.temperature_humidity_xs_sensor_air_temperature_3" | jq -r .state | |
} | |
# We used an Aranet4 and the clock-temperature thing to determine that | |
# the Zooz sensors read 1.1 C (when warming) to 3.4 C higher (when cooling) | |
# than what objects in the fridge are actually cooled to | |
get-actual-coldest-temperature() { | |
local switch_state=$(get-power-switch-state) | |
local offset | |
if [[ $switch_state = "off" ]]; then | |
# definitely warming | |
offset=2.2 | |
else | |
# maybe cooling | |
offset=3.2 | |
fi | |
#>&2 echo $offset | |
echo $(( $(get-sensor-temperature) - $offset )) | |
} | |
get-power-switch-state() { | |
curl-with-args "$ha/api/states/switch.power_switch" | jq -r .state | |
} | |
set-power-switch-state() { | |
# "on" or "off" | |
local state=$1 | |
curl-with-args \ | |
-d "{\"entity_id\": \"switch.power_switch\"}" \ | |
-X POST \ | |
"$ha/api/services/switch/turn_${state}" > /dev/null | |
} | |
regulate() { | |
local temp | |
while true; do | |
get-actual-coldest-temperature | read temp | |
# TODO: make sure reading isn't out of date | |
echo -n "Coldest fridge temperature is: $temp °C; " | |
if [[ $temp -le $fridge_power_off_temp ]]; then | |
echo "temp <= $fridge_power_off_temp, turning off fridge" | |
set-power-switch-state off | |
elif [[ $temp -ge $fridge_power_on_temp ]]; then | |
echo "temp >= $fridge_power_on_temp, turning on fridge" | |
set-power-switch-state on | |
else | |
echo "temp within range, doing nothing" | |
fi | |
curl -s "$healthchecks_endpoint" > /dev/null | |
sleep 60 | |
done | |
} | |
regulate |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment