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?
tom_l
October 26, 2019, 7:12am
2
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
tom_l
October 26, 2019, 8:25am
4
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:
I don’t know if you saw this thread Trying to understand JSON parsing . The upshot is that state stores everything as a string so no matter how well-formed the JSON may be it will handled as a string. Perhaps parameters get the same treatment and are passed as strings only.
I found this post by Petro indicating an attribute can be used to store a JSON object. This information was given to an individual who was making a custom component therefore could create attributes. Otherwise, I don’t believ…
Thaks a lot!
It’s quite simple bug.
some other std out was occured in other parts of script!
It’s working now
SteveDinn
(Steve Dinn)
October 26, 2019, 11:54pm
6
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