Status setupbox based on port status (with netcat)

Hey guys,

I have been tinkering a while with hass and I am trying to find a solution to detect the status of my setupbox through netcat and the commandline sensor. So when I SSH into my pi (with HASSbian) I can run the following code with succes:
nc -zv [ip adress] [port] &> /dev/null; echo $?
When the setupbox is in standby it returns 1 and when it’s on it returns 0, all working like it should!

Now when I use this command in a commandline sensor, it always returns the value 0… I have tried several other parameters with the netcat command and value templating, but I can’t get it to work.

Commandline sensor:
`#Will output 0 if port x is open, and 1 if it’s closed.

  • platform: command_line
    name: Horizonbox netcat echo
    command: nc -zv 62137 &> /dev/null; echo $?
    scan_interval: 10`

Do you guys have any idea what is going wrong? If there is a better solution to this problem than with netcat, I would love to hear that too!

Best regards,


Hi @Cripplin, a command in a terminal is not the same in ha.
Take a look here, maybe something helps.

Hi @VDRainer,

Thx for the quick reply! I already figured that the problem I am having could be related to the virtual environment of hassbian, so I changed to the hassbian user and activated the virtual environment and ran the code. The command returns the same results as the root Pi user, so 0 if the port is open and 1 if the port is closed…

TL;DR: it should not be a virtual environment issue…

Thanks anyway!

I have a switch where the state works nice with netcat.
command_state: "nc -z -w 5 9295"
Netcat already returns the exit status, so you do not need the echo.
Maybe this works for you.