Are you sure this is working now? how can you send the API key via url?
In the meantime I’ve found another way to access their data, using a script to collect the data form their website, and using the http binding and jsonpath from internal openhab web server:
rule openuv
when
Time cron "* 0/15 * * * ?" // this one cycles every 15 minutes.
then
// executing the script and waiting for the JSON return
var String cmd = "sh /etc/openhab2/myscripts/openuv.sh"
executeCommandLine(cmd, 120000)
end
thanks for your input. I modified your rule to fit it to my needs, but for some reason it is causing havoc on my openhab installation (running on 2.3.0~20180324205302-1 (Build #1240)).
When loading this rule during startup, the boot process is stopped.
Any help on why and what to do to fix it? Never had this before
rule "openuv"
when
Time cron "0 0/15 * 1/1 * ? *" // this one cycles every 15 minutes.
then
executeCommandLine("sh /srv/openhab2-conf/scripts/openuv.sh", 10000)
end
rule "UV recalc"
when
Item Openuv_sunburn received update or
Time cron "0 0/16 * 1/1 * ? *"
then
var int hours = ((Openuv_sunburn.state as DecimalType).intValue / 60) - (24 * days)
var int minutes = ((Openuv_sunburn.state as DecimalType).intValue - 60 * (24 * days + hours))
var String result = ""
// STUNDEN
if (hours > 0) {
result = result + hours + " Std. / "
}
// MINUTEN
if (minutes > 0) {
result = result + minutes + " Min."
} Openuv_sunburn_format.postUpdate(result)
logInfo("UV recalc", "Sonnenbrandzeit upgedated auf: " + Openuv_sunburn_format.state)
end
The Shellscript is as below:
#!/bin/sh
curl -X GET 'https://api.openuv.io/api/v1/uv?lat=xx.xx&lng=xx.xx&alt=xx' -H 'x-access-token: mytoken' > /srv/openhab2-conf/html/openuv.txt
The items are defined the following way:
String Openuv_UVIndex "UV Index (Jetzt): [%s]" <uvsun> (Wetter) { http="<[http://192.x.xx.x:8080/static/openuv.txt:60000:JSONPATH($.result.uv)]" }
String Openuv_UVIndexMax "UV Index (max): [%s]" <uvsun> (Wetter) { http="<[http://192.x.xx.x:8080/static/openuv.txt:60000:JSONPATH($.result.uv_max)]" }
String Openuv_Ozone "Ozone Index (Jetzt): [%s]" <ozon> (Wetter) { http="<[http://192.x.xx.x:8080/static/openuv.txt:60000:JSONPATH($.result.ozone)]" }
Number Openuv_sunburn "Sonnenbrand in Minuten:[%s] " <sunburn> (Wetter) { http="<[http://192.x.xx.x:8080/static/openuv.txt:60000:JSONPATH($.result.safe_exposure_time.st3)]" }
Number Openuv_sunburn_format "Sonnenbrand in: [%s] %unit%" <sunburn> (Wetter)
Whenever I exclude the rule (rename the rules file, so it won’t get loaded) the OpenHAB service is starting up as normal.
i find this very interesting - as my whole Family is outside quite often for biking, running or whatever!
I just did @rtvb already suggested an simple http binding with the additional header. With that you won’t need all that external script stuff… the API gets updated every 3-4 hours, as I read. you have something like 500 API-calls per day, so I figured and update every hour is enough. 1 hour in msec is 3600000.
insert a cached URL in http.cfg
# configuration of the openuv.io cache item
openUV.url= https://api.openuv.io/api/v1/uv?lat=<<LAT>>&lng=<<LNG>>{x-access-token=<<API-KEY>>}
openUV.updateInterval=3600000
please replac LAT und LNG with your Location and replace the API-Key
with that cached item (it’s a string in JSON format), you can then easily update your items:
Number openUV_UV "aktueller UV-Wert [%d]" { http="<[openUV:60000:JSONPATH($.result.uv)]" }
DateTime openUV_UV_Time "UV-Wert Messung [%1$tH:%1$tM]" { http="<[openUV:60000:JSONPATH($.result.uv_time)]" }
Number openUV_UV_Max "Maximaler UV-Wert [%d]" { http="<[openUV:60000:JSONPATH($.result.uv_max)]" }
DateTime openUV_UV_MaxTime "Max. UV-Wert Zeit [%1$tH:%1$tM]" { http="<[openUV:60000:JSONPATH($.result.uv_max_time)]" }
Number openUV_Ozon "Ozonwert [%d]" { http="<[openUV:60000:JSONPATH($.result.ozone)]" }
Number openUV_st1 "maximale zeit Hauttyp 1 [%d min]" { http="<[openUV:60000:JSONPATH($.result.safe_exposure_time.st1)]" }
Number openUV_st2 "maximale zeit Hauttyp 3 [%d min]" { http="<[openUV:60000:JSONPATH($.result.safe_exposure_time.st2)]" }
Number openUV_st3 "maximale zeit Hauttyp 4 [%d min]" { http="<[openUV:60000:JSONPATH($.result.safe_exposure_time.st3)]" }
Number openUV_st4 "maximale zeit Hauttyp 5 [%d min]" { http="<[openUV:60000:JSONPATH($.result.safe_exposure_time.st4)]" }
Number openUV_st5 "maximale zeit Hauttyp 6 [%d min]" { http="<[openUV:60000:JSONPATH($.result.safe_exposure_time.st5)]" }
Number openUV_st6 "maximale zeit Hauttyp 7 [%d min]" { http="<[openUV:60000:JSONPATH($.result.safe_exposure_time.st6)]" }
That’s it! of Course you can now add some additional items, which calculate the risk (e.g. for high Ozone-values or high UV-values!) and so on. But getting the raw items is easy as that! even DateTime works easily.
aha, interesting. It’s owned by openhabian:openhabian (obviously, this is what I’m running here ). I’ve now made it openhab:openhabian. Thanks for the hint.
Any idea why openhab would completely freeze at startup due to it?
I am trying to get this to work, and I keep getting the error:
2018-04-05 23:03:33.412 [ERROR] [org.openhab.io.net.http.HttpUtil ] - Fatal transport error: javax.net.ssl.SSLHandshakeException: sun.security.validator.$
2018-04-05 23:03:33.416 [ERROR] [ab.binding.http.internal.HttpBinding] - No response received from ‘openUV’
I am using openhab 2.2 on a Raspberry pi 3 - installed with apt-get. When I type the curl command noted above the system returns the expected data, so I think the api key is correct.
A little suggestion and caveat regarding the DateTime items (if look closely in @rtvb’s example, you can see, that he solved this also). The API gives back the time in UTC, so in order to match the Datetime items to your timezone, you could change the code like this:
rule "UV Index DateTimeItem"
when
Item openUV_UV_Time changed or
Item openUV_UV_MaxTime changed
then
val DateTime uv_time = new DateTime(openUV_UV_Time.state.toString)
val DateTime uv_time_max = new DateTime(openUV_UV_MaxTime.state.toString)
openUV_UV_Time_MEZ.postUpdate(uv_time.toString)
openUV_UV_MaxTime_MEZ.postUpdate(uv_time_max.toString)
end
I use only open_UV_Time_MEZ in my sitemap. As you can see, there is already an transformation in the item definition ([%1$tH:%1$tM]), which results in e.g. 12:13.
Have a look in the docs, there you can find all possible formatters for Date/Time Conversions.