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