core-ssh:~# curl --url http://192.168.0.96:21080
curl: (7) Failed to connect to 192.168.0.96 port 21080: Connection refused
Basically I need the syntax that checks if the curl response says Connection refused then make the switch off, on otherwise.
No idea how to formulate the expression to give the values above however
Yes a ping doesn’t work because it might ping ok but refuse the connection on that port.
No other commands needed, not sure how often it updates either, can I set the refresh value to something like every 5 minutes?
I used a port that I know to be not in use as an example above
core-ssh:~# curl -o /dev/null -w "%{http_code}\n" http://192.168.0.96:21080
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
curl: (7) Failed to connect to 192.168.0.96 port 21080: Connection refused
000
Yes I will name that correctly once I finalize but I am getting this (a 200 message) when it does work
core-ssh:~# curl -o /dev/null -w "%{http_code}\n" http://192.168.0.96:21081
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1902 0 1902 0 0 8025 0 --:--:-- --:--:-- --:--:-- 8025
200
Please add the -qsI options as well, so you can get rid of the progress part.
Please also note that you have a lot of HTTP codes, 200 is only one of them. So you’ll have to parse the returned value and set the payload value, something like:
I’m not sure whether the response is a string or an int, from the response 000, I assume it’s a string, however if this doesn’t work you may need to remove the quotes around 200 and change the payload_off to 0.
‘payload_on’ works fine when set to “200”. However, I cannot get proper ‘disconnected’ status, as ‘payload_off’ never works properly with both “000” and 0 (integer) as value - status is either Connected or Unknown (never ‘Disconnected’).
Can anyone help here? Looks like some stupid mistake from my side, but it does not work properly.
Fixed by clearing curl exit code. HASS is unable to properly handle non-zero output from scripts, and reporting this to the log file as an error (leaving actual sensor value unavailable). Also, curl timeout is good to have.