Template Example: Weather Binding

Tags: #<Tag:0x00007f18277906d0>

(Alexander) #84

Hello Andy,

thanks for that work, i like your template very much! I hope you dont mind taking your template and adapt it to a responsive-friendly widget by me.

Wetter-Widget.widget.json (2.6 KB)

(Nic P) #85

Hi Andrew, it would be great if you could share this as I was thinking of creating something similar but have never got around to it.

(Nathan Wilcox) #86

Hi All,

What do I need to add to the “.replace” coding below in order to fix the fact I have two spaces in a condition type ( btw I’ve changed the original icon “scattered showers” to be “chance-of-rain”)

Returned Condition Type:
Chance of Rain

Code Snippet:

		<td rowspan="2"><img id="weather-icon" src="{{ServerPath}}/images/{{IconSet}}/{{itemValue(config.Condition0).replace(' ','-') | lowercase }}.png"/></td>


(Andy) #87

Hi @Nathan_Wilcox,

I’m not sure I really understand your problem, but assuming you get something like “Chance[space][space]of[space]rain” then by far the easiest way would be to rename the associated icon file to have a double hyphen where the double space occurs i.e. chance–of-rain.png.

I guess you could try the line of code:

<img id="weather-icon" src="{{ServerPath}}/images/{{IconSet}}/{{itemValue('Condition_ID1').replace('  ',' ').replace(' ','-') | lowercase }}.png"/>

where the first .replace changes two spaces to one, but I have no idea if this will work and no quick way to test this for you. If you do try the line of code and it works, let the community know as it may help others.

Sorry if this is not the problem that you are trying to solve, if you need to can you provide the source you are getting and the output you need as this may make it a bit clearer to me.


(Nathan Wilcox) #88

Hi Andy,

The problem I am having is that my Forecast condition of tomorrow is telling me “Chance of Rain” When the .replace is kicking in it is changing the condition to show the following:

And when opening the image in a new tab:


Problem accessing /static/images/colorful/chance-of%20rain.png. Reason:

    Not Found3

Therefore it is not finding an icon called


If this now makes sense?


(Nduduzo Ngcobo) #89


I’m new to openHab and Linux as a Whole.

I’ve managed to write the HTML files to the custom Widget Code Section,But my Putting the directory on the server, I’m not sure how that Goes

I’m using Ubuntu: 16.04
And the Server is hosted on the same PC

Could you please help me on how to Upload the Pics on the Server…


(Andy) #90

Hi @Nathan_Wilcox,

OK, I can see the problem now. I think there are two potential causes:

  1. A double space between of and rain, as you suggested. In this case try:
<img id="weather-icon" src="{{ServerPath}}/images/{{IconSet}}/{{itemValue('Condition_ID1').replace('  ',' ').replace(' ','-') | lowercase }}.png"/>
  1. They have used an inconsistent approach to adding spaces to the supplied condition. First they used a simple space (which was then replaced with a hyphen by the code), but the second space is encoded as %20. So we need a way to replace the %20 in the source.
<img id="weather-icon" src="{{ServerPath}}/images/{{IconSet}}/{{itemValue('Condition_ID1').replace('%20','-').replace(' ','-') | lowercase }}.png"/>

I can’t be certain which is the problem as most browsers with remove double spaces when displaying text and they are hard to spot with a standard (non-fixed width font).

Let me know how you get on. Andy

(Andy) #91

Hi @Nduzi,

Check the documentation at: http://docs.openhab.org/installation/linux.html#file-locations.

The icons go in the Site configuration folder, html subdirectory. So for a repository installation that would be /etc/openhab2/html/.

If you have implemented the SAMBA shares as http://docs.openhab.org/installation/linux.html#network-sharing this is shared as \\your IP address\openHAB-conf. Again the subdirectory html is the place for the downloaded files. All my scripts and instructions are based upon deployment from a windows PC with a mapped drive in place, so if you have access to that technology it may be the easiest way to get going.

Hope this helps. Andy

(Booster B (Booster)) #92

my settings
Number Precip_Probability “Precip probability [%d %%]” {weather=“locationId=lublinFIO, type=precipitation, property=probability”}

{{itemValue(‘Precip_Probability’)}} %

I have Yahoo and ForecastIO, and see only 0 :frowning: where in sites yahoo and forecast this values is different
others valueas like temp, wind is works.

(Nic P) #93

Hi Nathan,

I had a similar problem and found that .replace will only replace the first instance in a string, so in your string the first space is being changed to - but the second space is not.

I found some resources that referred to using regex to replace all instances in a string but in the end I found it was easier to change the image reference to look at “commonid” and rename my images based on this

(Andy) #94

Hi @Nic_P / @Nathan_Wilcox,

Nic’s comments have made me look at this in more detail (for about two hours :persevere:)… looks like I am beaten.

Nic is correct that the replace command only replaces the first instance of the space. In my defence I’ve used loads of programming languages on Windows and none of them work in this way :frowning:… ok I made a bad assumption…

Looks like you should be able to use a regular expression instead .replace(/\s/g, '') but that does not seem to be supported within the HTML. Probably the various slashes screws things up…

So went looking for other mechanisms, looks like a custom filter would do(lowercase is an example of a built-in filter). I’ve had a go at this, but it looks like I would need a script in the <HEAD> section of the HTML to make this work. Our custom templates are in the <BODY> section of the HTML and so I can’t work out how to do it this either…

Perhaps @ysc may be able to suggest the right way to replace multiple spaces. I’m sure that there is a knack to make this happen, but right now my Google Foo is not getting me there…

For now Nic is right, switching to the Condition_ID0 method and renaming the icon files as I discussed in this post Template Example: Weather Binding is the only workaround I can offer.

Grrrr… I hate being beaten by computers!


(Yannick Schaus) #95

The right way would be a filter in a separate Javascript file, loaded with oc-lazy-load (check other threads for examples) as you suggest @AndyMB.

Now for a dirtier way… well, here you go: 'chance of rain'.split(' ').join('-')

(Andy) #96

Thanks @ysc,

I knew you would have the (two!) answer! I’ve tested the ‘dirty’ way with the line:

<img id="weather-icon" src="{{ServerPath}}/images/{{IconSet}}/{{itemValue('Condition1').split(' ').join('-')  | lowercase }}.png"/>

tested with a manually forced status and this works perfectly to solve @Nathan_Wilcox Nahan’s bug.

I’ve taken a quick delve into the ‘right’ way to, but I’m going to need a bit more time to get my head around that. But I do think this will offer some better ways to deal with some of the workarounds that have been added over time that have bloated the HTML.

I can only apologise that I did not keep up-to speed with this addition functionality, delving into custom widgets has been on my to-do list for too long!

Thanks for your help solving this one.


(Nathan Wilcox) #97

@Nic_P @AndyMB @ysc

Than you all for your assistance on this. I have now implemented the new ‘dirty’ line and will await the result, shouldn’t be long as there’s always “Chance of Rain” where I live :joy:

(Nathan Wilcox) #98

@AndyMB @ysc

So I thought I had implemented the change but now I’m thinking I’ve done something incorrect as I still Have the problem:


	<img src="{{ServerPath}}/images/{{IconSet}}/{{itemValue(config.Condition2).split(' ').join('-')  | lowercase }}.png"/>

Can you help please?

(Andy) #99

Hi @Nathan_Wilcox,

Can you repeat your trick of opening the broken image URL in a new tab and posting the URL. Without some idea of what you are getting as the URL now it’s difficult to guess what is going on here.

From my testing I know that chance of rain is successfully mapped to `chance-of-rain’. So I can only conclude that there is some weird character being used to generate the second space / supplied by the weather API. At a guess, it could be a double space being turned into a double hyphen or the inconsistent use of %20 in the condition being returned by the weather binding.

Can you also double check and confirm:

  • you have changed all four of the image URLs to use the .split(' ').join('-') method.
  • you have an icon of the name chance-of-rain in the correct icon folder (looks like you are using flat_colorful).

I’m sure you have done the above, but worth double checking the obvious.

You may also like to use the ability to manually set the status of an item in the console. The command for the condition would be:

smarthome:update Condition0 "Chance of Rain"

I would manually set this for each of the conditions 0,1 and 2 using a SINGLE space between each word. If this all works it provides some more evidence that there is something strange in the condition data being provided by the binding (or more accurately the provider of the data).

If you see inconsistent display with all conditions manually set that would point to one of the image URLs not being right in the HTML.

Sorry but step-by-step, systematic diagnosis looks like the only way that we will get to the bottom of this one.

Regards, Andy

PS: you could always ‘give up’ on the 'named icon approach and change to the conditionID / number format for the icons. IMHO this is much more easier to maintain over time and allows you the option to switch providers without too much hassle should you ever need.

(Jay) #100


I just gave this widget a try as it looks really good and exactly what I wanted. I have the same problem as some other people seem to be having, but haven’t had a chance yet to read through all the comments.

Basically, the conditions don’t match the icon names so no icons display. Does this only work with certain providers? If so, it might be an idea to update your first post with that. I just tried with OpenWeatherMap & ForecastIo.


(Matt) #101

Hi Jay,

no its working with all providers. i actually had the same problem in the beginning.
the easiest way i thing is to copy/rename the existing icons to match the once you need.


(Ste Phan Mckillen) #102


Just what the doctor ordered

(Nduduzo Ngcobo) #103

Can I Please ask: Where do I place the Icons in Ubuntu(Local Host) and how do I specify the directly in the HTML directory?