I think the key thing is the split command doesn’t actually change the String. It returns an array with copies of the String split on the given character. So the command
val weather0 = BBCWeatherRSS_LatestTitle.state.toString.split(',').get(0)
and you never use weather0 is a noop (does nothing). It doesn’t change BBCWeatherRSS_LatestTitle.state.toString
for the succeeding lines so there is no reason for it to exist.
Which you stated and it appears to be born out in the code you don’t actually use. So why split it out in the first place?
Right, but I think the part you missed is that the weather1 line is operating on the entire text, not just on what is in weather0. Had you written it as:
val weather1 = weather0.split(":").get(1)
then weather0 would be a useful line. But as you should see, you don’t need that first line because the second Item in the list when you split the whole text on : will be the part you want anyway. So you can skip it.
My code should be (unless I have a typo) functionally equivalent to what your original code does. The important things to realize are:
- calling split does not change the original text; each one of your weatherX lines is operating on the full text from the RSS feed.
- split returns a list of the parts of the string split on the occurrences of ‘:’ in this case, excluding the ‘:’. So if you have the string
foo:bar:baz
, calling split(‘:’).get(0) will befoo
and split(‘:’).get(2) will bebaz
. You are getting the Nth part of the String after the split. As with most programming, we start with 0 instead of 1
So just split the String once and save the list to a variable (parts). Then pull out the parts that you want and save those to their own variables.
You can’t use an aggregation function with String Items (what’s the AVG of a bunch of Strings?). Maybe you tried to do that?
This should be possible. I’d have to see what you tried.
In general, editing data (I’m calling Items data here for sake of argument) is safer than editing code. This is why I always fall on the side of making the changes in .items files and sometimes .map files and the like before I’d write a Rule that I would expect to need to edit in the future. It is easier to accidentally introduce subtle errors in code. If you introduce an error in a .items file, it will usually break the whole file and you immediately know.
Another good reason. Other approaches could be using an enable/disable proxy Item for each to turn it on or off. But Group membership would be much easier.
DOH! Dumb mistake. Get rid of the .get(1)
at the end. It shouldn’t be there.
if(parts.size >= 4) parts.get(3)
So using the trinary operator (one line if statement)
val weather3 = if(parts.size >= 4) parts.toString.get(3) else ""
The size of the parts array tells you how many segments the String was split into.
You can do that too. Split on “,” and get(1). The use replace(“Minimum”, “Min”) and replace(“Maximum”, “Max”) and replace(“Temperature”, “”).
You can even do it in one line.
message = BBCWeatherRSS_LatestTitle.state.toString.split(',').get(1).replace("Minimum", "Min").replace("Maximum", "Max").replace("Temperature", "")