How do I get a local json file to load using Rest or File

I wonder if someone can help me with this problem.
As an update to my aircraft data table I wanted to add in other calculations using Node Red and I had sort of solved the issue in getting the data to Home Assistant as separate entities for each aircraft, but it leaves outdated info in the table.
So I have attempted a different solution and generated a json file with Node Red, which is stored in the /share/ folder of home assistant.

{"airplanes":[{"hex":"40717c","flight":"EXS36PN","height":37000,"speed":"424","dir":187,"lat":"50.66","lon":"-3.35","alt":37000,"km":63,"nm":34,"azimuth":35,"elev_angle":35,"state":"6","ts":0,"last_updated":"2023-11-01T16:43:11.000593+00:00"},{"hex":"40690a","flight":"EZY70FE","height":39025,"speed":"437","dir":185,"lat":"50.18","lon":"-3.34","alt":39025,"km":51,"nm":28,"azimuth":109,"elev_angle":50,"state":"6","ts":0,"last_updated":"2023-11-01T16:43:11.000593+00:00"},{"hex":"4d2155","flight":"VJT993","height":40000,"speed":"482","dir":353,"lat":"51.27","lon":"-6.47","alt":40000,"km":224,"nm":121,"azimuth":301,"elev_angle":9,"state":"6","ts":0,"last_updated":"2023-11-01T16:43:11.000593+00:00"}]}

I now want to use this in the config as a rest entry as follows

  - platform: rest
    name: planes
    resource: http://192.168.1.130/share/airdata.json
    value_template: >
      {{ value_json.airplanes | length }}
    method: GET
    scan_interval: 60
    json_attributes:
      - "airplanes"
      - "flight"
      - "height"
      - "speed"
      - "dir"
      - "lat"
      - "lon"
      - "alt"
      - "km"
      - "nm"
      - "azimuth"
      - "elev_angle"
      - "state"
      - "ts"
      - "last_updated"

To test my json file I have uploaded it to my personal webspace and set the resource to my website URL and that works perfectly, but this does not work with the resource file as referenced above. I assume this is because it is not formed properly as an http file. Using the resource as a file:// doesn’t work as that has different criteria.
Can someone give me some help to get this working, I have spent countless hours trying, but lack of knowledge has beaten me.

You could try adding a JSON content type to see if that helps:

headers:
      Content-Type: application/json

Aside from that, what’s the JSON payload actually look like. Has the “length” command worked on this before? Generally for the value template I address the JSON array I’m digging into.

I have attached the JSON payload in my notes at the top, you can copy/see it there.
As I have said if I load this json file on to my web server and call it from my external webpage as the resource URL, it works. That’s no good because the file changes and is overwritten every 60 secs so I need to grab the file locally

Sorry, I was blind :slight_smile: . Have you tried it with the content type header? Same result?

I don’t know how I can add that to my Node Red raw json datafile, it would have to be an html file, wouldn’t it? As I said it works from my web server, just uploading the basic raw datafile

That is for your REST sensor in HA:

- platform: rest
    name: planes
    resource: http://192.168.1.130/share/airdata.json
    value_template: >
      {{ value_json.airplanes | length }}
    method: GET
    headers:
      Content-Type: application/json
    scan_interval: 60
    json_attributes:
      - "airplanes"
      - "flight"
      - "height"
      - "speed"
      - "dir"
      - "lat"
      - "lon"
      - "alt"
      - "km"
      - "nm"
      - "azimuth"
      - "elev_angle"
      - "state"
      - "ts"
      - "last_updated"

No that doesn’t make any difference, there is no sensor loaded

I have also tried this, but doesn’t work.
It just has to be possible to get HA to use a local json file, but I cannot find any proper info to help

  - platform: file
    name: planes
    file_path: /share/airdata.json
    value_template: >
      {{ value_json.airplanes | length }}
      method: GET
      headers:
        Content-Type: application/json
      scan_interval: 60
      json_attributes:
        - "airplanes"
        - "flight"
        - "height"
        - "speed"
        - "dir"
        - "lat"
        - "lon"
        - "alt"
        - "km"
        - "nm"
        - "azimuth"
        - "elev_angle"
        - "state"
        - "ts"
        - "last_updated"

You changed from REST to file and your parameters don’t look correct. Take a look at this thread, it might help you get to the result you are after:

After much reading I now understand that you cannot load a local json file as an html webpage which, fails due to Access-Control-Allow-Origin viotaion.

You can if you put it your home assistant folder set and use ip:8123/path_to.json

Thank you for that information, I have been tearing my hair out for days.
I have the file in the /share/ folder, do you mean the /config/ folder and if so, which subfolder should use?

You can choose whatever you like within the www folder.
I have many such files.
For example, physically located in “www” I create a folder for the subject, for example “Roborock”.

.homeassistant/www/Roborock

Inside that folder I place a file say “roborock.json”
Then I use (inside an included sensor.yaml) using “local” to refer to the “www” path:

##
## Roborock Rooms 
##
- platform: rest
  name: roborock_rooms
  resource: http://192.168.1.245:8123/local/Roborock/roborock.json
  value_template: "{{ now() }}"
  json_attributes:
    - rooms
    - points

Note that 192.168.1.245:8123 is my homeassistant instance. Be sure to set appropriate access rights for homeassistant user to access the file.

image

Now, I would think that in the first instance you show above, you control the web site. So you can set it up to add Access-Control-Allow-Origin for the homeassistant instance or even “*” for anything. But I prefer to have all mine in the instance themselves for backup and portability.

In another instance I have I actually create a main folder called “custom” and then place everything in folders under that one. It is a bit cleaner that way as then it exists at the same level as “community”, “icons”, “images” …

Thank you so much, I am going to try this later on as I have to go out
I will post the results.

Cheers

You genius,
You might just have saved the last hair on my balding head.
I knew it must be possible, thank you so much for your help, you have no idea how many hours I have spent on this trying all sorts of ingenious web server ideas to get this done. I can’t give up when I get this near to solving a puzzle, and having just turned 72 years old, this keeps my brain churning around.
I am off to have a beer to celebrate

You are welcome … I am 62 so its us oldies sticking together!

Ha ha oldies but goldies, I have lots of other ways I can use this, I am on a roll now and there is no stopping me. Have a nice evening

Excellent. A few of mine are:

  1. I use the Roborock one to maintain the list of zones to nice names
  2. I have 5 Vizio TVs with DirecTV and have large JSON file for each TV+DIrecTV pair to create super remotes that add things like “favorite channels”
  3. I have a CSV with the top 1200 music artists that populates a dropdown to select an artist and play on Pandora or Spotify
  4. My IBBQ integration where I maintain a map of temperatures, doneness and meat type for easily selecting the set temperatures for the probes

Hi kbrown01,

Everything has been working just great until about 4pm this afternoon when suddenly I have an error message as follows

“failed to write to file: Error: ENOENT: no such file /config/www/community/skyware/airdata.json”

I know that that the SAMBA has been updated but I can’t see if I should change the path and it didn’t coincide with the update.
Any ideas?

Is that file there? And it seems confusing … it says failed to write to file … are you editing the file with some code as creating a sensor is not writing anything.