Moving from Vera home automation to HA (running hass.os on RPi4) I have a challenge to control 8 relay board. It is almost identical with this one: https://www.aliexpress.com/item/32788609819.html (at least when it comes to controlling [read/write with RS232]). It was working very well with Vera using USB-RS232 converter.
When connecting USB to RPi4 it is getting recognised (“FTDI USB Serial Device converter now attached to ttyUSB0”) so very first steps are positive.
I am not an expert on programming (but with some examples I believe I can adpdate/adjust existing code) and looking for help in finding simplest and easiest way to make it working.
I believe I need to configure 8 switches which will:
a) read the status from the board (sending request and then receiving status)
b) change the status of the relay (sending data and then receiving comfirmation)
It seems realtively simple. I believe I have read most (if not all) post related to RS232 on this forum and coudl not get clear idea of next steps.
Appreciate any hint or link to help me with this. I know I can change the board to other with already exisitng integration but since I have two of them and all wiring is in place I first check possibliity to integrate this with HA.
Maybe you can get something out of this post.
Thank you @Tomahawk,
I am kind of sure that this is good material to use. It is about bi-directional communication with device using RS232 protocol. It seems for the firts view quite large and many thinks to understand - as your integration at the end grew being quite universal and comprehensive.
I am still looking for something simple to adopt and have this silution in mind. Thanks.
More reading brough so far simple solution with command line. I am not sure if this is a final solution but so far the part to turn the switch on/off works.
Here is the yaml definition:
- platform: command_line
switches:
8ch_relay01:
command_on: 'echo -e "\x55\x01\x12\x00\x00\x00\x01\x69" > /dev/ttyUSB0'
command_off: 'echo -e "\x55\x01\x11\x00\x00\x00\x01\x68" > /dev/ttyUSB0'
8ch_relay02:
command_on: 'echo -e "\x55\x01\x12\x00\x00\x00\x02\x6A" > /dev/ttyUSB0'
command_off: 'echo -e "\x55\x01\x11\x00\x00\x00\x02\x69" > /dev/ttyUSB0'
8ch_relay03:
command_on: 'echo -e "\x55\x01\x12\x00\x00\x00\x03\x6B" > /dev/ttyUSB0'
command_off: 'echo -e "\x55\x01\x11\x00\x00\x00\x03\x6A" > /dev/ttyUSB0'
8ch_relay04:
command_on: 'echo -e "\x55\x01\x12\x00\x00\x00\x04\x6C" > /dev/ttyUSB0'
command_off: 'echo -e "\x55\x01\x11\x00\x00\x00\x04\x6B" > /dev/ttyUSB0'
8ch_relay05:
command_on: 'echo -e "\x55\x01\x12\x00\x00\x00\x05\x6D" > /dev/ttyUSB0'
command_off: 'echo -e "\x55\x01\x11\x00\x00\x00\x05\x6C" > /dev/ttyUSB0'
8ch_relay06:
command_on: 'echo -e "\x55\x01\x12\x00\x00\x00\x06\x6E" > /dev/ttyUSB0'
command_off: 'echo -e "\x55\x01\x11\x00\x00\x00\x06\x6D" > /dev/ttyUSB0'
8ch_relay07:
command_on: 'echo -e "\x55\x01\x12\x00\x00\x00\x07\x6F" > /dev/ttyUSB0'
command_off: 'echo -e "\x55\x01\x11\x00\x00\x00\x07\x6E" > /dev/ttyUSB0'
8ch_relay08:
command_on: 'echo -e "\x55\x01\x12\x00\x00\x00\x08\x70" > /dev/ttyUSB0'
command_off: 'echo -e "\x55\x01\x11\x00\x00\x00\x08\x6F" > /dev/ttyUSB0'
I know that it is not elegant and universal, but my goal at this point was to achieve connectivity and be able to control relays.
Now, the main challenge to continue “short term goal” is to read the status of relay from RS232 to be included in command_status section of that swich. Which command woudl you recommend to read from /dev/ttyUSB0 ?
Any other hint/comment/suggestion to this will be very much appreciated.
After more then month the controlling of the relays works 95% OK. 5% of not working is due to the occasions that in HA switch is OFF or ON and in reality the state is opposite (it seems that command did not reach the switch).
It might be the reason that from command line the “command” did not went correct and also that I am still not able to read the status of the switches from the board (serial read commands).
I believe the solution here is to write piece of code (not commend line) to control the board better and to read the status to update HA.
Still looking for hints and suggestions where to start. Maybe there is simple code to communicate with some device using serial interface I can adjust (in a way it is only about to send right code which I know and read the response).
Any help ?
ive spent well over a week trying to find proper use of a usb-relay connected to a rpi in homeassistant.
ive used homeassistant for about a year with a pretty good understanding of its use.
the fact that i cant find a single proper updated post that isnt people linking to other posts for responses for advice, is fucking bullshit.
i hope you find a way to do what you want, as i am also trying to do the exact same god damn thing.
if i make any progress, i will be sure to post here
- still a noob in regards to appdaemon, python, and other outside implementations
Thank you for this.
I gave up although I feel that to create simple python script should be rather easy and simple (all the command are known). I have made this in a way in Node-RED but then did not have a time to connect to HA.
At the end as the above solution was not stable I have switched to another one (MCP23017 + 8 relay board controlled with low/hod state) and I am happy.
As there will be some help here or I may have some more time in winter maybe will make a script or more stable solution. If I find anything, will post it here.
i am mid sucidial headache at the moment trying to run a python script from a shell command that includes a value template. updates later today hopefully.
Maybe my solution can help you. It s basically converter between rs232 (usb uart) and mqtt.
I have relay board like this:
My code can be found here:
https://github.com/MisoHampel/rs232-relay-mqtt
Thank you @MisoHampel.
I will consider this in the future, however my direction was to create the code from ESPHome to directly communicate with board (with no MQTT).
Anyhow I went fro other boards as I needed more of them and this one (RS232) is waiting for some free time to develop the code to use it.
Thanks.