Weather Forecast in openHAB based on OpenWeatherMap, using Ansible

Posted by ads' corner on Friday, 2019-01-25
Posted in [Ansible][Linux][Openhab]

After setting up OpenWeatherMap in openHAB, I had another project on my list: send a forecast for the next day.

That is rather easy to do with a Cron rule.

The rule is easy, it just combined a number of Items as text, and sends it to me, us. In this case it’s Telegram, but that is a different story.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
rule "Weather forecast for tomorrow"
when
    Time cron "0 30 17 * * ?"
then
    var weather_forecast = "Weather tomorrow 6am: " + String::format("%1$1d", (homef12Temperature.state as Number).intValue) + " °C"
    weather_forecast = weather_forecast + ", Humidity: " + homef12Humidity.state.toString
    weather_forecast = weather_forecast + ", Cloudiness: " + homef12Cloudiness.state.toString
    weather_forecast = weather_forecast + ", " + homef12Condition.state.toString

    var rain_value = homef12RainVolume.state.toString.split(' ').get(0)
    var rain_value_rounded = Math::round(Float::parseFloat(rain_value))
    var rain_unit = homef12RainVolume.state.toString.split(' ').get(1)

    if (rain_value_rounded > 0) {
        weather_forecast = weather_forecast + ", Rain: " + rain_value_rounded.toString + " " + rain_unit
    }

    var snow_value = homef012SnowVolume.state.toString.split(' ').get(0)
    var snow_value_rounded = Math::round(Float::parseFloat(snow_value))
    var snow_unit = homef012SnowVolume.state.toString.split(' ').get(1)

    if (snow_value_rounded > 0) {
        weather_forecast = weather_forecast + ", Snow: " + snow_value_rounded.toString + " " + snow_unit
    }

    logInfo("Weather forecast", weather_forecast)
    // send notification with the content of $weather_forecast
end

This rule is fired at 17:30 in the afternoon, uses the weather forecast data for 12 hours in the future, and creates a string which can be send using your favorite method.


Categories: [Ansible] [Linux] [Openhab]