[SOLVED] Any hint how to disable http Items, when http fails or catch the error without any log entry?

  • OH 2.5.8
  • Openhabian

With this item I check if a HUE lamp is available. This works great.
.item

String Light1StateOn "Lampe 1 - OH Schalter [MAP(huebridge.map):%s]" { http="<[http://192.168.x.xxx/api/<api-id>/lights/1:60000:JSONPATH($.state.on)]"}

But if this IP (the HUE Bridge) is not available, there are permanent error messages in the log.

log

2020-08-29 15:47:09.665 [ERROR] [org.openhab.io.net.http.HttpUtil    ] - Fatal transport error: java.net.NoRouteToHostException: Keine Route zum Zielrechner (Host unreachable)

Any hint how to disable this Item, when http fails or catch the error without any log entry?

Thanks!
Thomas

Not sure about your actual question, but as always, I think there is another way.

You could check the status of the Things.
https://www.openhab.org/docs/configuration/rules-dsl.html#thing-based-triggers

I think this would alleviate your issue.

1 Like

Yes, this looks good. It a complete different idea. Ok, I will check!

But you are HTTP version 1.x binding - there are no Things to look at.

You could manage this in rules using HTTP Action. Your rules can detect an error and decide not to poll again for a while.

1 Like

I rebuild everything into a rule and now i get the following questions:

a) What is the return value if the sendHttpGetRequest() fails and how could I check it. The return type is String. Logfile show me - null if http failed

2020-08-30 10:13:15.253 [ERROR] [.smarthome.model.script.actions.HTTP] - Fatal transport error: java.util.concurrent.ExecutionException: java.net.SocketTimeoutException: Connect Timeout
2020-08-30 10:13:15.257 [INFO ] [se.smarthome.model.script.Light1JSON] - null

.rules

[...]
val Light1JSON = sendHttpGetRequest("http://192.168.x.xxx/api/<api-id>/lights/1", 60000)
logInfo("Light1JSON", Light1JSON)
if ( Light1JSON === null ) {
} else {
 
}
[...]

log:

2020-08-30 10:36:00.033 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule 'Hue Bridge rule: Check state.on and state.reachable every minute (hue-bridge-changed.rules)': An error occurred during the script execution: index=1, size=1

b) Why can’t I use headers?

val headers = newHashMap("Cache-control" -> "no-cache")
val Light1JSON = sendHttpGetRequest("http://192.168.x.xxx/api/<api-id>/lights/1", headers, 60000)

log:

2020-08-30 10:00:00.012 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule 'Hue Bridge rule: Check state.on and state.reachable every minute (hue-bridge-changed.rules)': An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.actions.HTTP.sendHttpGetRequest(java.lang.String,int) on instance: null
2020-08-30 10:00:35.110 [INFO ] [el.core.internal.ModelRepositoryImpl] - Validation issues found in configuration model 'hue-bridge-changed.rules', using it anyway:
    // The value of the local variable headers is not used

As you’ve already discovered, the return is null.
That’s not a string though, it is just null.

So this will fail with the unhelpful mesage
An error occurred during the script execution: index=1, size=1
when Light1JSON is null, because logInfo() needs two strings.

You just need to rejig your if()

val Light1JSON = sendHttpGetRequest( ...)
if ( Light1JSON === null ) {
   logInfo("Light1JSON", "http response was null")
   ...
} else {
   logInfo("Light1JSON", "http response was " + Light1JSON)
  ... 
}

Headers are a recent addition to HTTP Actions.
I have an idea these are core Actions, and the core has been frozen at 2.5.0, so you won’t get headers until OH3

1 Like

exactly the right solution for me!
Many Thanks

Regards,
Thomas