Making binary_sensor to report API Alive?

I have device that uses API to report its status, when it goes offline, RESTsensor reports error, and to trying to make my HA error free, i have routed this offline state to file in my HA, this offline file works great, but problem is when going online again…

My device takes around 5min to report first JSON message, for this time my RESTsensor reports error and i want to get rid of this error.

To switch between online and offline address. I use resource_template in RESTsensor.
I have tried build in PING sensor, but this does not work for some reason for this specified address, i can get PING from device before it sends first JSON message but cant get PING from that “website” where JSON message is generated.

Currently i have Command Line binary_sensor and this works only some times, most of time this gives timeout error when device is online.

My sensor is

  - binary_sensor:
        scan_interval: 40
        command: 'out=`curl -o /dev/null --connect-timeout 2 -w "%{http_code}\n" http://192.168.0.46/cgi-bin/api.py` && echo $out || echo 000'
        name: "192.168.0.46"
        unique_id: "unique_id_192-168-0-46"
        device_class: connectivity
        payload_on: "200"
        payload_off: "000"

And error is

Timeout for command: out=`curl -o /dev/null --connect-timeout 2 -w "%{http_code}\n" http://192.168.0.46/cgi-bin/api.py` && echo $out || echo 000

Is my command wrong? I have copy-pasted that code from here forum and edited, i dont know much about command line comands currently.
Or should i use some other sensor/method

I think i found working solution, it was my device API that is kind of slow to respond and HA didnt want to wait that long, so needed to add some timeout commands.

  - binary_sensor:
        scan_interval: 40
        command: 'curl -4 -s -f --connect-timeout 28 -m 32 http://192.168.0.46/cgi-bin/api.py > /dev/null && echo "ON" || echo "OFF"'
        command_timeout: 35
        name: "192.168.0.46"
        unique_id: "unique_id_192-168-0-46"
        device_class: connectivity

So now sensor needs to wait 35s before timeout and command needs to wait 32s before timeout, also API webpage waits 28s before timeout, this way there is no timeout error anymore, hope this servers someone else also.
Used Google AI to explain and build curl commands