How to parse command_line json

This is my sensors.yaml

  - platform: command_line
    name: kocomtemp
    json_attributes:
      - l_cur_temp
      - l_set_temp
    command: "/config/python_script/kocom.py sensor temp"
    value_template: '{{ value_json.kocom_heat}}'

  - platform: template
    sensors:
      kocom_livingroom_current_temp:
        friendly_name: 'Current Temp'
        value_template: "{{ states.sensor.kocomtemp.attributes.l_cur_temp }}"
        unit_of_measurement: "C"

  - platform: template
    sensors:
      kocom_livingroom_set_temp:
        friendly_name: 'Desired Temp'
        value_template: "{{ states.sensor.kocomtemp.attributes.l_set_temp }}"
        unit_of_measurement: "C"

/config/python_script/kocom.py sensor temp returns like this

{ā€œkocom_heatā€: 0, ā€œl_cur_tempā€: 24, ā€œl_set_tempā€: 23}

However, I cannot parse this using written template.

[homeassistant.components.command_line.sensor] Unable to parse output as JSON

Whatā€™s wrong?

Can anybody help?

Copy the format you used in your command for the sensors:

  - platform: template
    sensors:
      kocom_livingroom_current_temp:
        friendly_name: 'Current Temp'
        value_template: "{{ value_json.l_cur_temp }}"
        unit_of_measurement: "C"

  - platform: template
    sensors:
      kocom_livingroom_set_temp:
        friendly_name: 'Desired Temp'
        value_template: "{{ value_json.l_set_temp }}"
        unit_of_measurement: "C"

Thank you tom.
But I got same error message
[homeassistant.components.command_line.sensor] Unable to parse output as JSON

Please ignore my last post. I didnā€™t understand what you were doing.

The problem you have is that once you grab the data in the command line sensor it becomes a string, not json data.

See here for more:

Thaks a lot!

Itā€™s quite simple bug.
some other std out was occured in other parts of script!
Itā€™s working now

Hey, just thought Iā€™d throw in here that Iā€™ve just implemented some template filters to_json and from_json. If you have JSON-formatted string and want to interpret it as JSON, you can use it in a template like this:

{%- set jsonObject = jsonFormattedString | from_json -%}
{{ jsonObject.MyJsonAttribute }}

These filters will be in v0.101, so they arenā€™t in the version you have right now, but they should be available soon.

1 Like