Generate custom URL based on weather CommonID

I’m trying to generate a URL based on the CommonID from the weather binding…

This is all I have in my rule file whenever the CommonID is changed.

postUpdate(WTH_CommonIdImageURL, “” + WTH_CommonId + “.jpg”)

But this updates the WTH_CommonIdImageURL with the following text… (Type=StringItem, State=Sunny).jpg

Can anyone help me… I just want to grab the “Sunny” text and use it in my URL, not all the other stuff…

WTH_CommonId -> ("" + WTH_CommonId.state)

1 Like

Thanks @martin_klimke that worked a treat…

I had tried the code below but that hadn’t worked, your suggestion worked perfectly.

postUpdate(WTH_CommonIdImageURL, “” + WTH_CommonId.state + “.jpg”)

This is what works…

postUpdate(WTH_CommonIdImageURL, “” + ("" + WTH_CommonId.state) + “.jpg”)

Another possible solution:

postUpdate(WTH_CommonIdImageURL, "" + WTH_CommonId.state.toString + ".jpg")

I never understood those class definition.
So using String
"WTH_CommonId.state.toString" is working nicely.
but if the item is a Number, then

var Number test
test = test_number_item.state.toNumber

is failing.

so I have to use the
test = test_number_item.state as DecimalType .

Also"test = test_dataetime_item.state.DateTime" is failing so I have to use some other construct I cannot remember right now.

All those type conversion are not following a similar pattern.
I never understood why this makes sense.
Do know about a deeper logic why this is so convoluted or is this just “historical” reasons and it was never properly fixed?

The reason .toString works is because the rule language is built on top of Java, and every object in Java has a .toString() method. (In the rule language, you can drop empty parentheses.) There aren’t equivalent methods like .toNumber(). In openHAB, every different kind of item state is of some XXXType class. If you know an item’s state is a number, then it must be of the DecimalType class, but the rule language needs to be told you want to treat it like a number by saying something like var Number myNumber = myItem.state as DecimalType.

You could have

var DateTimeType myDateTime =  test_dataetime_item.state as DateTimeType
var Calendar cal = myDateTime.calendar

but it would help to know the Java Calendar class.

It would be very helpful to have more documentation about these things. :wink:

Thanks for the answer.