Command line is not working properly

I have

command_line:
  - switch:
      name: Tree effect
      command_on: echo -e '\x47\x54\x04\x00\x{{'%02x' % state_attr('input_select.cicle_effect_tree','options').index(states('input_select.cicle_effect_tree')) |int }}' | nc -w2 -u 192.168.0.118 8888
      command_off: echo -e '\x47\x54\x02\x06' | nc -w2 -u 192.168.0.118 8888

but my devise not work for this command.
I tested static command and it is worked normal

 - switch:
      name: Tree effect
      command_on: echo -e '\x47\x54\x04\x00\x15' | nc -w2 -u 192.168.0.118 8888
      command_off: echo -e '\x47\x54\x02\x06' | nc -w2 -u 192.168.0.118 8888

who can explain why does not work dynamically?

image

command_on and command_off do not support templates, according to the documentation.

Perhaps try using a shell_command instead?

1 Like

I see such an example in the documentation

command_line:
  - sensor:
      name: Wind direction
      command: "sh /home/pi/.homeassistant/scripts/wind_direction.sh {{ states('sensor.wind_direction') }}"
      unit_of_measurement: "Direction"

maby use this solution?
But I dont know how is run “sensor”

A sensor gets a value, and its command does support templates:

image

Hi kdmutriy,

You have the same quote character inner and outer. double the inner set up or use " character there.
It’s getting confused.
So look for that in future templates, but it won’t help you here BC what he said.

2 Likes

of course. you need to use “shell_command” I went to google how to do it in my case

I`m testing code

shell_command:
  set_effect: echo -e '\x47\x54\x04\x00\x04' | nc -w2 -u 192.168.0.118 8888
  set_select_effect: echo -e '\x47\x54\x04\x00\x{{"%02x" % state_attr("input_select.cicle_effect_tree","options").index(states("input_select.cicle_effect_tree")) |int }}' | nc -w2 -u 192.168.0.118 8888

set_effect - normal worked
set_select_effect - not worked
Am I doing something wrong again?