Question about new Telnet Switch

Hey, I have a question about the new telnet switch added in 0.54.0.

Basically, I want to know if it’s possible to send >1 command in the command_on and command_off sections. In order for my telnet to work properly, I need to first unlock everything (by sending unlock) then I can change the status and finally I should lock it back (using lock). So basically it looks like this:

unlock
setstatus:on
lock

Is this possible in it’s current state? Also just for full disclosure I’m running hass.io

I have question as well, without creating a new topic.
Main question how is the authentication handled as this is not documented.
And also how would I get the command/status of the following I step I need to perform on/off/status:

    #!/usr/bin/expect
    spawn telnet 192.168.1.61 2024
    expect ">Ready"
    send "\r"  # required ENTER command
    send "Pass password \r" # Authentication + enter command
    send "Setrelay 34,1 \r" # switch ON + enter command
    >Relay 34,ON   
    send "Setrelay 34,0 \r" # switch OFF
    >Relay 34,OFF
    send "Getrelay 34\r"   # get switch status 
    >Relay 34,ON

I just had to deal with this, I ended up just making a script.

I am running hassio to, what you need to do is add a script file to your config folder make it executable then add

{ sleep 1; echo "unlock"; echo "setstatus:on"; echo "lock"; } | telnet [your ip]

if you dont sleep for 1 it doesn’t have time to connect to the server.

Out of interest is this for Prismatik? cos that’s what I needed this for and if it is then you have you lock and unlock in the wrong order.

and just encase it is here is all I did

2 files one ambiOn.sh ambiOff.sh both in a folder ambilight then add this to your configuration.yaml

  - platform: command_line
    switches:
      AmbilightTV:
        command_on: /config/ambilight/ambiOn.sh
        command_off: /config/ambilight/ambiOff.sh

ambiOn.sh = { sleep 1; echo “lock”; echo “setstatus:on”; echo “unlock”; } | telnet 0.0.0.0 3636

ambiOff.sh = { sleep 1; echo “lock”; echo “setstatus:off”; echo “unlock”; } | telnet 0,0,0,0 3636
Going to look in to using the state for the switch tomorrow.

1 Like

Yes! I am actually using it for that. Just thought it’d make more sense to anyone helping.

And I tried doing what you were talking about, but I can’t get it to quite work out for some reason. Not sure what I have wrong exactly.

I have this in my script:
{ sleep 1; echo "unlock"; echo "setstatus:on"; echo "lock"; } | telnet 192.169.1.71 3636

I’m pretty sure I made it executable, I ran chmod 755 /config/ambilight/ambiOn.sh in my ssh.

And then this is in my configuration.yaml:

switch:
  - platform: command_line
    switches:
      ambibox:
        command_on: /config/ambilight/ambiOn.sh
        command_off: /config/ambilight/ambiOff.sh

But when I flip the switch, nothing happens.

Can you Telnet in to the device before trying to run the script?
Have you unticketed the local only box in experimental?
Do you have an api key set? as that would need to be added to the script if you do.

Also from running this for a bit it seems that if i reboot the “experimental feature” disables it self and renders the script useless.

Yup, I can telnet in just fine at all times using putty really. That doesn’t seem to be a problem.
No idea what the local only box is in experimental.
No api key, just been using telnet.

So you can Telnet in to it from a machine its not running on? If not then you need to disable local access only.

Failing that i am not sure I can help much more.

Just read one of your previous post you still have lock and unlock in the wrong order
ambiOn.sh should have

{ sleep 1; echo "lock"; echo "setstatus:on"; echo "unlock"; } | telnet 192.169.1.71 3636

Thanks for the above suggestions, it allowed me to integrate my Lightpack (Prismatik) into my HASS.io instance.

However, I adjusted the commands to also exit gracefully otherwise the connection hangs for too long and further requests are not responded to until the first connection is dropped. I also added the command to get the current state of the lightpack.

Since these are simple lines of code, I prefer using command_line instead of shell scripts so that things like API keys can stay secrets while still be able to share the code.

- platform: command_line
  switches:
    lightpack:
      friendly_name: Lightpack
      # Command used:
      # '{ sleep 1; echo -e "apikey:<optional_api_key>"; sleep 1; echo -e "lock"; echo -e "setstatus:on"; echo -e "unlock"; echo -e "exit"; sleep 1; } | telnet <hostname-or-ip-address> <port>'
      command_on: !secret lightpack_on
      # Command used:
      # '{ sleep 1; echo -e "apikey:<optional_api_key>"; sleep 1; echo -e "lock"; echo -e "setstatus:off"; echo -e "unlock"; echo -e "exit"; sleep 1; } | telnet <hostname-or-ip-address> <port>'
      command_off: !secret lightpack_off
      # Command used:
      # '{ sleep 1; echo -e "apikey:<optional_api_key>"; sleep 1; echo -e "getstatus"; echo -e "exit"; sleep 1; } | telnet <hostname-or-ip-address> <port> | grep -q ''status:on'''
      command_state: !secret lightpack_state

FYI, a kind soul has created a Lightpack component that replaces the need of shell commands altogether: https://www.reddit.com/r/Lightpack/comments/7b9qrj/home_assistant_support/

That component almost works perfectly! I’m using Ambibox for its better performance. And the api is very similar other than not needing an api key (I just commented those lines out) but I seem to be having troubles with it recognizing when the status changes outside of HA. So if I manually turn it on/off it stays its state. Do you know how often it updates? Or if there is something I’m missing…

I’ve been trying to make it work with my nas to shut down.
The question is how you make sh file executable?
After adding busybox extras I can execute run sh file in terminal but I can’t make it work from command line switch.

chmod +x shfilename

The shell file should have a “shebang” first line pointing to the shell executable, such as:

#!/bin/sh

Nothing happened when I run chmod +x
Should it show that it is done?

So sh file content should be like
#! /bin/sh
{ sleep 1; echo “lock”; echo “setstatus:on”; echo “unlock”; } | telnet 192.169.1.71 3636

my config is:

# Command_line     
  - platform: command_line
    switches:
      networknas:
        friendly_name: Networknas
        command_on: /config/nas/nason.sh
        command_off: /config/nas/nasoff.sh

and sh files like:

#!/bin/sh
{ sleep 1; echo user; sleep 1; echo password; sleep 1; echo 'poweroff'; sleep 5; } | telnet 192.168.86.140

im geting these errors in log one for on command one for off.

Log Details (ERROR)
Logger: homeassistant.components.command_line.switch
Source: components/command_line/switch.py:162
Integration: command_line (documentation, issues)
First occurred: 14:26:24 (11 occurrences)
Last logged: 14:49:00

Command failed: /config/nas/nason.sh

and

Log Details (ERROR)
Logger: homeassistant.components.command_line.switch
Source: components/command_line/switch.py:168
Integration: command_line (documentation, issues)
First occurred: 14:26:40 (6 occurrences)
Last logged: 14:46:44

Command failed: /config/nas/nasoff.sh

Idealy i want to use WOL to switch NAS on and telnet script to switch NAS off