TCP commands for Ethernet relay

Netcat works fine, example:

echo -n 21 | nc -q 1 <ip_addr> <port>

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?

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

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 :smile: ) 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 :slight_smile:
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

# i need to send a long HEX string to turn something on/off; see the last bits 00 or 01
- platform: template
      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 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 1001
# created a py file here
  - platform: command_line
    command: python3 /config/
    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: template
    value_template: "{{ states.sensor.Prog.state[2:4] == '11' }}"
script below :(

import socket
import codecs
import binascii

host = ''
port = 1001
message = binascii.a2b_hex ("your string here")

mySocket = socket.socket()

data = mySocket.recv(1024)
data = binascii.hexlify(data).decode()

print (data)


btw, there is also a TCP sensor component, but never got it to work, i dont know what i needed to send as payload to retrieve the string i wanted

so if yiu got that to work, please share

As soon as possible I’m going to try it and if it works I certainly share the code!

Ok, it seems it works fine in this way. Example for the first relay:

  - platform: command_line
        command_on: echo -n 11 | nc -q 1 <ip_addr> <port>
        command_off: echo -n 21 | nc -q 1 <ip_addr> <port>
        command_state: echo -n 00 | nc -q 1 <ip_addr> <port>
        value_template: "{{ value[0:1] == '1' }}"
        friendly_name: Relay 1

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.


Yes , that’s an approach, I use a template switch, that calls a script… the script is then the shell command

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

The scan_interval description is hidden in a place no-one would ever think to look

1 Like

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

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

I used -q 1 to wait 1 second before close the socket allowing the device to send me the answer. I use it for as feedback.

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.

However, on my setup including -q 1 will not let the rest of the command work and I don’t know why

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.

i am using hassos, and indeed -q isnt an available option :frowning:

Please can you tell me what hardware you are controlling with this? I have a need to do something similar.