Skip to content

Instantly share code, notes, and snippets.

@llugo
Last active September 2, 2023 18:55
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 llugo/562e080c8ff9e051e67f4fc510f64f56 to your computer and use it in GitHub Desktop.
Save llugo/562e080c8ff9e051e67f4fc510f64f56 to your computer and use it in GitHub Desktop.
#!/bin/bash
############################################################
# Help #
############################################################
Help()
{
# Display Help
echo "TRV Controller Script"
echo
echo "Syntax: [-d|h|l|n|s|t]"
echo "options:"
echo "d Device id/key to set [Keys are displayed when using -l]"
echo "h Print this Help."
echo "l List TRV devices and additional information"
echo "n Change devices name [No white spaces supported]"
echo "s Change desired temp [°C value * 100, i.e. 25°C -> 2500]; This also changes devices preset to 'manual']"
echo "t Time to pass before setting desired temp back to minimum temp. [i.e. 180s, 10m, 2h; default minimum temp. is 15°C]"
echo
}
############################################################
############################################################
# Set variables #
############################################################
############################################################
####API
api_key="APIKEYGOESHERE"
uri="http://localhost:8080/api/"
endpoint="/sensors/"
api_request=$uri$api_key$endpoint
####DEFAULT TEMP AND TIMEOUT SETTINGS
#Default minimum temperature, can be understood as 'off'.
temp_off=1500
#Default desired temperature if nothing else is specified using -s
temp_dsrd=2200
############################################################
# List TRVs #
############################################################
ListTRVs()
{
echo
curl -s $api_request | jq -r '(["Key", "TRV Name", "Measured Temperature", "Temperature Setting", "Preset"] | (., map(length*"-"))),(to_entries[] | select(.value.type=="ZHAThermostat") | [.key, .value.name, .value.state.temperature, .value.config.heatsetpoint, .value.config.preset]) | @tsv' | column -t -s $'\t'
echo
}
############################################################
# Set Preset / Temp #
############################################################
# Set TRV Preset to 'manual'.
SetPreset()
{
echo "Setting TRVs preset to 'manual'"
curl -X PUT -H "Content-Type: application/json" -d '{"preset":"manual"}' $api_request$device_id/config
sleep 3s
echo
echo "Preset was set to 'manual'."
}
# Set device heat set point to desired temp.
SetTemp()
{
# Only set desired temperature if it is above measured temperature
temp_msrd=`curl -s $api_request$device_id | jq .state.temperature`
if [[ $temp_msrd -lt $temp_dsrd ]]
then
echo "Setting desired TRV temperature"
curl -X PUT -H "Content-Type: application/json" -d '{"heatsetpoint":'$temp_dsrd'}' $api_request$device_id/config
echo
echo "Desired temperature was set to $temp_dsrd"
else
echo "Desired temperature is below measured temperature."
fi
}
############################################################
# Set back TRV to minimum Temperate after timeout #
############################################################
# Set device temp back to temp_off if a timeout was set (using -t)
SetTempOff()
{
echo "Waiting $timeout"
sleep $timeout
curl -X PUT -H "Content-Type: application/json" -d '{"heatsetpoint":'$temp_off'}' $api_request$device_id/config
echo
echo "Desired temperature was set back to $temp_off"
}
############################################################
# Set Name #
############################################################
SetName()
{
echo "Setting name of device $device_id to $device_name"
echo
curl -X PUT -H "Content-Type: application/json" -d '{"name":'\"$device_name\"'}' $api_request$device_id
echo
echo "Name was set to $device_name"
}
############################################################
# Process the input options. #
############################################################
while getopts "d:hln:s:t:" option; do
case $option in
d) # Key of Device to set [name/temp] as assigned by deCONZ (use -l to see assigned keys)
device_id=$OPTARG
;;
h) # Display Help
Help
exit;;
l) # List of devices + additional information
ListTRVs
exit;;
n) # Set name of Device
device_name=$OPTARG
SetName
;;
s) # Set device temperature
temp_dsrd=$OPTARG
SetPreset
SetTemp
;;
t) # desired timeout before falling back to temp_off
timeout=$OPTARG
SetTempOff
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit;;
esac
done
echo "All done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment