I am having trouble using templates within the commands of command line switch. I want to implement the following.
switch:
- platform: command_line
switches:
alert1:
friendly_name: "alerter"
command_on: >-
/usr/bin/curl -X GET http://192.168.1.10:8122/nect?ip={{ state_attr('binary_sensor.alert', 'IPaddress') }}
command_off: >-
/usr/bin/curl -f -X GET http://192.168.1.10:8122/recon?ip={{ state_attr('binary_sensor.alert', 'IPaddress') }}
command_state: >-
/usr/bin/curl -f -X GET http://192.168.1.10:8122/status?ip={{ 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.
tom_l
April 12, 2023, 4:13am
2
None of the commands support templates:
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
switches:
lounge_amp_adaptive_dsp:
friendly_name: Adaptive DSP
value_template: "{{ is_state_attr('sensor.lounge_amp', 'adaptive_dsp_level', true ) }}"
turn_on:
- service: rest_command.lounge_adaptive_dsp_on
- delay:
seconds: 1
- service: homeassistant.update_entity # <---- ### Here ###
entity_id: sensor.lounge_amp
turn_off:
- 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:
# KIMAI API
sensor:
- platform: rest
name: Timesheet ID
resource: http://192.168.100.108:8001/api/timesheets/active
method: GET
headers:
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
:
command_line:
- switch:
name: Zeiterfassung MA 1
command_on: "curl -X 'GET' 'http://192.168.100.108:8001/api/timesheets/1/restart' -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' 'http://192.168.100.108:8001/api/timesheets/{{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 ?
Solution
I have tried that one:
shell_command:
zeit_amon: "curl -X 'GET' 'http://192.168.100.108:8001/api/timesheets/{{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
Thanks for the hint