Send simple telnet command in HA with response

It took some time, but it is solved…I write down what I have learned (sorry if I am not fully right; but I am just a nooby in IT: but I will do my best).
Fisrt the question:
Is there an easy way to send a telnet command and wait for the “apr0” answer, if not retry)?

  • Telnet is not really available for that but “nc” seem to be the same effective… (Thx for koying)
  • The most problem caused for me the dos-Linux [enter] incompability and probable file acccess permission. So what I wrote in HA File Editor was not always acceptable for the bash script. So before running I needed to run in HA terminal: dos2unix / chmod 755 / chmod +x for the files I used.

So the concept:
1, A shell command (Thx for tom_l) (in configuration.yaml) starts a bash file (Thx for koying)

shell_command:
  send_po: "bash /config/po_with_response.sh"

2, The bash file sends to the av-reciever nc command for power on (“po”), then waits for response (“pwr0”) and depending on the response switches the HA input.boolean to on or off

-I needed for debugging a text file (log did not work for me) (use “>>” if text should be added as a new line)
'echo “Script started” > /config/send_nc_command_debug.txt ’

-The syntax to send the power_on_code (“po”) with nc; and wait for the response (this syntax is the key; with any changes it did not work for me):

response=$((echo -e 'po\r'; sleep 1) | nc -w 10 [ip] [port])

-change a HA boolean switch according to the response:

if echo "$response" | grep -q "APR0"; then
  curl -X POST -H "Authorization: Bearer [deleted auth key]" \
    -H "Content-Type: application/json" \
    -d '{"entity_id": "input_boolean.pioapwr"}' \
    http://homeassistant.local:8123/api/services/input_boolean/turn_on
else
...

III. A Yaml sintax will re-aply the script again, if it was not successfull at first run (sometimes the avreciever does not react … if there is an other telnet info running, the sent power_off or on code may be lost … so no response will come).

There is an example here:

Sorry, I am quite new, I stroggled sending my post. Now I finished above…:slight_smile:

Telnet switch does not work for me; since it triggers the reciever all the time (every second) so the reciever answers every sec and hangs after a while. I can see this in a telnet shell window…

How about a shell command then?

1 Like

It does not work for me:
Config.yaml:
shell_command:
send_telnet_command: ‘echo -e “apo\n” | telnet 192.168.188.33 23’

DeveloperTools/Action (YAML):
action: >-
shell_command.send_telnet_command

RESPONSE:
stdout: “”
stderr: “/bin/sh: telnet: not found”
returncode: 127

Second version:
Config.yaml:
send_vd_command: telnet 192.168.188.33 8102 <<< ‘vd’

Developertools/Action:
action: >-
shell_command.send_vd_command

Response:
stdout: “”
stderr: “/bin/sh: syntax error: unexpected redirection”
returncode: 2

You have no telnet command inside the HA container, but you have nc

1 Like

What would be the correct syntax?
This does not work::
config.yaml:
send_nc_command: “echo -e ‘apo’ | nc 192.168.188.33 23”
Developertools/Action:
action: >-
shell_command.send_vd_command
RESPONSE:
stdout: “”
stderr: “/bin/sh: syntax error: unexpected redirection”
returncode: 2

Best I achieved:
config.yaml shell_command:
send_nc_command3: nc 192.168.188.33 8102 “apo”

DEVelopersection/action:
action: >-
shell_command.send_nc_command3
Response:
stdout: “”
stderr: “BusyBox v1.36.1 (2024-06-10 07:11:47 UTC) multi-call binary.\n\nUsage: nc [OPTIONS] HOST PORT - connect\nnc [OPTIONS] -l -p PORT [HOST] [PORT] - listen\n\n\t-e PROG\tRun PROG after connect (must be last)\n\t-l\tListen mode, for inbound connects\n\t-lk\tWith -e, provides persistent server\n\t-p PORT\tLocal port\n\t-s ADDR\tLocal address\n\t-w SEC\tTimeout for connects and final net reads\n\t-i SEC\tDelay interval for lines sent\n\t-n\tDon’t do DNS resolution\n\t-u\tUDP mode\n\t-b\tAllow broadcasts\n\t-v\tVerbose\n\t-o FILE\tHex dump traffic\n\t-z\tZero-I/O mode (scanning)”
returncode: 1

Pipes in shell_command are problematic, iirc.
Try to wrap the command in a shell script and use bash /config/the_script.sh in the shell_command.

1 Like

Do I need to run the following command to ensure the script is executable (via Terminal)?

chmod +x /homeassistant/send_nc_command.sh

It is not working…nothing sent
Timed out running command: bash /config/send_nc_command.sh, after: 60 seconds

bash /config/send_nc_command.sh file content is:
echo -n ‘vd’ | nc 192.168.188.33 8102

Did you try the nc command in, i.e., the terminal addon, first?

yes, it can listen, but I can not send any data eg “vd” =volume down
If I send on a different pc-telnet session “Vd”, it shows up with the reaction; but if I type in the terminal nothing happens

HOwever if I usde -v -v option; it states the typed data is sent; but it is not true

2024-11-22_165142

Big step forward: HUrrraaaa

in Temrinal both works (“\r” was missed):
printf “vd\r” | nc 192.168.188.33 8102
echo “VD\r” | nc 192.168.188.33 8102

BUT there is not seeable any answer
Using the following code, I can see answers, but codes typed [and hit enter] are not accepted
nc 192.168.188.33 8102

One step better again:
in terminal; the code works and the answer is also seeable…

(echo “apo\r”; sleep 1) | nc 192.168.188.33 8102

Now I need a way to use this in HA script. Send “APO”; check the answer; if it is APR0 then switch a switch to “on”; if the answer does not contain “APR0” then try again sending “apo/r”