Apologies if this is in the wrong place but I’ve spent the last 24 hours down a templating rabbit hole.
Basically I wanted a file selector dropdown, so I can run a script that will delete a podcast. I found many solutions/problems on the forum, and got it to work a few ways so I thought I would share as I think this is a little unclear.
My solution/components:
- Command line sensor (python script that returns a json with a number of files and a list of files and a list of files like
{"number": 5, "podcasts": ["NOPOD", "How_Do_You_Cope_p0c350qc.mp3", "Doomsday_Watch_with_Arthur_Snell_PMO7960405479.mp3", "Office_Ladies_3448.mp3", "Office_Ladies_8953.mp3"]}
(the NOPOD is always inserted at the front so that when I load the lovelace card clicking the delete button won’t do anything.)
The sensor looks like this
- platform: command_line
name: podcast_json
command: "path/to/script/podcasts_json"
scan_interval: 60
json_attributes:
- number
- podcasts
value_template: "{{ value_json.number }}"
My first attempt just loaded a comma seperated list, but a list of files soon exceeds 255 characters, and required a python script (which I cadged from petro on a post I can’t find) this worked OK, but the final solution is simpler.
As all the filenames are returned in a single key, you can then load that key back into the input select
Automation:
alias: 'JSON Update Podcast List '
description: ''
trigger:
- platform: state
entity_id: sensor.podcast_json
condition: []
action:
- service: input_select.set_options
data_template:
options: |
{{ states.sensor.podcast_json.attributes.podcasts }}
target:
entity_id: input_select.pod1,input_select.pod2
mode: single
(There are 2 input selectors so I can delete 2 at a time, which is a good enough compromise over having a proper file selection dialogue.
(there are lots of examples of tryong to load options with a for loop, this doesn’t work unless you do the loop inside a json Populating Input_Select options from REST - #3 by makani47
[{%- for item in states("sensor.fpp3_playlists")|from_json %} "{{ item }}"{%
if not loop.last %}, {% endif %} {%- endfor %}]
)
So basically this solution I stumbled across seems simple thant than most I found. And solving this was pretty fun. Suggestions, clarifications, and improvements welcome.