My netcat shell_command does not work with dynamic values

Hi All.

I am struggling with sending my slider value to my dimmer. The dimmer takes commands as a byte array so use netcat to send the commands. I have the following in my configuration.yaml:

light:
  - platform: template
    lights:
      drop_virtual:
          friendly_name: "Dråber"
          turn_on:
            service: shell_command.led_on
          turn_off:
            service: shell_command.led_off
          set_level:
            service: shell_command.led_brightness
            data_template:
              bri: '{{ (brightness /255*1024) | int }}'


shell_command:
  led_on: echo Fadetimer2=2000,LED2_Target=100 | nc -w 2 10.100.1.50 43333
  led_off: echo Fadetimer2=2000,LED2_Target=-1 | nc -w 2 10.100.1.50 43333
  led_brightness: echo -e "Fadetimer2=2000,LED2_Target={{bri}}" | nc -w 2 10.100.1.50 43333

My On and Off commands work :+1:
But the led_brightness command does not. In the log I get the following:

2020-10-04 20:47:08 DEBUG (MainThread) [homeassistant.components.shell_command] Stdout of command: `echo -n "Fadetimer2=2000,LED2_Target={{bri}}" | nc -w 2 10.100.1.50 43333`, return code: 0:
b'Fadetimer2=2000,LED2_Target=771 | nc -w 2 10.100.1.50 43333'

If I run the command in my terminal it works .
Any ideas why the heck it does not work when triggered from HA?
Any suggestions are welcome

So: The problem was that I used pipe in my script. Cant do that. So I create a script with everything and passed the parmeter in

@Kristian_Schneider, could you show what you did? I have a shell command very similar to yours:

led_on: echo Fadetimer2=2000,LED2_Target=100 | nc -w 2 10.100.1.50 43333

How would you do that without the pipe?

Cheers, Richard

I ended up doing it up like this:

light:
  - platform: template
    lights:
      drop_virtual:
          friendly_name: "Dråber"
          turn_on:
            service: shell_command.led_on
          turn_off:
            service: shell_command.led_off
          set_level:
            service: shell_command.led_brightness
            data_template:
              bri: '{{ (brightness /255*1024) | int }}'

shell_command:
  led_on: echo Fadetimer2=2000,LED2_Target=100 | nc -w 2 10.100.1.52 43333
  led_off: echo Fadetimer2=2000,LED2_Target=-1 | nc -w 2 10.100.1.52 43333
  led_brightness: ./script/drop.sh {{bri}}

on and off could have been done with the drop.sh aswell

And then I have a script /config/script/drop.sh with


#!/bin/sh
echo "Fadetimer2=2000,LED2_Target=$1" | nc -w 2 10.100.1.52 43333
2 Likes

Thanks for the trick but did you find out how to put multiple different commands in the script ? I want to do multiple different commands in the script as all commands to control my device needs to be putted in script (command string with some characters not usable in the shell_command section or in the template :frowning: