'value_json' is undefined when rendering

Hi AM am getting an error on this command line:

command_line:
  - switch:
      name: EVSE
      command_on: "curl http://192.168.207.98/setStatus?active=true"
      command_off: "curl http://192.168.207.98/setStatus?active=false"
      command_state: "curl -X GET http://192.168.207.98/getParameters"
      value_template: "{{ value_json.list[0].evseState == True }}"

Error:>

  • Template variable error: ‘value_json’ is undefined when rendering ‘{{ value_json.list[0].evseState == True }}’

This is the json:

{
    "type": "parameters",
    "list": [
        {
            "vehicleState": 2,
            "evseState": false,
            "maxCurrent": 20,
            "actualCurrent": 6,
            "actualPower": 0,
            "duration": 320000,
            "alwaysActive": false,
            "lastActionUser": "API",
            "lastActionUID": "API",
            "energy": 0.02,
            "mileage": 0.1,
            "meterReading": 0.03,
            "currentP1": 0,
            "currentP2": 0,
            "currentP3": 0,
            "useMeter": true
        }
    ]
}

I don’t understand why it’s giving me the error.

Any help is greatly appreciated.

Cheers,

Michel

that means your incoming data is not seen as json. Something is invalid in the response at some point. Either your device/endpoint is outputting empty data, or there’s “hidden” characters that make it not json like leading or trailing characters that cannot be visualized as text (So you don’t see them in the response).

You can try and see the full response by just using “{{ value }}” however it will be truncated to 254 characters.

I also believe there are debug logs for rest that allow you to see the full rest response.

Hi I suspect I have to just define a default state as this sensor is not immediately available during startup, I am just not sure how to define the default in this case.

This fixed it:

template:
  - sensor:
      - name: test
        state: "{{ this.attributes.test | default('Value when missing') }}"

now:

command_line:
  - switch:
      name: EVSE
      unique_id: command_line_evse
      icon: mdi:ev-station
      command_on: "curl http://192.168.207.98/setStatus?active=true"
      command_off: "curl http://192.168.207.98/setStatus?active=false"
      command_state: "curl -X GET http://192.168.207.98/getParameters"
      value_template: "{{ value_json.list[0]['evseState'] == true | default('false') }}"

Nope, still the same error in the log.

Anyone?

I already explained the problem. You aren’t getting json back on some calls. The only way to account for that is to not use value_json and use value instead.

Hi Thanks,

I validated the json:

Could you give me the correct syntax for the value_template so can try that out?

Everything I tried failed.

Cheers,

Michel

What I am trying to accomplish is that the command line switch is “on” when json attribute “evseState” = “true”

I seem to be asking something really hard as there seems to be no one having the answer :wink:

Iike this, I do not see the error anymore, but the switch does not work (switches off right after turning it on)

command_line:
  - switch:
      name: EVSE
      unique_id: command_line_evse
      icon: mdi:ev-station
      command_on: "curl http://192.168.207.98/setStatus?active=true"
      command_off: "curl http://192.168.207.98/setStatus?active=false"
      command_state: "curl -X GET http://192.168.207.98/getParameters"
      value_template: "{{ value.list[0]['evseState'] == true }}"

No, you aren’t understanding what I’m telling you. You seem to think your API always sends JSON. It does not. Sometimes it’s json, sometimes it’s not json. So you’re getting errors when it’s not json. You have to account for that by checking value, and if it has information, cast it to json and then use it.

If you’re getting the error every time, it’s 100% not json, regardless what json checker is telling you. You can see the full json being output by plopping the entire returned value in an attribute.

Sure I don’t understand, that is why I’m asking. :slight_smile:

What I do understand is that when I use:

value_template: "{{ value_json.list[0].evseState == True }}"

I see the error once, right after restarting home Assistant, but the switch works correctly.

and when I use"

value_template: "{{ value.list[0]['evseState'] == true }}"

I do NOT see the error, but the switch won’t work correctly.

That’s because value isn’t a JSON object, but a string.

When the API sends valid JSON, value_json will be populated with an object that you can interrogate. value will always be a string. See here:

If we assume the API sends something that doesn’t start with a '{' when it’s not sending JSON, you could do something like:

{% if value[0] == '{' %}
  {{ (value|from_json)['list'][0]['evseState'] }}
{% else %}
  unknown
{% endif %}

I’m not sure that will work, I personally don’t believe his output is json as he’s never gotten a value. But he keeps telling me it is otherwise. There’s most likely an encoding issue with his output that’s placing hidden characters at the start or end of his json.

Right, like a byte order mark or something.

OP: can you run the command in a terminal (if you have hexdump installed) and paste the result?

I am by no means an expert and I am happy with all the help I get.

I have validated the output repeatedly and I always get exactly this format:

{
    "type": "parameters",
    "list": [
        {
            "vehicleState": 1,
            "evseState": true,
            "maxCurrent": 20,
            "actualCurrent": 6,
            "actualPower": 0,
            "duration": 2471000,
            "alwaysActive": false,
            "lastActionUser": "API",
            "lastActionUID": "API",
            "energy": 0,
            "mileage": 0,
            "meterReading": 0,
            "currentP1": 0,
            "currentP2": 0,
            "currentP3": 0,
            "useMeter": true
        }
    ]
}

The json output is generated on a local esp32 and seems to be 100% consistent.

When using:

value_template: "{{ value_json.list[0].evseState == true }}"

I only see the error right after restarting Home Assistant. If I clear the log, the error never repeats itself.

Yes, I know. You’ve posted this multiple times. However I’m telling you: You will not get that error if yoru value is truely JSON. What you’re posting is truely JSON, but you’re getting the error. So there is only 2 possibilities:

  1. The API doesn’t always respond with json.
  2. The API is adding special hidden characters that you aren’t accounting for.

I’ve tried getting this information out of you, but you keep posting the json. So at this point, there’s nothing that can be done because you’re not able to give the requested information. As soon as you can supply myself or troon with the information we need, we can help you. But right now the ball is in your court.

curl -X GET http://192.168.207.98/getParameters | hexdump

% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 300 100 300 0 0 2287 0 --:–:-- --:–:-- --:–:-- 2290
0000000 227b 7974 6570 3a22 7022 7261 6d61 7465
0000010 7265 2273 222c 696c 7473 3a22 7b5b 7622
0000020 6865 6369 656c 7453 7461 2265 313a 222c
0000030 7665 6573 7453 7461 2265 663a 6c61 6573
0000040 222c 616d 4378 7275 6572 746e 3a22 3032
0000050 222c 6361 7574 6c61 7543 7272 6e65 2274
0000060 363a 222c 6361 7574 6c61 6f50 6577 2272
0000070 303a 222c 7564 6172 6974 6e6f 3a22 3832
0000080 3035 3030 2c30 6122 776c 7961 4173 7463
0000090 7669 2265 663a 6c61 6573 222c 616c 7473
00000a0 6341 6974 6e6f 7355 7265 3a22 4122 4950
00000b0 2c22 6c22 7361 4174 7463 6f69 556e 4449
00000c0 3a22 4122 4950 2c22 6522 656e 6772 2279
00000d0 303a 222c 696d 656c 6761 2265 303a 222c
00000e0 656d 6574 5272 6165 6964 676e 3a22 2c30
00000f0 6322 7275 6572 746e 3150 3a22 2c30 6322
0000100 7275 6572 746e 3250 3a22 2c30 6322 7275
0000110 6572 746e 3350 3a22 2c30 7522 6573 654d
0000120 6574 2272 743a 7572 7d65 7d5d
000012c

I just do not understand the error does not repeat itself.

If the output would not be valid json, the error should occasionally repeat itself in the HA logs, or not?

If the error is intermittent, that means your API is not outputting JSON every time.

It’s NOT intermittent, it ONLY occurs on HA start (I said that manny times ;)) and then never repeats!