Http request in .item file only if device is reachable

hello all,

  • Platform information:
    • Hardware: raspberry pi 3 b
    • OS: Raspbian GNU/Linux 9 (stretch)
    • Java Runtime Environment: Java™ SE Runtime Environment (build 1.8.0_181-b13)
    • openHAB version: 2.3.0 (Build)

My problem is the following: I would like to read information via http requests from my dreambox (VU + solo 4k). If the dreambox is turned off, however, then there are a lot of error messages in openhab.log. Is it possible to send the http request depending on the availability of the device?

here are my files:

.item:

Group gVUsolo4k
String  VUsolo4k_Sendung  "Aktuelle Sendung [%s]"  <tv>   ( gVUsolo4k ) { http="<[http://192.168.137.47:80/web/getcurrent:3000:REGEX(.*?<e2eventname>(.*?)</e2eventname>.*)]" }
String  VUsolo4k_Kanal    "Sender [%s]"            <tv>   ( gVUsolo4k ) { http="<[http://192.168.137.47:80/web/getcurrent:3000:REGEX(.*?<e2servicename>(.*?)</e2servicename>.*)]" }
Switch  VUsolo4k_sendMute "Mute"       <soundvolume_mute> ( gVUsolo4k )

These messages appear approximately every 3 seconds:

2018-07-06 15:58:57.642 [ERROR] [ab.binding.http.internal.HttpBinding] - No response received from 'http://192.168.137.47:80/web/getcurrent'
2018-07-06 15:59:00.759 [ERROR] [org.openhab.io.net.http.HttpUtil    ] - Fatal transport error: java.net.NoRouteToHostException: Keine Route zum Zielrechner (Host unreachable)

Many many thanks!

Yes but not using the HTTP binding like this.

You will need:

A Network binding linked Switch Item that pings the device that represents whether the device is online or not.

A Rule that triggers every three seconds that uses sendHttpGetRequest and parses out the result, but only if the online Switch is ON.

rule "TV"
when
    Time cron "*/3 * * * * * " // double check the cron expression, I don't know if this is right
then 
    if(TV_Online.state != ON) return;

    val results = sendHttpGetRequest("http://192.168.137.47:80/web/getcurrent")
    VUsolo4k_Sendung.sendCommand(transform("REGEX", ".*?<e2eventname>(.*?)</e2eventname>.*", results))
    VUsolo4k_Kanal.sendCommand(transform("REGEX", ".*?<e2servicename>(.*?)</e2servicename>.*", results))
end
3 Likes

Great, thank you, that’s the solution to my problem!
I had to make a small change regarding cron:

rule "VUsolo4k_Infos"
when
    Time cron "*/3 * * * * ?" 
then 
    if(VUsolo4k_online.state != ON) return;

    val results = sendHttpGetRequest("http://192.168.137.47:80/web/getcurrent")
    VUsolo4k_Sendung.sendCommand(transform("REGEX", ".*?<e2eventname>(.*?)</e2eventname>.*", results))
    VUsolo4k_Kanal.sendCommand(transform("REGEX", ".*?<e2servicename>(.*?)</e2servicename>.*", results))
end