I’m not sure what is the best component to use to control an Ethernet relay board. Despite what the vendor says, there is not rest API, just a plain TCP socket communication. Here what I’m trying to do:
eight lines, let’s call them CH1…CH8
I have ip address and port
to switch on a line I have to send over a TCP socket 1n where n is the number 1…8 (i.e. 11 turns on the first line)
to switch off send 2n (i.e. 21 turns off the first line)
adding :x will add a delay in seconds (i.e. 11:10 will turn on CH1 for 10 seconds)
after sending a command, the device answers with eight chars to show the current state. I.e. 00110000 means CH3 and CH4 are on. This should be used as feedback
on power up you can send 00 to read the current state
What component is more suitable for this type of switch?
If you are using linux, a command line switch using the netcat (nc) command to send tcp packets would be one way. But I only know of netcat, I couldn’t tell you the syntax to use.
Now I know how I can use a command line switch to trigger a command (what a pity there isn’t a TCP/UDP component in HA!). But how to parse the answer to be used as feedback? Something like it does for MQTT devices!
Can you send this at any time, or does it have to be on power up?
Any time, I said on power up to retrieve the current state. After you can either poll with this command (it would works but it’s ugly ) or just parse the answers.
I have exact the same setup, command line switch, I also retrieve a state like 001100000 , I use a sensor for that
For the command line I do a netcat to send tcp stuff, and a curl to read TCP stuff
Got it. I’m going to follow your hints. If I will have any trouble while making the value template string to parse the received data I will open a new issue. By the way I hope in the future HA will support plain TCP switch/sensor natively, without rely on command line ones.
here is my config, i am not a programmer at all, so if your code is better, please post your result
anyway, i my house i have an IP controller board to turn on lights/switches/covers/ventilation, some kind of domitica system
so i wanted to control this also with HA, so thats why i needed to get the state of switches/stuff, so it was als represented in HA
i dont think you can retrieve a status with netcat, its only to send, so thats why i use a python for that
anyway, here is an example of what i did
# SENDING CONFIG
# i need to send a long HEX string to turn something on/off; see the last bits 00 or 01
- platform: template
switches:
sc_living:
friendly_name: Living
command_on: echo -e "\xED\x43\x31\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xAF\xAF\x43\x07\x01" | nc 192.168.0.10 1001
command_off: echo -e "\xED\x43\x31\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xAF\xAF\x43\x07\x00" | nc 192.168.0.10 1001
#RECEIVING config
# created a py file here
sensor:
- platform: command_line
command: python3 /config/dobissreceive.py
name: Prog
scan_interval: 10
#so now my sensor contains the state of my relias
#so in my template lights/switches the state will change, or you can use automations whatever with a value template
#fox example, if you want to know if 001100000 , the 3th and 4th bit is ON ? 11 =>
#in template light/switch =>
value_template: "{{ states.sensor.Prog.state[2:4] == '11' }}"
#or in automations:
- entity_id: sensor.Prog
platform: state
condition:
- condition: template
value_template: "{{ states.sensor.Prog.state[2:4] == '11' }}"
script below :(dobissreceive.py)
#!/usr/bin/python3
import socket
import codecs
import binascii
host = '192.168.0.10'
port = 1001
message = binascii.a2b_hex ("your string here")
mySocket = socket.socket()
mySocket.connect((host,port))
mySocket.send(message)
data = mySocket.recv(1024)
data = binascii.hexlify(data).decode()
print (data)
mySocket.close()
Clicking on the switch icon in the UI it toggles its state invoking command_on or command_off. But if I change the state of the relay remotely (i.e. from another machine) after a while HA invokes the command_state and parse the answer, showing the actual state of the relay. Great!
I cannot find in the documentation the interval between the command_state calls.
So your echo -n command state is also actually working to retrieve a status of your IP controller? I never got that to work… I though netcat was only for sending tcp commands, not to retrieve the actual result
You have no idea how many months I have been trying to find some sort of command that would make that type of relay work with Hass.io.
I was trying with a telnet switch, which ended up turning itself on after 7 minutes every time!
This worked for me perfectly. The only change I made is I used nc <ip_addr> <port> instead. Include -q 1 it didn’t work. I’m not sure what that means and if it’s important though
So netcat is also sending feedback ? Some kind of response? I didn’t know that, I though netcat was always was for sending only
I created my commands in py for my receiving stuff…
Maybe if I tried the -q 1 before ;(
netcat doesn’t “send” anything as feedback. Is the device that (may) send back an answer. If you keep the socket open enough nc will be print it on stdout.
netcat comes in several different versions. You should carefully read the man pages of your exact version. For example, mine came from an old version of busybox and there was no way to get it works. I had to install a (parallel) newer version of busybox and then I was able to do what I need.