Pass value to command line

I’m trying too use a template in a sheel command. I´m using a input slider to pass the value.

Here is my setup:

input_slider:
 tv_volume:
    name: TV Volume
    initial: 8
    min: 1
    max: 20
    step: 1

shell_command:
 lg_volume_9: sudo echo ke 01 9 > /dev/ttyUSB0
 set_volume_tv: 'sudo echo kf 01 {{ states.sensor.input_slider.volume_tv | int }} > /dev/ttyUSB0'

When I run the lg_volume_9 works very well, but when I run set_volume_tv doesn´t work.

I try also transform the input slider as a sensor and doesn´t work.

What is the solution for me?

Thank´s

try:

set_volume_tv: 'sudo echo kf 01 {{ states.input_slider.volume_tv.state | int }} > /dev/ttyUSB0'

Thanks for the help.

But don’t work.

I m trying other away. My Lg tv have netcast, i will try use the input slider to change te volume.

Sorry,
I’ve got mines working without quotes:
set_volume_tv: sudo echo kf 01 {{ states.input_slider.volume_tv.state | int }} > /dev/ttyUSB0

Is it possible to use shell_command with data_template or some way to pass the entity_id as a variable to the shell command?

I’m trying to use one shell command for many things and I want to reference the calling entity name in the shell command.

This currently works but doesn’t allow much flexibility because the light name is specified.

shell_command:
  light_dim: ''/usr/local/bin/light_dim.sh {{ states.input_slider.light_office_ceiling.state | int }}'

Obviously this doesn’t work but something like:

shell_command:
  light_dim: '/usr/local/bin/light_dim.sh {{ states.input_slider.{{ state.entity_id }}.name }}'

That way I don’t have to have a shell command for each light.

In your shell_command, just use:

shell_command:
  light_dim: '/usr/local/bin/light_dim.sh {{ value }}'

Then, in your automation,

  action: 
    - service: shell_command.light_dom
      data_template:
        value: >
          {% template logic to determine value to send %}

“value” is passed as an argument to the shell_command.

5 Likes
  1. How did someone like your post before me??
  2. THANKS! I thought the parameters for data_template had to be available in the component.

Hi,
I am trying to set my AC to any temperature using an input slider and IR commands but unable to do it.
I can turn ON and OFF the AC using scripts but changing the temperature using the input slider is not working for me can you help me in that.

Post your automation/config so that we can help debug. (Use preformatted text, btw).

Confirm that your IR commands to change temp work when set manually (use the services dev tool to test).

Yes IR commands are working manually. The configuration.yaml file is below.

automation:
  - alias: run_set_ac
    trigger:
      platform: state
      entity_id: input_slider.ac_temperature
    action:
      service: script.set_ac_to_slider    
       
input_slider:
  ac_temperature:
    name: A/C Setting
    initial: 24
    min: 20
    max: 24
    step: 2

the script.yaml file

set_ac_to_slider:
  alias: AC ON
  sequence:
    - service: shell_command.set_ac_to_slider
ac_off:
  alias: AC OFF
  sequence:
    - service: shell_command.ac_off

the shell commmand.yaml file

ac_off: irsend SEND_ONCE Orientac OFF
set_ac_to_slider: 'irsend SEND_ONCE Orientac ACon_{{ states.input_slider.ac_temperature.state }}'

You may need {{ states.input_slider.ac_temperature.state | int}} as i think it defaults to float

Try just using a variable in the shell command and pass the input slider value to the shell command from your script instead. I know that way should work, I haven’t tried it the way you are doing it personally. @ih8gates example above should work. Pass value to command line

Thanks for your kind help. But I have done it the other way

You’re not actually doing it like in my example. I’m passing the variable via a data_template. I’m not sure if any template value is valid there.

Try passing the script a value from the automation’s action like in my example.

thanks for your help but i have done it without using automation action

I’m trying to use the shell command with an input select, to change the predefined heat programs of my Buderus central heating unit. The Buders KM271 serial board is configured with a stand alone FHEM instance, where i can send commands via telnet to.

So what I want to do is, to pass the input select value to the shell command and use the value to chance the heat program and use an automation to trigger the service.

  • action:
    • service: shell_command.set_heatprogramm
      alias: run_set_heatprogramm
      condition:
      id: ‘1510046601796’
      trigger:
    • entity_id: input_select.heatprogramm_select
      platform: state

To change the FHEM values via telnet I use netcat (nc) to send the commands to FHEM, but I have no idea how to escape the state of the input select so thats inside the command line.

shell_command:
set_heatprogramm: ‘echo “set Buderus hk1_programm {{ states.input_select.heatprogramm_select.state }}” | nc 192.168.x.x 7072’

Due to the fact that i have to use " to send the whole set … string to fhem with netcat i have to include the input select state in which kind of formatting?

Logfile after selection:

2017-11-07 11:09:53 INFO (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: service_data=option=frueh, entity_id=input_select.heatprogramm_select, service=select_option, service_call_id=1973120752-35, domain=input_select>
2017-11-07 11:09:53 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: old_state=<state input_select.heatprogramm_select=mittag; friendly_name=Heizprogramm, options=[‘eigen’, ‘familie’, ‘frueh’, ‘mittag’, ‘nachmittag’, ‘senior’, ‘single’, ‘spaet’, ‘vormittag’] @ 2017-11-07T10:46:59.973644+01:00>, entity_id=input_select.heatprogramm_select, new_state=<state input_select.heatprogramm_select=frueh; friendly_name=Heizprogramm, options=[‘eigen’, ‘familie’, ‘frueh’, ‘mittag’, ‘nachmittag’, ‘senior’, ‘single’, ‘spaet’, ‘vormittag’] @ 2017-11-07T11:09:53.275219+01:00>>
2017-11-07 11:09:53 INFO (MainThread) [homeassistant.components.automation] Executing run_set_heatprogramm
2017-11-07 11:09:53 INFO (MainThread) [homeassistant.core] Bus:Handling <Event logbook_entry[L]: message=has been triggered, name=run_set_heatprogramm, domain=automation, entity_id=automation.run_set_heatprogramm>
2017-11-07 11:09:53 INFO (MainThread) [homeassistant.helpers.script] Script run_set_heatprogramm: Running script
2017-11-07 11:09:53 INFO (MainThread) [homeassistant.helpers.script] Script run_set_heatprogramm: Executing step call service
2017-11-07 11:09:53 INFO (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: service_data=, service=set_heatprogramm, service_call_id=1973120752-36, domain=shell_command>
2017-11-07 11:09:53 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_executed[L]: service_call_id=1973120752-35>
2017-11-07 11:09:53 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_executed[L]: service_call_id=1973120752-36>
2017-11-07 11:09:53 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: old_state=<state automation.run_set_heatprogramm=on; friendly_name=run_set_heatprogramm, id=1510046601796, last_triggered=2017-11-07T10:47:00.068473+01:00 @ 2017-11-07T10:23:24.406207+01:00>, entity_id=automation.run_set_heatprogramm, new_state=<state automation.run_set_heatprogramm=on; friendly_name=run_set_heatprogramm, id=1510046601796, last_triggered=2017-11-07T11:09:53.357611+01:00 @ 2017-11-07T10:23:24.406207+01:00>>

Here is my code:
action:
- service: shell_command.enter_task
data_template:
value: >
{{states.input_text.daily_schedule_task_input.state}}

shell_command:
enter_task: ‘echo {{ value }}>>/home/homeassistant/.homeassistant/schedule’

input_text:
daily_schedule_task_input:
name: Input task

Basically it is the same as yours. Tried every possible thing I could, but it just doesn’t work. If I remove {{value}} from the shell_command, it works fine, but as soon as I try to input any varibale/parameter in the command line, its not working.

PS. Ignore the indentation, they are correct, checked them several times. I just don’t know how to enter the code in the format as you did in your comment. New here

Do you need a space between the value and the redirection?

shell_command:
    enter_task: ‘echo {{ value }} >> /home/homeassistant/.homeassistant/schedule’

bump

no templating within shell commands command_line is dumb and driving me nuts.

This was fixed 3 years ago - if it’s not working for you, create a new thread with your code for someone to have a look at, or if you’re 100% confident that your code is correct, report it as a bug.