Value template in Command line sensor

I’m having a problem with a command line sensor. I want a sensor that lists the current version of the supervisor:

- platform: command_line
  name: ha_supervisor_version_raw
  command: 'curl http://supervisor/supervisor/info -H "Authorization: Bearer $(printenv SUPERVISOR_TOKEN)" | jq ''{"current_version":.data.version}'''
  scan_interval: 3600

This entity has the following state:

{ “current_version”: “220”}

I’d like for the status of the entity to be only the version number, so have tried a value_template:

- platform: command_line
  name: ha_supervisor_version_raw
  command: 'curl http://supervisor/supervisor/info -H "Authorization: Bearer $(printenv SUPERVISOR_TOKEN)" | jq ''{"current_version":.data.version}'''
  value_template: "{{ value | regex_findall_index('(\d+)') }}"
  scan_interval: 3600

But this results in a validation error:

Error loading /config/configuration.yaml: while scanning a double-quoted scalar
in “/config/sensors.yaml”, line 19, column 19
found unknown escape character ‘d’
in “/config/sensors.yaml”, line 19, column 54

What am I doing wrong?

I can get the result I want by using a second template sensor, but I would like to understand why the value_template doesn’t work in the command line sensor?

- platform: template
  sensors:
    ha_supervisor_version:
      value_template: >-
        {{ states('sensor.ha_supervisor_version_raw') | regex_findall_index('(\d+)') }}

How about using the value_template in the command_line sensor to extract the desired value from the received data?

    value_template: '{{ value_json.current_version }}'
2 Likes

Thank you, that works :+1:

1 Like