Template JSON from Command line Sensor

Hi,

I’m trying to get a json value with a template.

{{ states("sensor.ca_balances_json") }}

gives

{"200110276": {"b": "1412.06", "n": "CCHQ"}, "200113369": {"b": "4362.53", "n": "LDD"}, "300145311": {"b": "4103.05", "n": "LIV A"}, "200116245": {"b": "0.0", "n": "LIV A"}, "300145311": {"b": "5742.58", "n": "LIV A"}}

But I’m not able to extract the b value with
{{ states.sensor.ca_balances_json.state["200110276"].b }}

Any idea please ?

That’s because an entity’s state is a string, not a dictionary. How is sensor.ca_balances_json defined? If you can get the JSON into an attribute instead of a state, then you can do what you want.

That makes sense, I use a commande line sensor.

sensor:
  - platform: command_line
    command: '/usr/bin/python3 ~/.homeassistant/scripts/pyCA.py'
    name: 'Balances json'

You might want to try:

sensor:
  - platform: command_line
    command: '/usr/bin/python3 ~/.homeassistant/scripts/pyCA.py'
    name: 'Balances json'
    json_attributes:
      - '200110276'

If that works then you should be able to do:

{{ state_attr('sensor.balances_json', '200110276').b }}
1 Like

It works, thanks a lot.

1 Like