Json into sensor value + attributes - rest or file

I am trying to get json data into a sensor - state and an attribute .. similar to nordpool but from my own script but I have not been able to get the file or rest sensors to work - the file sensor there is no sensor created and rest sensor no values in the sensor even after updating.

I would appreciate some help to get this working!

json is in www, example of json is below, I checked it with a tool I found in other places and it seems to checkout as well as the path (https://jsonpath.com/)

what I did with file sensor (no sensor ever spawned):

sensor:
  - platform: file
    name: extrema_py
    file_path: /config/www/extrema_py.json
    value_template: "{{ value_json.state }}"
    json_attributes:
      - data

what I did last with rest sensor - sensor spawns but no values are being written (even after updates)

sensor:
  - platform: rest
    name: extrema_py
    resource: http://homeassistant.local:8123/local/extrema_py.json
    unit_of_measurement: kWh
    value_template: "{{ value_json[0].state }}"
    scan_interval: 2592000   # = 30 days x 24hr x 60min x 60s more or less never - will be activated by automation
    json_attributes_path: "$"
    json_attributes:
      - data

json is as follows

{
  "state": 62.1,
  "data": [
    {
      "start": "2025-11-07 00:00:00",
      "value": 5.116666666666667
    },
    {
      "start": "2025-11-08 00:00:00",
      "value": 5.033333333333333
    }
  ]
}

I think File has been completely moved into the UI... the integration docs no longer contain any YAML config examples for File sensors. Based on the docs history and this PR it looks like that has been the case for nearly 2 years... and, from what I can see, there is no evidence that json_attributes was supported as a configuration variable when last they could be configured via YAML.

The following example was tested on my system and produced a sensor containing JSON data from a file.

It's a Trigger-based Template Sensor that employs the file.read_file action to get the contents of /config/www/extrema_py.json. It triggers at 00:00:00 and whenever Template entities are reloaded. It can be changed to trigger whenever you prefer.

template:
  - triggers:
      - trigger: time
        at: "00:00:00"
      - trigger: event
        event_type: event_template_reloaded
    actions:
      - action: file.read_file
        data:
          file_name: /config/www/extrema_py.json
          file_encoding: JSON
        response_variable: ep
    sensor:
      - name: extrema_py
        unique_id: extrema_py_abc_123
        state: "{{ ep.data.state | default('unknown') }}"
        attributes:
          data: "{{ ep.data.data | default([]) }}"

1 Like

that worked great!

note: not sure why, but I had to change triggers to trigger and actions to action in order to get through the yaml check, but after these changes and restarting it worked great!

question regarding the line :

- trigger: event
        event_type: event_template_reloaded

i thought template sensors loaded if one for example through actions did reloaded templates, I suppose you included this as they do not reload, right? or is there some other reason? (doc i found which is a bit unclear to me : Template - Home Assistant )

@ Didgeridrew I understand file sensor method is likely not working, I also noticed the docs were thin.. but found a number of resources explaining what I did should have worked. thanks for confirming!

@all would be nice still find a solution with the rest method as well, as it seems it should be possible as well, but I am happy with the template sensor!!

Glad to hear it worked for you.

Please consider marking my post with the Solution tag. It will help other users find answers to similar questions.

Whatever you are using to check YAML is outdated. The documentation shows the key names are plural and includes examples with plural key names.

That is an Event Trigger for triggering the Trigger-based Template Sensor whenever Template entities are reloaded. It is entirely optional; feel free to remove it and use whatever triggers are needed for your application.

Try the following example. I tested it on my system and it reported the state and the data (as an attribute).

sensor:
  - platform: rest
    name: extrema_py
    resource: http://homeassistant.local:8123/local/extrema_py.json
    unit_of_measurement: kWh
    value_template: "{{ value_json.state }}"
    scan_interval: 2592000
    json_attributes:
      - data

NOTE

The Trigger-based Template Sensor provides more fine-grained control over when the sensor should update its values. The REST Sensor is strictly based on seconds elapsed since it was loaded.

1 Like

ahh. it was in vscode the extension complained.. and i see now it anyway worked with triggers or trigger.

regarding the rest sensor. I have tested it now and am not getting it working actually. You have the file in www right? I triggered it through action rest.reload .. but maybe that is not working as I thought it should?

regarding template sensors - I wonder, if maybe there is a way to trigger an individual template sensor directly through an action? So far I just create a button helper that i reference as a trigger and then "press the button" in automations - i guess there is a better way?

I have read loads of docs and have not seen what I meant - I meant more like in an automation to trigger this template sensor (eg taras's above) like with an action in the automation gui to trigger template sensors with a target where i could set to this template sensor and it would just trigger.

is there such an action (without creating a button that when pressed will trigger this template sensor)?

You can change the state of the entity used in the template of your sensor under the states tab of the dev tools. Keep in mind, when that sensor again updates your value will be overridden.

Yes, by firing a custom event, as I linked previously...

ACTION:

And adding an Event trigger for that custom event to the configuration of the template sensor.

1 Like

ahh.. sorry! I did not understand it and really didn't understand a custom event was a manual event! I am embarrassed to say how many times I have searched and not found any docs and thought I will figure it out later .. today seems to be later. thank you very much!
any idea why the names are not the same or why "fire manual event" is not found in the docs? I would humbly request that it just says in that section "This can be done in the GUI via 'fire manual event'" (if someone should see this that can modify this as it seems I can not suggest it myself via github?)

With this insight I think I am getting it - I guess I would trigger an event with "fire manual event" i think something like this:

event: "trigger_extrema_py"
event_data: { state: "on" }

then in the template sensor I would simply include the even as a trigger:

triggers:
    - trigger: event
      event_type: "trigger_extrema_py"

then it would run 1x, right?

1 Like