Is it possible to create a sensor via Telnet?, or a script to get data via Telnet Commands?

It is actually is online at a remote location.

perhaps PM me the details, that requires you to trust me. I will NOT be offended if you don’t PM me.

Sure, I was actually able to create a vbs script which writes the output to a file, but have no idea how to bring that info into HA.

Edit: Sent

If you have that file visible to HA, (exactly as the output above) then the command is reasonably simple, to get the first line it’s something like

grep READING1 filename |cut -b 17-20

Ok, I’ll have a go at it, were you able to try it out?

I had a short play but then it was bed time :slight_smile:

:+1: no worries

If you can do it in Python, then you can use telnetlib. The following is what I use to login to a router and get some data. By no means saying its well written, but it does work :smiley:

I run it via cron periodically, and the results are output in JSON, which I decode in HA to create sensors. I am sure you could tailor it to achieve what you do above.

    try:
#        print("Opening Telnet connection")
        telnet = telnetlib.Telnet('192.168.xxx.xxx')
#        print("Created new Telnet instance")
        telnet.read_until(b'Username : ', 20)
#        print("Found username")
        telnet.write(('xxxxxx' + '\r\n').encode('ascii'))
#        print("Sending username")
        telnet.read_until(b'Password : ', 20)
#        print("Found password")
        telnet.write(('xxxxxx' + '\r\n').encode('ascii'))
#        print("Sending password")
        telnet.read_until(b'=>', 20)
#        print("Found prompt")
        telnet.write(('hostmgr list\r\n').encode('ascii'))
#        print("Calling hostmgr")
        devices_result = telnet.read_until(b'=>').split(b'\r\n', 60)
#        print("Command complete")
        telnet.write(('exit' + '\r\n').encode('ascii'))
#        print("Sent exit command")
        exit
    except EOFError:
        print("Unexpected response from router")
        return
    except ConnectionRefusedError:
        print("Connection refused by router. Telnet enabled?")
        return
    except:
        print("Error")
        return

Hope that helps get you started.

1 Like

OK so you have a script that gets the text that the telnet generates. Lets call that text file scirpttest.

I assume that there are always the same line lengths, ie the data you want starts at byte 32 and finishes on 35.

grep searches a file and outputs the line that contains the text searched for.

cut finds characters from that line. In this case by bytecount from the start of the line.

grep READING1 scripttest

outputs

  1   READING1                 1578   22.25     0.0   78.5     4422

After cut is applied

grep READING1 scripttest|cut -b 32-35

outputs

1578

So your commandline sensor

sensor:
  - platform: command_line
    command: "grep READING1 scripttest|cut -b 32-35"
    unit_of_measurement: "gal"
    name: Reading1 Sensor
    upload_interval: #this will depend how often you want it to update

Looks promising, I’ll give it a shot, not so great with python or scripting, thanks.

Looks simple enough, but one issue, the script I ran is on a Windows Laptop and not automated, as it’s not always on or in one place, I guess I just need to figure out a python script to run on my Raspberry Pi, which could then be used as advised.

Either a python script or an expect script :slight_smile:

Meanwhile I seem to have broken it, the server is rejecting my connections now :frowning:

(Sorry)

Yeah, happened a few times for me as well while using scheduled task, issue was in windows group policy, just rebooted, should be fine now.

I’m doing something very similar to this, did you ever get this working?

Yes it works.

@nickrout I have stumbled across this post. Your data and connection seems very familiar to a Veeder Root Tank Monitor. I am to working on this. Would you mind sharing any scripts or tutorials?

Thank you,
Alex

They are shared above.

I know this is a lot to ask for but I looking for a step by step tutorial. I am a complete novice with home assistant and im trying to learn. I am getting lost on what the procedure is to actually create this sensor. @nickrout

Thank you for you help!
Alex

This approach doesn’t seem to work from within HA (HomeAssistantOS install) because Python scripts, while exposed as services via the Python Script integration Python Scripts - Home Assistant , run in a sandbox environment where one gets no telnetlib:

NameError: name ‘telnetlib’ is not defined

Code:

command = data.get("command")
logger.info("telnet {} at {}".format(command, time.time()))

try:
    telnet = telnetlib.Telnet('10.X.Y.Z', 23, 90)
    output = telnet.read_until(b'login: ', 20)
    telnet.write(('abcxyz' + '\r\n').encode('ascii'))
    output = telnet.read_until(b'connection established : ', 20)
    telnet.write((':G\r\n').encode('ascii'))
    current_scene = telnet.read_until(b' OK')
    logger.info('Telnet: current scene: ' + current_scene)
    Telnet.close()
except EOFError:
    logger.error('Telnet: unexpected output from server: ' + output)
except Exception as e:
    logger.error('Telnet error :' + e)

Yes
example “date” 192.168.1.4

command_line:
  - sensor:
      name: aqara_test
      command: "apk -q add inetutils-telnet && ( sleep 3; printf 'root\n'; sleep 1; printf 'clear\n'; sleep 1; printf 'date\n'; sleep 1; ) | telnet 192.168.1.4 | sed '1,8d'"
      value_template: "{{ value | replace ('#','') | replace ('~','')  }}"

immagine
immagine