Python command-line script output as dict

I’m running the following Python command-line script:

sensor:
  - platform: command_line
    name: sma test
    command: "python3 /config/python_scripts/sma.py"
    scan_interval: 5

with following dict string as it’s output:

{ u'timestamp': u'2018-01-17 22:46:57', 
  u'ac_power': 15, 
  u'daily_yield': 277, 
  u'total_yield': 596969 }

Creating sensors with coresponding values:

sensor:
  - platform: template
    sensors:
      sma_power:
        value_template: "{{ states.sensor.sma_test.state.ac_power }}"
      sma_daily:
        value_template: "{{ states.sensor.sma_test.state.daily_yield }}"
      sma_total:
        value_template: "{{ states.sensor.sma_test.state.total_yield }}"

But this is not working and I don’t know why. It’s looks like the output is seen like a string but not parsed as a dict.
What am I doing wrong?

value templates look good to me. It’s gotta be python side. Are you seeing the event in HA? Is sma.py getting called?

I have similar issue with python command in Hass.io. My python scripts works well if I run on terminal screen. But doesnt work with any atomation, sensor or script command in Hass.io.

1 Like

You aren’t. The command_line sensor reads the result as a string so isn’t going to break out attributes like that.

Ok, I have to try a different approch. I would have been nice to use JSON for this…

you could always use appdaemon

Did you ever figure it out? I’m having the same issue Python script works in terminal, but fails in command line sensor