Create template-sensor through API?

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.

-Volker

1 Like

I don’t know if there is such API but there is a way to separate your configuration from other configurations: Packages - Home Assistant

1 Like

Thanks, that’d be a partial solution but I’m looking for one that requires no user-intervention.

See below link for some pointers. I wrote this as a custom template helper via UI just before it was included in the core.

Basically create a SensorTemplate entity in your code.

1 Like

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.

1 Like

<LayoutModificationTemplate xmlns="http://
schemas.microsoft.com/Start/2014/
LayoutModification"
xmlns:defaultlayout="http://
schemas.microsoft.com/Start/2014/
FullDefaultLayout" xmlns:start="http://
schemas.microsoft.com/Start/2014/
StartLayout" Version="1">
<LayoutOptions
StartTileGroupCellWidth="6"/>
<RequiredStartGroupsCollection>
<RequiredStartGroups>
<AppendGroup>
<start:Tile
AppUserModelID="AD2F1837.myHP_
v10z8vja
g6ke6!App" Size="2x2" Row="0" Column="0"/>
<start:Tile
AppUserModelID="AD2F1837.OMENComman
dCenter
_
v10z8vjag6ke6!
AD2F1837.OMENCommandCenter" Size="2x2"
Row="0" Column="2"/>
<start:Tile
AppUserModelID="4DF9E0F8.Netflix
_
qhnhss8!Netflix.App" Size="2x2" Row="0"
Column="4"/>
mcm4nj
<start:Tile
AppUserModelID="5A894077.McAfeeSecurity
_
wafk5atnkzcwy!App" Size="2x2" Row="2"
Column="0"/>
<start:Tile
AppUserModelID="PricelinePartnerNetwork.
Booking.comUSABigsavingson_mgae2k3ys4
ra0!App" Size="2x2" Row="2" Column="2"/>
<start:Tile
AppUserModelID="AD2F1837.HPQuickDrop_
v
10z8vjag6ke6!App" Size="1x1" Row="4"
Column="2"/>
<start:DesktopApplicationTile
DesktopApplicationLinkPath="C:
\ProgramData\Microsoft\Windows\Start
Menu\Programs\Walmart.lnk" Size="2x2"
Row="2" Column="4"/>
<start:DesktopApplicationTile
DesktopApplicationLinkPath="C:
\ProgramData\Microsoft\Windows\Start
Menu\Programs\LastPass.lnk" Size="2x2"
Row="4" Column="0"/>
<start:DesktopApplicationTile
DesktopApplicationLinkPath="C:
\ProgramData\Microsoft\Windows\Start
Menu\Programs\ExpressVPN.lnk" Size="1x1"
Row="4" Column="3"/>
<start:DesktopApplicationTile
DesktopApplicationLinkPath="C:
\ProgramData\Microsoft\Windows\Start
Menu\Programs\WildTangent Games.lnk"
Size="1x1" Row="5" Column="2"/>
<start:Tile
AppUserModelID="26720RandomSaladGame
sLLC.SimpleSolitaire_
kx24dqmazqk8j!App"
Size="1x1" Row="5" Column="3"/>
</AppendGroup>
</RequiredStartGroups>
</RequiredStartGroupsCollection>
</LayoutModificationTemplate>

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

1 Like