Hey Home Assistant Community,
Quick background to what I’m trying to do:
I have a managed network switch that allows SSH connections and provides a limited shell with commands to monitor the device. Unfortunately it is only possible to retrieve some data this way, which is not possible to fetch over SNMP (like PoE power consumption per port, in this case).
So I wrote a bash script to connect to the switch via SSH, run the command to print the data on the switch and parse it to JSON on a 2 second interval.
The script opens the SSH session once, then runs the command on the switch repeatedly without closing the SSH session (I use a expect
script to do this). I do it this way instead of re-connecting on every interval, because when connecting to the switch you can notice that it takes 1 to 3 seconds to even authenticate via SSH, so I keep the session open and just run the command repeatedly on the switch once logged in.
This is where we get to the issue: I want to create a command_line
sensor from the data retrieved by my script (JSON array with information about each 8 ports). Looking at the documentation for command_line
, I see that Home Assistant has it’s own interval to run the script, but this is exactly what I’m trying to avoid. I basically just want to execute my script once which should keep running until HA stops, then “tail” it’s output and load it as sensors for each of the 8 ethernet ports.
How I understand it is, that using command_line
it’s not possible to constantly tail the output of a running script and avoid it’s own interval (correct me if I’m wrong, because this would be optimal).
So my other idea would be to create some kind of automation that runs a shell_script
on HA startup, then modify my script to output the data to something like /tmp/switch-data.json
and using command_line
and it’s interval option to just read this file every 2 seconds using HA’s own interval and parse it as sensors.
Am I on the right path? Is there a more optimal solution to this problem?
Thank you in advance!