Command Line from Switch

Howdy all- I’m just getting started with Home Assistant and am trying to program a switch to send a TCP message to a group of devices when toggled. I’m able to get the command to work just fine from a terminal, however when I plug it into a switch in the configuration.yaml, it doesn’t seem to send. Is there something I’m missing?

Below is how it is listed in the configuration.yaml file. Any input or suggestions are very much appreciated.

Thanks,
Amina

command_line:
  - switch:
      name: "Red"
      command_on: for i in {200..206}; do echo -e "0" | nc -w 1 192.168.0.$i 8080; done
      command_off: echo "off"

It’s possible that the command line integration does not allow pipes. I know for sure that the shell command integration doesn’t.

Speaking of the shell command integration, that would seem to be a more appropriate than a command line switch for what you’re trying to do here.

The way to get around the limitation of not being able to use pipes is to move the command (pipes and all) into a shell script that you save somewhere in /config or a subdirectory thereof. Then you can just reference that shell script in configuration.yaml

Thanks much for the suggestions- I created a shell script and used the shell command integration to add them into configuration.yaml, and also changed the switch over to an input_select in order to select through different colors. The input selector seems to be working just fine, however I keep getting an error code 2 when the script is executed.

I’ve tried the following to remedy the error code:

  • Using “<<<” instead of pipes in the nc command to rule out whether the issue was related to use of pipes; same error code.
  • Using a python script instead of a shell script to send the TCP commands; same error code.
  • Ensuring the script permissions are executable; same error code.
  • Testing that just ‘echo “test”’ works when everything else is commented out in the shell script; no error code.

It seems like there’s still some permissions issue or command I’m using which isn’t allowed, but I can’t seem to figure out what it is.

Below is my configuration.yaml as well as the shell script. Any additional input is much appreciated!

shell_command:
  led_red: sh /root/homeassistant/shell_scripts/ledCloud_send_tcp_message.sh "0"
  led_orange: sh /root/homeassistant/shell_scripts/ledCloud_send_tcp_message.sh "1"
  led_yellow: sh /root/homeassistant/shell_scripts/ledCloud_send_tcp_message.sh "2"
  led_green: sh /root/homeassistant/shell_scripts/ledCloud_send_tcp_message.sh "3"
  led_blue: sh /root/homeassistant/shell_scripts/ledCloud_send_tcp_message.sh "4"
  led_indigo: sh /root/homeassistant/shell_scripts/ledCloud_send_tcp_message.sh "5"
  led_purple: sh /root/homeassistant/shell_scripts/ledCloud_send_tcp_message.sh "6"
  led_pink: sh /root/homeassistant/shell_scripts/ledCloud_send_tcp_message.sh "7"
  led_rainbow: sh /root/homeassistant/shell_scripts/ledCloud_send_tcp_message.sh "8"
  led_white: sh /root/homeassistant/shell_scripts/ledCloud_send_tcp_message.sh "9"
  led_off: sh /root/homeassistant/shell_scripts/ledCloud_send_tcp_message.sh ":"

input_select:
  multi_state_switch:
    name: LED Clouds
    options:
      - Red
      - Orange
      - Yellow
      - Green
      - Blue
      - Indigo
      - Purple
      - Pink
      - Rainbow
      - White
    initial: Rainbow
    icon: mdi:toggle-switch

automation:
  - alias: Red
    triggers:
      platform: state
      entity_id: input_select.multi_state_switch
      to: 'Red'
    actions:
      - action: shell_command.led_red
  - alias: Orange
    triggers:
      platform: state
      entity_id: input_select.multi_state_switch
      to: 'Orange'
    actions:
      - action: shell_command.led_orange
  - alias: Yellow
    triggers:
      platform: state
      entity_id: input_select.multi_state_switch
      to: 'Yellow'
    actions:
      - action: shell_command.led_yellow
  - alias: Green
    triggers:
      platform: state
      entity_id: input_select.multi_state_switch
      to: 'Green'
    actions:
      - action: shell_command.led_green
  - alias: Blue
    triggers:
      platform: state
      entity_id: input_select.multi_state_switch
      to: 'Blue'
    actions:
      - action: shell_command.led_blue
  - alias: Indigo
    triggers:
      platform: state
      entity_id: input_select.multi_state_switch
      to: 'Indigo'
    actions:
      - action: shell_command.led_indigo
  - alias: Purple
    triggers:
      platform: state
      entity_id: input_select.multi_state_switch
      to: 'Purple'
    actions:
      - action: shell_command.led_purple
  - alias: Pink
    triggers:
      platform: state
      entity_id: input_select.multi_state_switch
      to: 'Pink'
    actions:
      - action: shell_command.led_pink
  - alias: Rainbow
    triggers:
      platform: state
      entity_id: input_select.multi_state_switch
      to: 'Rainbow'
    actions:
      - action: shell_command.led_rainbow
  - alias: White
    triggers:
      platform: state
      entity_id: input_select.multi_state_switch
      to: 'White'
    actions:
      - action: shell_command.led_white
#!/bin/bash

# Assign the first argument to a variable
MESSAGE="$1"

# Loop through the IP addresses
for IP in 200 201 202 203 204 205 206; do
  nc -w 1 192.168.0.$IP 8080 <<< "$MESSAGE"
  #echo "$MESSAGE" | nc 192.168.0.$IP 8080 || echo "fail" &
done