Sensor - Not picking up JSON value

Here is my JSON - Nice and simple - log.json

[
  {
    "watts": 123
  }
]

sensor:

  • platform: file
    name: getwatts
    file_path: /config/python_scripts/log.json
    value_template: ‘{{value_json[“watts”]}}’

the sensor returns unknown.

I can’t for the life of me work out whats wrong

when i change that line to the example I get an error in the log file:
Template variable error: ‘value_json’ is undefined when rendering ‘{{ value_json.watts }}’

The root of the json is an array, so should be

{{value_json[0]["watts"]}}
1 Like

Thanks for looking at this. my config now looks like this:
sensor:

  • platform: file
    name: getwatts
    file_path: /config/python_scripts/log.json
    value_template: ‘{{value_json[0][“watts”]}}’

But I get ‘unknown’ on the data value in the sensor entity. Log file error has gone.

Does the json file need to be laid out in a specific way? It is currently as provided i.e. not one long string.

Ah, file
Only the last line is read, so your json must be on a single line

I manually edited the json to:

[{"watts": 123}]

I still get unknown as the value

Just did a reboot and it works!!!

if my full JSON is:

{ "latestUtc ":1628591223, "id ": "17e44c71-d622-4a08-9b6e-478318296eda ", "power ":[{ "type ": "ELECTRICITY ", "watts ":498, "valueAvailable ":true}], "powerTimestamp ":1628591223, "localTime ":1628594823, "localTimeTimestamp ":1628591223, "creditStatus ":null, "creditStatusTimestamp ":0, "remainingCredit ":null, "remainingCreditTimestamp ":0, "zigbeeStatus ":{ "electricityClusterStatus ": "CONNECTED ", "gasClusterStatus ": "NOT_FOUND ", "hanStatus ": "CONNECTED ", "networkRssi ":-65}, "zigbeeStatusTimestamp ":1628591223, "emergencyCredit ":null, "emergencyCreditTimestamp ":0, "systemStatus ":[{ "component ": "DISPLAY ", "statusType ": "STATUS_OK ", "systemErrorCode ": "ERROR_CODE_NONE ", "systemErrorNumber ":0},{ "component ": "ZIGBEE ", "statusType ": "STATUS_OK ", "systemErrorCode ": "ERROR_CODE_NONE ", "systemErrorNumber ":0},{ "component ": "ELECTRICITY ", "statusType ": "STATUS_OK ", "systemErrorCode ": "ERROR_CODE_NONE ", "systemErrorNumber ":0},{ "component ": "GAS ", "statusType ": "STATUS_NOT_PRESENT ", "systemErrorCode ": "ERROR_CODE_NONE ", "systemErrorNumber ":0}], "systemStatusTimestamp ":1628591223, "temperature ":0.0, "temperatureTimestamp ":0, "ttl ":120}

would the line be something like

value_template: ‘{{value_json[0][“power.watts”]}}’

Not sure why all your keys have an additional blank…

If it’s meant to be so, it is

{{value_json["power "][0]["watts "]}}

Feels so close now.

I get an error when I check the config
expected ‘,’ or ‘}’, but got ‘[’

I’m a fool didn’t put the ’ at the beginning and end’. rebooting

Working !!!

I’ve learnt a lot. Many thanks. I owe you a coffee or three.