Template commands for command line switch

I am having trouble using templates within the commands of command line switch. I want to implement the following.

- platform: command_line
      friendly_name: "alerter"
      command_on: >- 
        /usr/bin/curl -X GET{{ state_attr('binary_sensor.alert', 'IPaddress') }}
      command_off: >-
        /usr/bin/curl -f -X GET{{ state_attr('binary_sensor.alert', 'IPaddress') }}
      command_state: >-
        /usr/bin/curl -f -X GET{{ state_attr('binary_sensor.alert', 'IPaddress') }}
      value_template: >
        {{ value != "1" }}
      unique_id: alert1

when i tested template in debug tab, it was corrct but when i delpoy it, this doesn’t work at all. But if change the template to the IP address, it works.

is there any other way to implement this.

None of the commands support templates:

Screenshot 2023-04-12 at 11-04-20 Command line Switch

Shell commands do support templates.

Use them with a template switch.

You will need an entity for the switch state. So an automation or triggered template sensor that calls the state command regularly. You can also use the homeassistant.update_entity service to update this state after your turn on and turn off commands so that you don’t have to wait for the sensor to be polled (possibly causing bouncing of the switch state in the dashboard). e.g

- platform: template
      friendly_name: Adaptive DSP
      value_template: "{{ is_state_attr('sensor.lounge_amp', 'adaptive_dsp_level', true ) }}"
      - service: rest_command.lounge_adaptive_dsp_on
      - delay:
          seconds: 1
      - service: homeassistant.update_entity # <---- ### Here ###
        entity_id: sensor.lounge_amp
      - service: rest_command.lounge_adaptive_dsp_off
      - delay:
          seconds: 1
      - service: homeassistant.update_entity # <---- ### And here ###
        entity_id: sensor.lounge_amp
      icon_template: "{{ 'mdi:tune-vertical'}}"
1 Like

Hello, I am having also trouble generating a dynamical for a time tracker

I have a sensor that gives me the ID of a running task:

  - platform: rest
    name: Timesheet ID
    method: GET
      accept: "application/json"
      X-AUTH-USER: "superadmin"
      X-AUTH-TOKEN: "supergeheimespassword"
    value_template: "{{ value_json[0].id }}"

That ID is needed in order to stop this specific task. So I want to dynamically insert it into a template

I have a Command Line Switch that is giving an error in the off_command:

  - switch:
      name: Zeiterfassung MA 1
      command_on: "curl -X 'GET' '' -H 'accept: application/json' -H 'X-AUTH-USER: superadmin' -H 'X-AUTH-TOKEN: supergeheimespassword' -H 'Content-Type: application/json' -d '{\"copy\": null, \"begin\": null}'"
      command_off: "curl -X 'GET' '{{states(sensor.timesheet_kima_id_superadmin)}}/stop' -H 'accept: application/json' -H 'X-AUTH-USER: superadmin' -H 'X-AUTH-TOKEN: supergeheimespassword'"

Can you point me into the right direction how I get started with the recommended Shell Command in combination with a Template Switch?


I have tried that one:

  zeit_amon: "curl -X 'GET' '{{states.sensor.timesheet_kima_id_superadmin.state}}/stop' -H 'accept: application/json' -H 'X-AUTH-USER: superadmin' -H 'X-AUTH-TOKEN: supergeheimespassword'"

and it is working as expected :partying_face:

Thanks for the hint