Value_template User error? Please help

I need some help in getting a rest sensor setup.

I have this setup in the config file.

sensor:
  - platform: rest
    resource: "http://192.168.1.41/vip.php?apiKey=KEY&cmd=req_extended_status"
    name: Ram
    value_template: "'{{ value_json.results.device.deviceStatus }}'"

The curl of the URL returns the below json data. However the the sensor entity says “unknown” and the attributes just say “Friendly Name: Ram”. The logs say that the value template I have set isn’t valid json data. I’m stuck at this point. Any help would be greatly appreciated.

{"errors":[],"results":{"commandResponseValue":{"valueName":"SET_EXTENDED_STATUS","valueType":"FIXME: SET_EXTENDED_STATUS","valueUnit":"changed","numericalValue":null,"textValue":"SupportChanged"},"device":{"action":"req_extended_status","latitude":"0" ,"longitude":"0", "address":null,"speed":"0.00 mph","fastestSpeed":null,"heading":0,"commandSequenceNumber":"223","odometerEstimate":null,"totalDistanceDriven":null,"rssi":"-67","satellites":"9","timeOfFix":"2022-07-18T04:51:00.000Z","updateTime":null,"deviceStatus":{"doorsLocked":true,"doorsOpen":false,"remoteStarterActive":false,"ignitionOn":false,"trunkOpen":false,"hoodOpen":false,"panicOn":false,"securitySystemTriggered":false,"securitySystemArmed":true,"valetOn":null,"dtcCodePresent":false,"engineStateChanged":false,"appMessage":"020000DF001F7500D003010090","appMessageD2D":"xxxx","deviceInterface":{"interfaceType":"02","interfaceTypeDesc":"D2D","partnerId":null,"customerDeviceCapabilities":null}},"displayMessage":null,"batteryVoltage":12.705}}}

Devicestatus is not a value (well … )
Are you trying to load the whole array into the state? This is maximized by 255c

Remove one of those sets of quotes (double, or single) around the template. What you have there is not a template. It is a string.

Also when pasting json data into the forum please put it in code blocks (like your sensor config). That way the quotes aren’t changed to the wrong “curly” type and we can paste the data into tools to find paths to the required value.

Also, your json is not valid (outlined in pink):

The zeros should have quotes either side of them, or none at all.

The quotes around the zeros are an editing error because i removed my lat/long from the data. Please see my edited post for the corrected data.

I’m not trying to load the value into a state. I want the values from “deviceStatus” in an attribute.

I’m mostly interested in doorsLocked, doorsOpen, remoteStarterActive, ignitionOn, securitySystemArmed and securitySystemTriggered. I would also like the lat/long value from status.device as well but wanted to get the important ones into attributes first.

Also I’ve tried the value_template with and without the quotes to no avail. The way you see it here was just my last attempt at it before coming here to ask for help lol…

Your example is loading the value into state, not into an attribute.

If you want the value to be stored in one or more attributes, refer to the examples posted in the documentation. You will need to employ the json_attributes option.

My log receives the below two errors. I attempted to copy the docs example and carry overy my information as below. The sensors are created but only with the Friendly name in the attributes.

REST result could not be parsed as JSON

Template variable error: ‘value_json’ is undefined when rendering ‘{{ value_json.results.device.deviceStatus }}’

sensor:
  - platform: rest
    name: Ram Truck
    json_attributes:
      - doorsOpen
      - doorsLocked
    resource: "http://localhost/vip.php?apiKey=ahhhhhh&cmd=req_extended_status"
    value_template: "{{ value_json.results.device.deviceStatus }}"
  - platform: template
    sensors:
      doorsopen:
        friendly_name: "Ram_Locks"
        value_template: "{{ state_attr('sensor.Ram_Truck', 'doorsLocked') }}"
      doorslocked:
        friendly_name: "Ram_Doors"
        value_template: "{{ state_attr('sensor.Ram_Truck', 'doorsOpen') }}"

On another note I hope my edit history doesn’t show as I just posted a private api key… :confused:

The advice provided by vingerha still applies so change value_template to point to a key whose value is less than 255 characters in length (perhaps value_json.results.device.timeOfFix). In addition, you must use the json_attributes_path option (set it to the value you have currently assigned to value_template).

I thought you could have more than 255c in attributes?

Yes, attributes can store more than 255 characters but that’s not what your example is doing. value_template is used to assign the value of the sensor’s state.

you shouldn’t be using a rest sensor anyways. Use the rest integration and you won’t have this problem. And you wont have the template senosrs.

rest:
  - authentication: basic
    scan_interval: 60
    resource: "http://localhost/vip.php?apiKey=ahhhhhh&cmd=req_extended_status"
    sensor:
    - name: Doors Open
      value_template: "{{ value_json.results.device.deviceStatus.doorsOpen }}"
    - name: Doors Locked
      value_template: "{{ value_json.results.device.deviceStatus.doorsLocked }}"

Ok that makes sense. Ultimately I want the json_attributes_path. However, I did the below, making it as simple as possible and still giving the same errors.

I’m only getting the one error now. “Template variable error: ‘value_json’ is undefined when rendering ‘{{ value_json.results.device.timeofFix }}’”

sensor:
  - platform: rest
    name: Ram Truck
    resource: "http://localhost/vip.php?apiKey=ahhhhhh&cmd=req_extended_status"
    value_template: "{{ value_json.results.device.timeofFix }}"

Change timeofFix to timeOfFix (key names are case-sensitive).

This…

Returns this error…“Template variable error: ‘value_json’ is undefined when rendering”

rest:
  - authentication: basic
    scan_interval: 60
    resource: "http://localhost/vip.php?apiKey=ahhhhh&cmd=req_extended_status"
    sensor:
    - name: Doors Open
      value_template: "{{ value_json.results.device.deviceStatus.doorsOpen }}"
    - name: Doors Locked
      value_template: "{{ value_json.results.device.deviceStatus.doorsLocked }}"

if value_json is undefined that means your result is not json.

curl -X POST http://localhost/vip.php?apiKey=ahhhh&cmd=req_extended_status returns the following.

{"errors":[],"results":{"commandResponseValue":{"valueName":"SET_EXTENDED_STATUS","valueType":"FIXME: SET_EXTENDED_STATUS","valueUnit":"changed","numericalValue":null,"textValue":"SupportChanged"},"device":{"action":"req_extended_status","latitude":"42.9125817","longitude":"-83.645574","address":null,"speed":"0.00 mph","fastestSpeed":null,"heading":0,"commandSequenceNumber":"95","odometerEstimate":null,"totalDistanceDriven":null,"rssi":"-67","satellites":"9","timeOfFix":"2022-07-18T12:51:33.000Z","updateTime":null,"deviceStatus":{"doorsLocked":true,"doorsOpen":false,"remoteStarterActive":false,"ignitionOn":false,"trunkOpen":false,"hoodOpen":false,"panicOn":false,"securitySystemTriggered":false,"securitySystemArmed":true,"valetOn":null,"dtcCodePresent":false,"engineStateChanged":false,"appMessage":"0200005F001F7500D003010090","appMessageD2D":"015F03560521097591435557D56242007503010090000000000000000000005557D562B9F09319C4B224CE000000000D01096F3103BDB693","deviceInterface":{"interfaceType":"02","interfaceTypeDesc":"D2D","partnerId":null,"customerDeviceCapabilities":null}},"displayMessage":null,"batteryVoltage":12.655}}}

well it’s somehow failing. value_json is only not defined when the endpoint fails to return valid json.

Just to make sure, you’re putting

rest:
  - authentication: basic
    scan_interval: 60
    resource: "http://localhost/vip.php?apiKey=ahhhhh&cmd=req_extended_status"
    sensor:
    - name: Doors Open
      value_template: "{{ value_json.results.device.deviceStatus.doorsOpen }}"
    - name: Doors Locked
      value_template: "{{ value_json.results.device.deviceStatus.doorsLocked }}"

into configuration.yaml?

I am…could it be that the response begins with < pre >? I missed that. So this is the complete response from a curl -X POST

<pre>{"errors":[],"results":{"commandResponseValue":{"valueName":"SET_EXTENDED_STATUS","valueType":"FIXME: SET_EXTENDED_STATUS","valueUnit":"changed","numericalValue":null,"textValue":"SupportChanged"},"device":{"action":"req_extended_status","latitude":"42.9125522","longitude":"-83.6455354","address":null,"speed":"0.00 mph","fastestSpeed":null,"heading":0,"commandSequenceNumber":"105","odometerEstimate":null,"totalDistanceDriven":null,"rssi":"-69","satellites":"9","timeOfFix":"2022-07-18T14:11:33.000Z","updateTime":null,"deviceStatus":{"doorsLocked":true,"doorsOpen":false,"remoteStarterActive":false,"ignitionOn":false,"trunkOpen":false,"hoodOpen":false,"panicOn":false,"securitySystemTriggered":false,"securitySystemArmed":true,"valetOn":null,"dtcCodePresent":false,"engineStateChanged":false,"appMessage":"02000069001F7500D003010090","appMessageD2D":"01690356052109759143DD6AD5624200750301009000000000000000000000156AD56292EF931946B424CE000000000D0109883104BBE94A","deviceInterface":{"interfaceType":"02","interfaceTypeDesc":"D2D","partnerId":null,"customerDeviceCapabilities":null}},"displayMessage":null,"batteryVoltage":12.68}}}

well that’s not valid json, you have to remove that from the value before using json.

rest:
  - authentication: basic
    scan_interval: 60
    resource: "http://localhost/vip.php?apiKey=ahhhhh&cmd=req_extended_status"
    sensor:
    - name: Doors Open
      value_template: "{{ (value[5:] | from_json).results.device.deviceStatus.doorsOpen }}"
    - name: Doors Locked
      value_template: "{{ (value[5:] | from_json).results.device.deviceStatus.doorsLocked }}"

This was the trick lol. Thank you!!
.
Now for future use where do you come up with “value[5:]?..”

Sorry for the newb question. Long time home automation guru but new Home Assistant guru.

That’s python string splicing syntax. It’s removing the first 5 characters.

specifically this method