Does template sensor support value_json?

Hi all,

After spending hours trying to get this working, I have caved in and decided to ask the experts! I’ve tried searching, but nothing I have found works, and so I am not sure if it is me, or just not possible.

I have a command line sensor, which runs a Python script, and returns a JSON payload.

I want to then template that JSON payload out into two new sensors.

JSON payload is:
{"A": "Connected", "B": "Not Connected"}

In the template sensor, I use:
value_template: '{{ value_json.A }}'

I’ve tried upper case, lower case, .a.value, .[“A”] etc etc (all from various searches on here).

I always get “unknown” for sensor.box_a_status:

but I see the JSON listed correctly under sensor.device_status, so I know the python is working well.

And in the error log, I get:

Could not render template Box A Status: UndefinedError: 'value_json' is undefined
12:32 PM components/template/sensor.py (ERROR)

Could not render template Box B Status: UndefinedError: 'value_json' is undefined
12:32 PM components/template/sensor.py (ERROR)

I’m starting to think value_json is not supported for the template sensor, but it suggests it is, in the docs (unless I am reading it completely wrong).

Any help would be appreciated.

- platform: command_line
  name: Device Status
  command: 'ssh pi@xxxxxxxxxxxx blah blah"'

- platform: template
  sensors:
    box_a_status:
      friendly_name: Box A Status
      entity_id: sensor.device_status
      value_template: '{{ value_json.A }}'

template sensors do not support value_json. That is only supported in MQTT and some miscellaneous sensors similar to MQTT.

It just so happens that command_line supports it. So if you want to extract both values:

- platform: command_line
  name: Device Status
  command: 'ssh pi@xxxxxxxxxxxx blah blah"'
  json_attributes: "{{ value_json }}"

- platform: template
  sensors:
    box_a_status:
      friendly_name: Box A Status
      entity_id: sensor.device_status
      value_template: "{{ state_attr('sensor.device_status','A') }}"

- platform: template
  sensors:
    box_a_status:
      friendly_name: Box A Status
      entity_id: sensor.device_status
      value_template: "{{ state_attr('sensor.device_status','B') }}"

Or you could just make 2 command_line sensors but then you’re pinging the same command twice. Not really a big deal but you have options.

- platform: command_line
  name: Device Status A
  command: 'ssh pi@xxxxxxxxxxxx blah blah"'
  value_template: "{{ value_json.A }}"

- platform: command_line
  name: Device Status B
  command: 'ssh pi@xxxxxxxxxxxx blah blah"'
  value_template: "{{ value_json.B }}"
2 Likes

Argh! So simple :slight_smile: Thank you so much @petro - I was trying to avoid calling the python script twice, so your first option looks like the winner!

I had to change it slightly to get it to work, otherwise I was always getting “None” as the value and the device_status never had any attributes:

- platform: command_line
  name: Device Status
  command: 'ssh pi@xxxxxxxxxxxxx blah blah"'
  json_attributes:
    - A
    - B
  value_template: "{{ value_json }}"

Thanks for pointing me in the right direction!

1 Like
- platform: template
  sensors:
    box_a_status:
      friendly_name: Box A Status
      entity_id: sensor.device_status
      value_template: "{{ state_attr('sensor.device_status','A') }}"

- platform: template
  sensors:
    box_a_status:
      friendly_name: Box A Status
      entity_id: sensor.device_status
      value_template: "{{ state_attr('sensor.device_status','B') }}"

I presume it should be box_a_status and box_b_status