Error with a command_line binary_sensor

Hi everyone,

I’m trying to manage a binary sensor to check if a port is open on a remote device
I did

binary_sensor:
  - platform: command_line
    name: ip_cam_alive
    command: "/bin/nc -z 192.168.1.54 8080"
    payload_on: 0
    payload_off: 1
    scan_interval: 30

The return value of the nc command is 0 when successful and 1 when not.
But I get an error message

17-04-25 11:32:05 ERROR (MainThread) [aiohttp.server] Unhandled exception
Traceback (most recent call last):
File “/home/homeassistant/.homeassistant/deps/aiohttp/web_protocol.py”, line 430, in start
yield from resp.write_eof()
File “/home/homeassistant/.homeassistant/deps/aiohttp/web_response.py”, line 574, in write_eof
yield from super().write_eof(body)
File “/home/homeassistant/.homeassistant/deps/aiohttp/web_response.py”, line 413, in write_eof
yield from self._payload_writer.write_eof(data)
File “/home/homeassistant/.homeassistant/deps/aiohttp/http_writer.py”, line 281, in write_eof
yield from self.drain(True)
File “/home/homeassistant/.homeassistant/deps/aiohttp/http_writer.py”, line 294, in drain
yield from self._stream.drain()
File “/home/homeassistant/.homeassistant/deps/aiohttp/http_writer.py”, line 127, in drain
yield from self._protocol._drain_helper()
File “/usr/lib/python3.4/asyncio/streams.py”, line 197, in _drain_helper
yield from waiter
File “/usr/lib/python3.4/asyncio/futures.py”, line 388, in iter
yield self # This tells Task to wait for completion.
concurrent.futures._base.CancelledError

If I try directly with the homeassistant user

homeassistant@oahu:~ $ /bin/nc -z 192.168.1.54 8080
homeassistant@oahu:~ $ echo $?
0
homeassistant@oahu:~ $

I don’t understand what I’ve done wrong :confused:

OK… that was stupid…
I manage to do it :slight_smile:
Just add echo $? after and it’s OK

binary_sensor:

  • platform: command_line
    name: ip_cam_alive
    command: “/bin/nc -z 192.168.1.54 8081 && echo $?”
    payload_on: 0
    payload_off: 1
    scan_interval: 30

Glad that you got it to work, but you should probably change that line from using && to ;

 command: "/bin/nc -z 192.168.1.54 8081 ; echo $?"

Otherwise “echo $?” will only run on success (0).

You’re right
I’ve seen this just after ^^