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. 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.
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
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.
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.