OpenWeatherMap Use Icon Channel as icon on Sitemap

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"
when
  Item vWeather_Conditions_Icon changed
then
    // 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)
end

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.

4 Likes

Great, thx. I can use this.

Just as a little addition :slight_smile:

import javax.imageio.ImageIO;
import java.io.File; 

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

2 Likes

Hey @rlkoshak,

i am just making my first steps with OH2 and tried to use your rule but did not manage to get it work. OpenWeatherMap Binding is running and working (Basic UI shows the values and the icon as well).

I extended my openweathermap.items file and it now includes

String  vWeather_Conditions_Icon  "MAP Icon String [%s]"                                 {channel="openweathermap:weather-and-forecast:api:local:current#icon"} //Needed to download icons from OpenWeatherMap
String  localCurrentCondition     "Wetter [%s]"           <openweathermap_lokal_Wetter>  {channel="openweathermap:weather-and-forecast:api:local:current#condition" } //Current condition 
Image   localCurrentConditionIcon "Icon"                                                 {channel="openweathermap:weather-and-forecast:api:local:current#icon"}

My home.sitemap contains (beside others)

    Frame label="Wetter" {
        //Text item=localStationId
        Text item=localStationName        
        //Mapview item=localStationLocation
        Text item=localCurrentCondition
        Image item=localCurrentConditionIcon
        Text item=localCurrentTemperature
        Text item=localCurrentPressure
        Text item=localCurrentHumidity
        ...
    }

For my Raspberry Pi i modified the paths and created an OWMicon2OHicon.rules containing

import javax.imageio.ImageIO;
import java.io.File; 

rule "Copy the conditions icon"
when
  Item vWeather_Conditions_Icon changed
then
    // Were to save the downloaded file
    val file = "/etc/openhab2/icons/classic/openweathermap_lokal_Wetter.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)
end

But still no file is stored in /etc/openhab2/icons/classic/openweathermap_lokal_Wetter.gif.
When i try to see the contents of vWeather_Conditions_Icon via Karaf console via

smarthome:status vWeather_Conditions_Icon 

it answers NULL.

Can you help me out with this and give me a hint, what i am doing wrong? Any hint is very much appreciated.

Thanks in advance
Patrick

If the item remains NULL then the problem is with your Link to the Channel that had the icon or the problem is with the binding itself. I don’t use OWM any more so I can’t speak to whether something had changed or not.

Thanks for the very fast reply. Too bad, that you are not using it anymore. That is what i also thought. Probably the binding changed somehow.

I will try to find a different solution. Thanks

You could look at the icon-id channel, which would allow you to fetch the icon image directly.
Other approaches in this thread -