Possible to have a button send custom rs232 commands?

Is it possible to send custom rs232 commands? I have an amplifier that there currently isn’t integration but it supports rs232 and I want to know if I can write buttons that would send set commands to it.

Thank you

Home Assistant has a Serial integration however it is designed to create a sensor for receiving data via a serial port. If you want to send data via a serial port, you will need to use some other method. If you are already using Node-Red with Home Assistant, that would be the simplest way to do it.

Alternately, you could create a shell script to send the command (to the serial port) and call the script from Home Assistant.

You might have a look at this combo of espHome and esp8266 hardware:

UART Bus

Malouf Electronics ESP8266 Development Board w/ RS232 Interface
https://www.amazon.com/dp/B08GY2GTW5/ref=cm_sw_em_r_mt_dp_BFo5FbK7668YE

I have used the Malouf boards for some tests. What is nice about this board is that is has RS-232 interface at correct electrical levels built in, so I think you could create a small clean device next to your amp pretty easily. Minimal hardware setup, just a USB power brick, box for Malouf esp8266, serial cable to amp. You will have a little work to do to figure out the RS-232 wiring, it is sometimes a head scratcher to get RX/TX correct and if the amp requires any of the other RS-232 lines set in order to communicate.

I would use a MQTT server in addition to Home Assistant, easy to create UI buttons and other in Lovelace publish commands via MQTT to espHome devices via this route.

I have spent the last year trying to figure out how to control a few devices i own with a usb-to-serial interface, and to say the least at times i wanted to hang myself. have came on this forum searching for answers and ended up with such a messy set of trial and error setups i couldnt think straight at times and it was such a mess i had to stop and revisit countless times.

in short, like two weeks ago i had gotten to the point where i had taken an arduino uno, 2 line LCD shield, and usb cable and wrote code to examine exactly what homeassistant was sending. I mean a total nightmare to get this handled.

absolute nightmare. why even force almost outdated hardware to work with homeassistant? because we can thats why.

if you need help feel free to alert me somehow and ill try to do my best to monitor the forums a tad more over the next week as i try to work on something else…

Not sure if you said you’re using a usb to serial cable but if you are, this should help get you started

sensor:
  - platform: serial # arduinouno
    serial_port: /dev/ttyACM0
    name: usb3
    baudrate: 9600

  - platform: template
    sensors:
      usb_sensor3:
        friendly_name: usb sensor3
        value_template: "{{ states('sensor.usb3') }}"

shell_command:
  temp_io1: /bin/bash -c "echo -n '{{ states.input_text.usbtext.state }}' >> /dev/ttyACM0"
  temp_io2: /bin/bash -c "echo -e -n '{{states('input_text.usbtext')}}'>> /dev/ttyACM0"

and in the lovelace ui, created a button with this yaml…

type: button
tap_action:
  action: call-service
  service: shell_command.temp_io1
name: io1
show_icon: false
icon_height: 4px
show_name: true

i had an input_text area so i could trial n error the \n and \r \n types of commands that are apparently common in bash or echo commands? idk. nightmares.

the difference in the echo -n and echo -e -n commands are to aid in the adding and usage of the \n and \r type of parameters that can be sent and how they are interpeted.

the way the serial port integration and the way it handles the incoming \r and \n parameters, is beyond me and almost required i re-make the serialport integration as a custom component so i could fingerbang that too, but ultimately the issue with months and months of trial and error, was adding the ‘/bin/bash -c’ text before the echo commands. I use homeassistant image on a rpi4 , so the countless outdated forum posts about running homeassistant as a sub image on a rpi install and these words like docker, containers, root privledges, all greek to me. again this was an absolute nightmare.

also, this code worked as well, in place of 1 call from the UI button to the shell command, this rediculous amount of extra was deemed necessary in the past apparently but not atm, so both the shell command above, and this script passing, also work…

shell_command:
  temp_value2: /bin/bash -c "echo '{{ value }}' > /dev/ttyACM0"

script:
  set_temp_value2:  
    alias: set template value2
    sequence:
    - service: shell_command.temp_value2
      data_template:
        value: >-
                {{ states('input_text.usbtext') }}  

i hope for the love of god this helps someone else.
would love to hear back any updates or improvements of how you manage to make it work.

best of luck

2 Likes

Sorry to hear it was such a struggle. It’s one of the ways I suggested (Serial Sensor and she’ll scripts) but an even easier way is via Node-Red which supports bi-directional serial communications and is tightly integrated with Home Assistant.

Ideally, there ought to be an integration supporting bi-directional serial comms (as well as one for UDP).

1 Like

i dont blame you! haha. and learning node just isnt an option for me atm however i did tinker with it slightly. but again, entire other chapter in homeassistant for me.

the headache was truly from trying to figure out how to send templatable lines to the command lines, having pretyped commands worked semi-quickly with what i found on the forum but the changes over time to each integration and regards to how templating worked, again, a struggle.

thanks again for your pitty , for real. it sucked.

THANK YOU for Posting this! I was going nuts until I realized thanks to your post, that HA does NOT allow “shell helpers” like ~ or even >. Since I was trying to send commands to a serial port I was stuck.

Using your approach with /bin/bash -c solved my issue :slight_smile: