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.