Sending TCP packets to a device

Maybe it’s because of the line endings - echo normally adds a LF (linefeed) character to the end of a string.
Depending on what your device expects, you can try

echo -ne "..."

(where the -n supresses any newline characters) and add a "\n" (<LF>) or "\r\n"(<CR><LF>) manually - depending on what your device is looking for (this should be in the protocol specifications).

For debugging, you could point the IP address to another system, run nc in server mode there (-l <port> option) and pipe that into a tool like e.g. hexdump just to see if your command a) works at all and b) is sending the exact string you’re expecting.

Sebastian

1 Like

https://community.home-assistant.io/t/possible-to-have-a-button-send-custom-rs232-commands/259522/4?u=docsparks binbash and proper quoting was my save all, best of luck.

Thank you very much for sharing this! I’ve been stuck with trying to send netcat commands from ssh on the new Blue box. Thanks for sharing your fix!

Hi to all!
I was wondering how to make a TCP Call to my SR201 Web realy bougth on Bang Good, and found this article very helpful.

Thanks to all because I managed to create a switch reading around this article in minutes.

Now I would like to ask one question:

  • I use HOMEKIT to monitor devices with my phone, the SWITCH appears n the devices shown in homekit, but when I press the switch (it works), but it remain on, even if the switch turns off by itself after 1 second.

the code is this

switch:     
- platform: command_line
  scan_interval: 10
  switches:
    apri_cancello:
      command_on: echo -n "12*" | nc 192.168.20.14 6722
      command_state: echo -n 00 | nc -q 1 192.168.20.14 6722
      value_template: "{{ value[0:1] == '1' }}"
      friendly_name: "Apri Cancello"
      icon_template: mdi:gate

NOTE: I do not use command_state_off because the switch turns on and off by itself (like a push button or a switch with inching mode on). The icon on homeassistant behave well, it turns on and off after 1 second, but in homekit the switch keep to stay pressed (on) i need to push it again.

I’m trying to send commands to my alarm but it’s not working through the home assistant.

If I send through a VM with ubuntu the alarm gets the command, but using shell command by home assistant doesn’t work.

ubuntu:

echo -e "\x08\xe9\x21\x31\x32\x33\x34\x44\x21\x5e" | nc 192.168.0.15 9009

HA:

amt_ativar: echo -e "\x08\xe9\x21\x31\x32\x33\x34\x41\x21\x5b" | nc 192.168.0.15 9009

Any idea?

Hard to say without logs.

There’s nothing in the logs. :thinking:

Full logs
2022-09-03 11:14:34.435 WARNING (SyncWorker_0) [homeassistant.loader] We found a custom integration hacs which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2022-09-03 11:14:34.438 WARNING (SyncWorker_0) [homeassistant.loader] We found a custom integration rpi_gpio which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2022-09-03 11:14:34.439 WARNING (SyncWorker_0) [homeassistant.loader] We found a custom integration dht which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2022-09-03 11:14:34.441 WARNING (SyncWorker_0) [homeassistant.loader] We found a custom integration localtuya which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2022-09-03 11:14:34.443 WARNING (SyncWorker_0) [homeassistant.loader] We found a custom integration next_holiday which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2022-09-03 11:14:34.445 WARNING (SyncWorker_0) [homeassistant.loader] We found a custom integration duckdns_ipv4_ipv6 which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2022-09-03 11:14:34.447 WARNING (SyncWorker_0) [homeassistant.loader] We found a custom integration arpscan_tracker which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2022-09-03 11:14:45.921 WARNING (MainThread) [homeassistant.components.number] custom_components.localtuya.number::LocaltuyaNumber is overriding deprecated methods on an instance of NumberEntity, this is not valid and will be unsupported from Home Assistant 2022.10. Please report it to the custom integration author.
22-09-03 11:49:57 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)
22-09-03 11:51:04 INFO (MainThread) [supervisor.homeassistant.api] Updated Home Assistant API token
22-09-03 11:54:59 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)
22-09-03 12:00:01 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)
22-09-03 12:05:03 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)
22-09-03 12:10:05 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)
22-09-03 12:15:07 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)
22-09-03 12:19:59 INFO (MainThread) [supervisor.resolution.check] Starting system checks with state CoreState.RUNNING
22-09-03 12:19:59 INFO (MainThread) [supervisor.resolution.checks.base] Run check for IssueType.NETWORK_CONNECTION_PROBLEM/ContextType.SYSTEM
22-09-03 12:19:59 INFO (MainThread) [supervisor.resolution.checks.base] Run check for IssueType.DNS_SERVER_IPV6_ERROR/ContextType.DNS_SERVER
22-09-03 12:19:59 INFO (MainThread) [supervisor.resolution.checks.base] Run check for IssueType.PWNED/ContextType.ADDON
22-09-03 12:19:59 INFO (MainThread) [supervisor.resolution.checks.base] Run check for IssueType.FREE_SPACE/ContextType.SYSTEM
22-09-03 12:19:59 INFO (MainThread) [supervisor.resolution.checks.base] Run check for IssueType.SECURITY/ContextType.CORE
22-09-03 12:19:59 INFO (MainThread) [supervisor.resolution.checks.base] Run check for IssueType.DNS_SERVER_FAILED/ContextType.DNS_SERVER
22-09-03 12:19:59 INFO (MainThread) [supervisor.resolution.checks.base] Run check for IssueType.TRUST/ContextType.SUPERVISOR
22-09-03 12:19:59 INFO (MainThread) [supervisor.resolution.check] System checks complete
22-09-03 12:19:59 INFO (MainThread) [supervisor.resolution.evaluate] Starting system evaluation with state CoreState.RUNNING
22-09-03 12:20:00 INFO (MainThread) [supervisor.resolution.evaluate] System evaluation complete
22-09-03 12:20:00 INFO (MainThread) [supervisor.resolution.fixup] Starting system autofix at state CoreState.RUNNING
22-09-03 12:20:00 INFO (MainThread) [supervisor.resolution.fixup] System autofix complete
22-09-03 12:20:09 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)
22-09-03 12:21:00 INFO (MainThread) [supervisor.updater] Fetching update data from https://version.home-assistant.io/stable.json
22-09-03 12:21:05 INFO (MainThread) [supervisor.homeassistant.api] Updated Home Assistant API token
22-09-03 12:25:11 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)
22-09-03 12:30:13 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)
22-09-03 12:34:21 INFO (MainThread) [supervisor.host.info] Updating local host information
22-09-03 12:34:22 INFO (MainThread) [supervisor.host.services] Updating service information
22-09-03 12:34:22 INFO (MainThread) [supervisor.host.network] Updating local network information
22-09-03 12:34:23 INFO (MainThread) [supervisor.host.sound] Updating PulseAudio information
22-09-03 12:34:23 INFO (MainThread) [supervisor.host.manager] Host information reload completed
22-09-03 12:35:15 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)
22-09-03 12:40:17 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)
22-09-03 12:45:19 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)
22-09-03 12:50:21 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)
22-09-03 12:51:05 INFO (MainThread) [supervisor.homeassistant.api] Updated Home Assistant API token
22-09-03 12:55:23 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)
22-09-03 13:00:25 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)
22-09-03 13:05:27 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)
22-09-03 13:10:29 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)
22-09-03 13:15:31 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)
22-09-03 13:20:00 INFO (MainThread) [supervisor.resolution.check] Starting system checks with state CoreState.RUNNING
22-09-03 13:20:00 INFO (MainThread) [supervisor.resolution.checks.base] Run check for IssueType.NETWORK_CONNECTION_PROBLEM/ContextType.SYSTEM
22-09-03 13:20:00 INFO (MainThread) [supervisor.resolution.checks.base] Run check for IssueType.DNS_SERVER_IPV6_ERROR/ContextType.DNS_SERVER
22-09-03 13:20:00 INFO (MainThread) [supervisor.resolution.checks.base] Run check for IssueType.PWNED/ContextType.ADDON
22-09-03 13:20:00 INFO (MainThread) [supervisor.resolution.checks.base] Run check for IssueType.FREE_SPACE/ContextType.SYSTEM
22-09-03 13:20:00 INFO (MainThread) [supervisor.resolution.checks.base] Run check for IssueType.SECURITY/ContextType.CORE
22-09-03 13:20:00 INFO (MainThread) [supervisor.resolution.checks.base] Run check for IssueType.DNS_SERVER_FAILED/ContextType.DNS_SERVER
22-09-03 13:20:00 INFO (MainThread) [supervisor.resolution.checks.base] Run check for IssueType.TRUST/ContextType.SUPERVISOR
22-09-03 13:20:00 INFO (MainThread) [supervisor.resolution.check] System checks complete
22-09-03 13:20:00 INFO (MainThread) [supervisor.resolution.evaluate] Starting system evaluation with state CoreState.RUNNING
22-09-03 13:20:00 INFO (MainThread) [supervisor.resolution.evaluate] System evaluation complete
22-09-03 13:20:00 INFO (MainThread) [supervisor.resolution.fixup] Starting system autofix at state CoreState.RUNNING
22-09-03 13:20:00 INFO (MainThread) [supervisor.resolution.fixup] System autofix complete
22-09-03 13:20:33 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)
22-09-03 13:21:06 INFO (MainThread) [supervisor.homeassistant.api] Updated Home Assistant API token
22-09-03 13:25:35 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)
22-09-03 13:30:37 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)
22-09-03 13:31:45 INFO (MainThread) [supervisor.api.middleware.security] /network/info access from core_ssh
22-09-03 13:31:45 INFO (MainThread) [supervisor.api.middleware.security] /network/info access from core_ssh
22-09-03 13:31:45 INFO (MainThread) [supervisor.api.middleware.security] /host/info access from core_ssh
22-09-03 13:31:45 INFO (MainThread) [supervisor.api.middleware.security] /core/info access from core_ssh
22-09-03 13:33:10 INFO (MainThread) [supervisor.api.proxy] Home Assistant WebSocket API request initialize
22-09-03 13:33:11 INFO (MainThread) [supervisor.api.proxy] WebSocket access from a0d7b954_vscode
22-09-03 13:33:11 INFO (MainThread) [supervisor.api.proxy] Home Assistant WebSocket API request running
22-09-03 13:35:39 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)
22-09-03 13:36:24 INFO (MainThread) [supervisor.api.proxy] Home Assistant WebSocket API connection is closed
22-09-03 13:40:28 INFO (MainThread) [supervisor.api.proxy] Home Assistant WebSocket API request initialize
22-09-03 13:40:29 INFO (MainThread) [supervisor.api.proxy] WebSocket access from a0d7b954_vscode
22-09-03 13:40:29 INFO (MainThread) [supervisor.api.proxy] Home Assistant WebSocket API request running
22-09-03 13:40:41 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)
22-09-03 13:45:43 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)
22-09-03 13:50:45 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)
22-09-03 13:51:06 INFO (MainThread) [supervisor.homeassistant.api] Updated Home Assistant API token
22-09-03 13:55:47 WARNING (MainThread) [supervisor.addons.options] Option 'cnames' does not exist in the schema for Mosquitto broker (core_mosquitto)

If I use -v in Terminal & SSH it returns OPEN and the command is not executed

echo -e "\x0a\xe9\x21\x31\x32\x33\x34\x35\x36\x41\x21\x5a" | nc -v -w 2 192.168.0.15 9009

Can you give some more context to this?

You mean about the code?

\x08\ Number of bytes to be sent
xe9\ Command (as they call it in the documentation)
x21\ Frame limiter (as they call it in the documentation)
x31\x32\x33\x34\ password
x44\ Deactivation code
x21\Frame limiter (as they call it in the documentation)
x5e Checksun

I was trying to use the TCP sensor to communicate with the alarm, but I couldn’t.
I don’t know if it was in the cache, but I deleted it from the yaml file and it kept showing up in the UI.
Only after several restarts it disappeared.
I thought it was blocking the port, but apparently it’s not.

This was the code I used for the sensor

- platform: tcp
  name: TESTING_TCP
  host: 192.168.0.15
  port: 9009
  payload: "\x08\xe9\x21\x31\x32\x33\x34\x5b\x21\x41"

I completely turned off the alarm and raspberry, still nothing.

If I open wireshark I can see the communication when I use ubuntu.
But nothing about HA

I managed to solve the problem with the shell command with @Luuk_Neele tip.

Any way to use the tcp sensor and get the response?

- platform: tcp
  name: AMT 4010 TCP
  host: 192.168.0.15
  port: 9009
  payload: "\x0a\xe9\x21\x36\x38\x36\x31\x37\x33\x5b\x21\x4a"
2022-09-06 15:48:15.277 WARNING (SyncWorker_0) [homeassistant.components.tcp.common] Timeout (10 second(s)) waiting for a response after sending '\né!686173[!J' to 192.168.0.15 on port 9009

Response

37 E9 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 41 52 01 00 00 11 0F 33 06 09 16 00 00 00 00 00 0F 00 01 00 00 02 00 00 00 00 00 00 00 00 0B

Hello,

I have the same problem. Can you tell me how you solve this issue?
I send the below command_line and doesnt do anything, WIth udp which i have worked before the commands works immidiately and if the remote device was out of the network i had error at HA. Now with tcp nothing, I will appreciate if you can help me with this issue. I have managed to send with third party softwares the command from my pc to the remote device

switch:
  - platform: command_line
    switches:
       audac:
        command_on: 'echo  "#|M001|F001|SV01|40|U|\r\n" | nc 192.168.168.202 5001'
        command_off: 'echo "#|M001|F001|SV01|60|U|\r\n" | nc 192.168.168.202 5001'

I ended up using Node Red.
There are TCP and UDP nodes

Thanks. The udp connections works perfectly. Do we know why the tcp commands doesn’t work at all?

1 Like

Find this:

This is a sensor, Not for sending ASCII tcp commands. Thank you very mush for your time anyway!

Try this Send TCP/UDP Packets on Linux using /dev/tcp & /dev/udp Pseudo Devices | Platform Engineer

Hello Nick,

I didn’t see that you replied.
I will check it today and i will inform you about the result.
I will try this type of syntax at ssh terminal of Hassos?
Thank you very much!

I know that is for linux but i made a try from Hassos.
image

I investigated this further. I originally thought this was a kernel feature, but found it is in fact a bash feature. Try it in bash, not zsh.

Is there any way to send payload as template (state of some sensor) ??
Pretty lot of devices simply listens for some info (temp, hum, state, status). Without any response.
So TCP sensor is perfect way to send them data - no need to use command_line or shell_command, no automations…
As for now it can send value, but doesn’t convert template to value.