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