Template sesnor, how to parse json string? (need For each loop)

Hi!
i have cmd_line sensor, he provide me this string:
{"86": ["2017-10-19T21:18:24+03:00", "2017-10-19T21:21:02+03:00", "2017-10-19T21:32:35+03:00"], "86к": ["2017-10-19T21:05:56+03:00"], "265": ["2017-10-19T21:14:33+03:00", "2017-10-19T21:34:12+03:00"]}
keys and count of values is various.
how to parse it?

sensor:
  - platform: command_line
    command: python3 /home/homeassistant/.homeassistant/maps/GetInfo.py 123345
    name: bus
    scan_interval: 30
    value_template: "{{ value_json }}"
  - platform: template
    sensors:
      any_template:
        friendly_name: "my bus"
        value_template: "{% for attr in states.sensor.bus.state%}{{ attr }}{% endfor %}"

Can anybody help?

First, your string isn’t valid JSON, but I’ll assume that’s just a paste error.
If your sensor DID return valid JSON, it would look like this:

{"86к": [], "86": ["2017-10-18T23:06:09+03:00", "2017-10-18T23:30:16+03:00"], "265": ["2017-10-18T23:15:46+03:00"]}

Then, to “parse” out the value for “86k” you would do this:
value_template: '{{value_json["86k"]}}'

To get the first value from ‘86’ your template would look like this:
value_template: '{{value_json["86"][0]}}'

See the “processing incoming data” section from this page:

Thank you to reply,
but data is various, and i don’t know keys.
also i don’t know number of values in list,
i want to print all data one time, how can i do it?
need loop list as For Each

Now value template is:

   value_template: "{% set value_json=states.sensor.bus.state %}{ %from b in value_json %}{{ b }}{% endfor %}"

And
When i give him:
{“86”: [“2017-10-19T21:18:24+03:00”, “2017-10-19T21:21:02+03:00”, “2017-10-19T21:32:35+03:00”], “86к”: [“2017-10-19T21:05:56+03:00”], “265”: [“2017-10-19T21:14:33+03:00”, “2017-10-19T21:34:12+03:00”]}
He return me the same.
It mean that HA cant parse utf8…:neutral_face:

1 Like

I’m not pretty sure what you actually want to display.

Maybe this is helpful:

{% for value in  value_json %}  {{value}} contains {{value_json[value]|count}} items - {{value_json[value][0]}}
{% endfor %}

would give you as an output
86к contains 1 items - 2017-10-19T21:05:56+03:00
265 contains 2 items - 2017-10-19T21:14:33+03:00
86 contains 3 items - 2017-10-19T21:18:24+03:00
Is this what you are looking for?

thank you for reply
it is bus number and they arriving time.
i want to see:
86: 21:18, 21:21, 21:32
86к: 21:05
256: 21:14, 21:34
when i’m writing {% for value in value_json %} i see error:
Unknown value_json

Something like:

{% set value_json=states.sensor.bus.state %}{% for value in  value_json %}{{value}}{% for item in value_json[value]%}{% set list1 = value_json[value][loop.index0].split('T') %}{{ list1[1] }}{% endfor %}{{value_json[value][0]}} {% endfor %}

Should give something like:
86к 21:05:56+03:00 2017-10-19T21:05:56+03:00
265 21:14:33+03:00 21:34:12+03:00 2017-10-19T21:14:33+03:00
86 21:18:24+03:00 21:21:02+03:00 21:32:35+03:00 2017-10-19T21:18:24+03:00

return:
2017-10-20 18:04:51 ERROR (MainThread) [homeassistant.components.sensor.template] Could not render template: UndefinedError: 'str object' has no attribute '{'
he can’t parse my str to json… dunno why…

Hi guys.
I’m understand why this don’t wokrking…

Cmd line sensor can’t provide attributes from json

Love to suggest JSON tool to work with JSON data.