(solved) JSON controlled case switch

Hello Guys,
I want to set my ambient lighting with a weather forecast response with the Yahoo YQI.
Therefore im trying to get a JSON response from the Yahoo weather server using this poll request:


Using JSONPATH I can isolate a single number (forecast code for tomorrow) : $..forecast[1].code
This is the weather code reference
Installed the HTTP binding and the JSONPATH conversion. I am trying to put my Number in an item directly with:

Number FC_Code “ForeCast Code” [ “CurrentTemperature” ] {http="<[http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D"orsbach%2Cgermany")&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys:6000:JSONPATH($…forecast[1].code)]"}

This does not work and I can’t figure out why. I also tried to pull the data in a rule like this:

import org.openhab.core.library.types.*
import org.opemhab.core.types.*
import org.openhab.model.script.actions.*

rule "Wettersauele"
    Item Wettervorhersage changed from OFF to ON
    //Time cron "0 0 16 1/1 * ? *"    

    logInfo("wetter","Binding Running")

var String json = sendHttpGetRequest("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22orsbach%2Cgermany%22)&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys")

var String value = transform("JSONPATH", "$..forecast[1].code", json)

But that does not work aswell, Giving me the Error “Error while executing script” … “index=1,size=1”.

Hm… anyone of you guys got an idea how to fix this error?

No answer from me, just a question.
Why are you trying to isolate a value from the weather report by yourself? There is a weather binding which does that for you.

I tried to figure out how to get a condition forecast but afaik this is not possible with these.
Yahoo gives me 49 different condition forecasts for the next 7 days and they are there, but I can’t grab them :confused:
Similar actions have been done in openhab several times but they are not working for me…

The tree address for the code information is: query.results.channel.item.forecast[x].code Are you sure you can concatenate your request to only $..forecast[1].code ?

yes, both giving me the result


with this online JSONPath emulator. http://jsonpath.com

Are you looking for the weather condition converted to a number?
That would be the “id” in the weather binding!
You just need the correct woeid for your place.




String TestWeather "Code [%s]" {weather="locationId=Test, forecast=1, type=condition, property=id"}


Text item=TestWeather

Log entry:

17:20:28.087 [INFO ] [marthome.event.ItemStateChangedEvent] - TestWeather changed from UNDEF to 30

Note: 30 is the code for partly-cloudy!

1 Like

Well I did move my openHab server yesterday and I somehow managed to get it to work.
Sadly though I read your reply afterwards, but I will stick to my solution because it costed lots of effort - thanks anyway !!

rule “Wettersauele”


	Time cron  	"0 0 17 1/1 * ? *" and

Item Wettervorhersage is ON or
Item dummy1234 changed from OFF to ON and
Item Wettervorhersage is ON

logInfo(“weather”,“Binding Running”)

var json = sendHttpGetRequest(“http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D"orsbach%2Cgermany")&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys”)

logInfo(“weather”,"JSON Request: " + json)

var value = transform(“JSONPATH”,"$…forecast[1].code",json)

logInfo(“weather”,"PATH Response: " + value)

var stringcut = value.replaceAll("\p{P}","")
logInfo(“weather”, stringcut)
var wcode = Integer::parseInt(stringcut)



1 Like

If it works for you, everything is fine. Thanks for the feedback.

P.S.:In case your problem is solved you can either edit the thread title to show something like: “[solved] …” or use the “This reply solves the problem” button in one of the replys( your own) to show that in the initial post.

1 Like