OpenWeatherMap Use Icon Channel as icon on Sitemap

Tags: #<Tag:0x00007f2fc08ed900>

In my previous Wunderground based setup I would download the current conditions icon and save it in my icons/classic folder so it can be displayed on my sitemap. Now that Wunderground is ending their free API I’ve switched over to the OpenWeatherMap binding. To minimize the changes necessary I need to do the same thing with the icons from this service and luckily there is an Icon Channel.

As far as I can tell there is no way to use an Image type Item as the icon for another Item. :frowning: But I did notice that if I link a String to the Icon Channel instead of an Image I will get the URL to the Icon.

So I rewrote my Rule that downloads Wunderground icons to work with this Channel. Here it is:

vWeather_Conditions_Icon is a String Item linked to the Icon Channel of the OpenWeatherMap Thing.

rule "Copy the conditions icon"
  Item vWeather_Conditions_Icon changed
    // Were to save the downloaded file
    val file = "/openhab/conf/icons/classic/weather.gif"

    // Download it using wget
    executeCommandLine("/usr/bin/wget -q -O " + file + " " + vWeather_Conditions_Icon.state.toString, 5000)

    // Convert from gif to png
    val input = new File(file)
    val output = new File(file.replace("gif", "png"))
    ImageIO::write( ImageIO::read(input), "png", output)

    // Remove the gif
    executeCommandLine("rm " + file)

Set the icon on whatever Item you have linked to the Current Conditions Channel to <weather> and it will use the icon downloaded from OpenWeatherMap.

The above will work on Docker and any system with wget installed. You may need to change the path to wget though.

But the above will not work if you need SVG icons. For that you will probably need to install gif2png or convert on your machine and replace the ImageIO stuff with an executeCommandLine call to that utility. I run in Docker and don’t want to deviate from the stock image so those were not an option for me. But I was already using PNG icons so that’s no big deal.

Hope this helps someone.


Great, thx. I can use this.

Just as a little addition :slight_smile:

import javax.imageio.ImageIO;

you need to implemet those 2 thingies to make it work. maybe put that on top of the source code.