Command works in terminal but not as shell command

Sorry if this is dumb question but I am very new to Home Assistant. I am trying to send RS-232 commands from Home Assistant to an HDMI Matrix using a USB to serial adapter to change inputs and outputs on the Matrix. I have found a few topics on something like this or at least similar enough and most of them say to use something like this:

echo -e -n “data to send here” > /dev/ttyUSB0

In my case I have added this code to the configuration.yaml:

shell_command:
  output1_1: echo -e -n "@W 00 00 #" > /dev/ttyUSB0

I have verified my USB to serial adapter is in fact /dev/ttyUSB0 and I have even been able to get this to change my input on the HDMI Matrix when I type it directly into the terminal. However, when trying to use it as a button calling the shell command, nothing happens. When I check the logs I see this error:

Error running command: echo -e -n "@W 00 00, return code: 2

Can anyone help me figure out what I am doing wrong? I am using the Generic x86-64 Home Assistant OS installed on an HP mini PC.

Home Assistant 2022.9.7
Supervisor 2022.09.1
Operating System 9.0
Frontend 20220907.2 - latest

If you go to “Settings > System > Hardware > kebab menu (3 dots) > All hardware”, does your USB show up as “/dev/ttyUSB0”? You might need to use a different device from within HA. You could also try replacing it with a file such as “/config/test.txt” and confirm it’s this that is the cause of the problem.

It does.

I looked again at my error and I’m noticing it cuts off at the # within my echo -e -n “@W 00 00 #” > /dev/ttyUSB0 command.

I’ve tried changing it to single quotes and that didn’t help. I then added a \ in front of the # thinking maybe I had to escape the special character. When I do that, I no longer get an error but it still does not change my output on my Matrix.

I suggest you try putting single quotes around the whole thing:

output1_1: 'echo -e -n “@W 00 00 #” > /dev/ttyUSB0'

Alternatively, on a separate line:

  output1_1: >-
    echo -e -n "@W 00 00 #" > /dev/ttyUSB0

In both cases, it should stop the “#” being interpreted as a comment.

1 Like

That worked! Thank you so much for your help!

How did you setup the Baudrate, parity etc…?