I am looking for a way to configure a new template-sensor programatically. That is, I neither want to somehow touch configuration.yaml, nor manually click through the GUI-based dialog.
If I have all the options that I would need to configure for a new template via either mechanism, is there a way to do so e.g. through the API from the outside? Maybe I should check how “Repairs” work?
As background-info, I’m kind of developing a setup-assistant.
Thanks in advance for any tips & pointers! The keywords are unfortunately too overloaded so that I did not spot any existing info.
Not possible. You have to touch yaml files. There is no API for HA to create sensors & entities on the fly outside websockets. And websockets don’t handle the config entry generation.
Hi @VolkerStolz , little late, but here is a bash script you can you use to get any config file (e.g Template Sensors), you can then programatically add new sensors, then upload it, and trigger a ‘template sensor reload’ in HA, so your new sensors are available…
This is just a short ‘proof of concept’ in bash w/ ‘websocat’, but you could easily implement in the language of your choice etc.
#!/bin/bash
#Proof of concept example to download/upload any HA config file, (but will work for any config file - Im using it just to make new Template Sensors)
#Example usage case: for building your template sensors 'offline'
#Requirements: just websocat, and the standard HA file editor (https://www.home-assistant.io/common-tasks/os/#installing-and-using-the-file-editor-app)
#Example Usage, creating template sensors 'offline', assuming yours are in 'template_sensors.yaml'
#Step1: ./ha-config-file.sh --download template_sensors.yaml
#Step2: [[then just edit your template sensors file, add new ones etc, as desired]
#Step3: ./ha-config-file.sh --upload template_sensors.yaml
#Step4: ./ha-config-file.sh --reload-templates
TOKEN="MY_HA_TOKEN"
HOST="192.168.9.2:8123"
case "$1" in
--download|--upload)
[ -z "$2" ] && { echo "Usage: $0 $1 <filename>"; exit 1; }
FILE="$2"
# WS auth + get ingress path + get session
WS_OUT=$(printf '%s\n%s\n%s\n' \
"{\"type\":\"auth\",\"access_token\":\"$TOKEN\"}" \
"{\"id\":1,\"type\":\"supervisor/api\",\"endpoint\":\"/addons/core_configurator/info\",\"method\":\"get\"}" \
"{\"id\":2,\"type\":\"supervisor/api\",\"endpoint\":\"/ingress/session\",\"method\":\"post\"}" \
| websocat --max-messages-rev 4 -n "ws://$HOST/api/websocket")
INGRESS=$(echo "$WS_OUT" | jq -r 'select(.id==1) .result.ingress_entry')
SESSION=$(echo "$WS_OUT" | jq -r 'select(.id==2) .result.session')
BASE="http://$HOST${INGRESS}"
if [ "$1" = "--download" ]; then
curl -s -b "ingress_session=$SESSION" \
"$BASE/api/file?filename=/homeassistant/$FILE" -o "$FILE"
echo "Saved $FILE"
else
curl -s -b "ingress_session=$SESSION" \
-X POST "$BASE/api/save" \
--data-urlencode "filename=/homeassistant/$FILE" \
--data-urlencode "text@$FILE"
echo "Uploaded $FILE"
fi
;;
--reload-templates)
curl -s -X POST -H "Authorization: Bearer $TOKEN" "http://$HOST/api/services/template/reload"
echo "Template sensors reloaded"
;;
*)
echo "Usage: $0 --download <file> | --upload <file> | --reload-templates"
;;
esac