Create sensor from json file

I want to create a sensor that reads data from a json file. The file data is shown below. I have tried several different ways of doing it, but the sensor is either not created or shows as unavailable in developer tools>states.

{“storeDevicePower”:{“pvPower”:700.00,“gridPower”:400.00,“inputOutputPower”:0.00,“batteryPower”:0.00,“totalLoadPower”:300.00,“backupLoadPower”:0.00,“solarPower”:4.50,“batCurr”:0.00,“batEnergyPercent”:94.00,“runningState”:1,“isOnline”:1,“isAlarm”:0,“mark”:1,“batCapcity”:300.00,“pvConsumpPower”:300.00,“pvGridPower”:400.00,“pvChargePower”:0.00,“gridConsumpPower”:0.00,“gridChargePower”:0.00,“batConsumpPower”:0.00,“batGridPower”:0.00,“pvDirection”:1,“pvConsumpDirection”:1,“pvGridDirection”:1,“pvChargeDirection”:0,“gridDirection”:1,“gridConsumpDirection”:0,“batteryDirection”:0,“batConsumpDirection”:0,“batGridDirection”:0,“outPutDirection”:1,“dataTime”:1729416480000,“updateDate”:1729405673000},“type”:2,“status”:“success”}

For background, this is what I am trying to do (How to import real time solar battery data without API - #5 by Ibg75). No-one responded to this post, but I have now narrowed it down to the above problem. Thanks!

See File - Home Assistant

Pay attention that:

  • Your JSON must be on a single line
  • Only the last line will be read

Thanks for the response. It is a single line when I open it in Studio Code Editor.

I noticed there was a second line as shown in the screenshot in my previous reply. I have deleted that, but it still doesn’t work.

Use the rest integration with resource pointing to the file.

1 Like

Like in “file:///…”? Cool…

EDIT: No, apparently the “file” protocol is not handled.

Thanks, but i get the error below Rest doesn’t appear to support reading from local files.

Invalid config for ‘sensor’ from integration ‘rest’ at configuration.yaml, line 70: invalid url for dictionary value ‘resource’, got ‘file:///config/eveready_data.json’, please check the docs at RESTful - Home Assistant

Put the file in your www directory and use http://<IP>:8123/local/<path_to_file>

Thanks, I’ll give that a try. Note that the data in the file needs to be updated regularly and I get it using a shell command that uses curl to log in, handle cookies and retrieve the data in the json file. Will I still be able to update the file automatically in the location you suggested?

yes

Note that, if you’re going the curl route anyway, you can nowadays get the output of a shell command in variable, for reuse in a template.

Some thing like

- trigger:
  - platform: time
    at: "06:32:00"  
  - platform: homeassistant
    event: start 
  action:
  - service: shell_command.tibi_get_history
    response_variable: tibi_history
  sensor:
    - name: Tibi organique ytd
      unique_id: tibi_organique_ytd
      state_class: total
      device_class: weight
      unit_of_measurement: kg
      state: >
        {% set value_json = tibi_history['stdout'] | from_json %}
        {% set rest_history = value_json.history[0].card.products | selectattr('status', "eq", 'active') | selectattr('fraction', "eq", 'GFT') | map(attribute='emptyings') | list %}
        {% set ns = namespace(weight=0) -%}

        {% if rest_history[0] | count > 0 %}
          {% for k in rest_history[0] %}
            {% set ns.weight = ns.weight +  rest_history[0][k]["weight"]| float %}
          {% endfor %}
          {{ ns.weight }}
        {% else %}
          unavailable
        {% endif %}
      attributes:
        vidanges: >
          {% set value_json = tibi_history['stdout'] | from_json %}
          {% set rest_history = value_json.history[0].card.products | selectattr('status', "eq", 'active') | selectattr('fraction', "eq", 'GFT') | map(attribute='emptyings') | list %}
          {{ rest_history[0] | count }}

The key is {% set value_json = tibi_history['stdout'] | from_json %}, to get a workable json from the output of the shell command, which is a string as far as HA is concerned.

I got it to work in the end using a combination of curl shell commands and file sensors configured using the File integration (for some reason they wouldn’t work if coded directly in yaml). I’m pretty happy with the dashboard I have been able to build using the data I now have available.

If anyone else has an Eveready Energy Vault battery you should be able to do the same.

The YAML code in configuration.yaml is below. The file sensors can be configured for each data point you want to monitor. They use /config/eveready_data.json as the source and have a template like this {{ value_json.storeDevicePower.batEnergyPercent }} depending on the sensor you want.

homeassistant:
  allowlist_external_dirs:
    - /config
    
shell_command:
  eveready_login_and_retrieve: >-
    curl -v -L -k --header 'Content-Type: application/x-www-form-urlencoded' --header 'User-Agent: PostmanRuntime/7.42.0' --header 'Accept: */*' --header 'Connection: keep-alive' --data 'username=YOUR_USERNAME&password=YOUR_PASSWORD&lang=en' --cookie-jar /config/eveready_cookies.txt 'https://app.evereadysolarstorage.com/cloud/login' &&
    curl -v -L -k -b /config/eveready_cookies.txt --header 'User-Agent: PostmanRuntime/7.42.0' --header 'Accept: */*' --header 'Accept-Encoding: gzip, deflate, br' --header 'Connection: keep-alive' --header 'Referer: https://app.evereadysolarstorage.com/' --header 'Origin: https://app.evereadysolarstorage.com' 'https://app.evereadysolarstorage.com/cloud/monitor/site/getStoreOrAcDevicePowerInfo?plantuid=&devicesn=YOURPLANTID' | tee /config/eveready_debug.log | grep -o '{.*}' > /config/eveready_data.json