Using file platform to load json array

I use a Roku Express media player as my internet tv server. I have a simple UI on my dashboard to choose a source (eg BBC iPlayer) and a channel (eg BBC News). I have a json data array that provides the commands to be sent to the Roku Express for each source/channel combination. It all works very well except I have a maintenance issue. I set value_json from a long text string in three separate places in the code.
I would like to use the file platform to load the json array as an attribute of a sensor. The json array is stored in the config folder. As a first step, I have tried:

platform: file
    name: TVjson
    file_path: /config/test.json
    value_template: '{{ value_json.sources[0].name }}

in sensors.yaml but a sensor is not created. Is the file_path correct? Development Tools tells me the value of value_json.sources[0].name is ‘BBC iPlayer’. Any and all help gratefully received.

Well you definitely have an indentation issue because that’s not valid YAML. Your sensor would work if put into your configuration.yaml file like this:

- platform: file
  name: TVjson
  file_path: /config/test.json
  value_template: '{{ value_json.sources[0].name }}

Sounds like you’ve extracted some of the YAML in configuration.yaml file to a separate sensors.yaml file that is !included somehow. Feel free to adjust accordingly based on how you did that.

I’m confused why you have this JSON duplicated all over though. Wouldn’t it be easier to just make a template select? Set the list of options in its config and have it send the correct command in select option based on the selected option. Then just use that select entity everywhere in your automations and UI.

For others seeking a similar solution …

The file platform does not provide a sensor with attributes. You can only use the state of the sensor, limited to 255 characters, to hold your data.


  - sensor:
      - name: "Sensor Name"
        state: "OK"
          array: 'VERY long text string'

allows you to assign your json array to a sensor’s attribute. In my case, I can access data in the array using either of these

{% set value_json = state_attr('sensor.tv_json','array') | from_json %}
{{ value_json.sources[0].name }}

{{ (state_attr('sensor.tv_json','array') | from_json).sources[0].name }}