Template for setting input_select options from JSON

Hello,

I would like to define a JSON in a local file and then load it and use it to define the state of template sensors and set the option for input_select entities.

The JSON I want to store would have the following format:

{% set my_test_json = {
  "number_cd_loaded": 2,
  "cd": [
    {
      "CD Number": 1,
      "Artist": "Artist CD 1",
      "Album": "Album CD 1 ",
      "NumberTracks": 6,
      "Tracks": ["CD1 Track 1", "CD1 Track 2", "CD1 Track 3", "CD1 Track 4", "CD1 Track 5", "CD1 Track 6"]
    },
    {
      "CD Number": 2,
      "Artist": "Artist CD 2",
      "Album": "Album CD 2 ",
      "Number Tracks": 5,
      "Tracks": ["CD2 Track 1", "CD2 Track 2", "CD2 Track 3", "CD2 Track 4", "CD2 Track 5"]
    } ]
} %}

For the template sensors I would then plan to define them as follows:

template:
    sensor:
      - name: "Album Playing"
        state: "{{ my_test_json.cd[states('sensor.esp8266_test_playing_disk') |int -1].Album }}"

First Question:
Where can i define/save this JSON and how to load it?
I would like to have this saved in a local file to be edited as needed (won’t be very often at all).

Second question
For the input_select instead I would like for example to set the options for input_select.cdp_track_list each time that sensor.esp8266_test_playing_disk changes and the options should be set to {{ my_test_json.cd[x].Tracks}}
How can I use a template to pull the list of option from the JSON file?
I was hoping I could use something like this:

alias: New automation
description: ""
triggers:
  - trigger: state
    entity_id:
      - sensor.esp8266_test_playing_disk
conditions: []
actions:
  - action: input_select.set_options
    metadata: {}
    data:
      options:
      {{ my_test_json.cd[states('sensor.esp8266_test_playing_disk') |int -1].NumberTracks}}
    target:
      entity_id: input_select.cdp_cd_list
mode: single

If the value needs to by dynamic/editable through HA actions then your best bet is probably:


If the json is going to be relatively static, and you don’t mind being limited to manually editing it, you could also store and retrieve it in the same way you do Template macros. This is an undocumented use of the custom templates files, but it does work generally… though, I’ve never tested it for this exact use.

In use you would just copy paste your set statement from your post into a file, for example cd_changer.jinja, then your action would be something along the lines of:

alias: New automation
description: ""
triggers:
  - trigger: state
    entity_id:
      - sensor.esp8266_test_playing_disk
conditions: []
actions:
  - action: input_select.set_options
    metadata: {}
    data:
      options: |
        {% from 'cd_changer.jinja' import my_test_json %}
        {{ my_test_json.cd[states('sensor.esp8266_test_playing_disk') |int -1].Tracks}}
    target:
      entity_id: input_select.cdp_cd_list
mode: single

Note that I have altered the action you had in your original post since it did not make sense to use the value from the NumberTracks attribute there.

FWIW, you would probably be better off using a Template Select instead of an Input Select + multiple automations.