Posted by
ads
on
Thursday, 2019-01-24 Posted in [Ansible][Linux][Openhab]
Next item on my home automation todo list: weather, and forecast. No good system without that data!
After exploring the options which openHAB supports, I settled for OpenWeatherMap. Note: you need an account with OWM, the basic functionality is free, the paid options give you more and better forecast.
And of course, I install everything using Ansible, and can just repeat the entire installation if something does not work.
The bridge_uuid and forecast_uuid will become part of the Things names later on. The apikey is the OpenWeatherMap API key. refresh is the refresh time after which the plugin will fetch updates. language is the plugin language. location_name is a freely defined name which you can choose, and location are the WGS84 coordinates of the location where you want to see the weather reported for. forecastdays and forecasthours depend on your OWM subscription, the free account works fine with 5/24. The other variables are for the Playbook, and not relevant here.
The OWMopenHAB plugin comes in two parts, a bridge and the forecast. Let’s install the bridge first. For that we fetch all Things, and check if the bridge is already in the list.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
- name:Get thingsuri:url:"http://{{ ansible_host }}:8080/rest/things"register:o2_thingschanged_when:false# figure out if the OpenWeatherMap bridge already exists- name:Copy Things dataset_fact:owm_uuid:"{{ item.UID }}"owm_thingtypeuid:"{{ item.thingTypeUID }}"when:item.thingTypeUID == "openweathermap:weather-api"loop:"{{ o2_things.json }}"loop_control:label:"{{ item.thingTypeUID }} - {{ item.UID }}"
This uses two of the variables we created earlier.
If the bridge does not yet exist, let’s create it.
In order to create the new Thing bridge, a JSON-encoded request must be sent to openHAB. The required structure is a bit more complex, hence I moved it into a template, and fill in all the variables before. The template looks like this:
First part done, now the forecast. For that I reload the Things again, just to be sure, and scan for the Bridge and the Forecast. If the bridge was just created, the old $o2_things does not contain this data.
# after the bridge is available, we need to re-scan "things", and retrieve the bridge data# two possible cases:# 1) bridge was created moments ago# 2) bridge was already there- name:Get thingsuri:url:"http://{{ ansible_host }}:8080/rest/things"register:o2_thingschanged_when:false- name:Copy Things data (Bridge)set_fact:owm_uuid:"{{ item.UID }}"owm_thingtypeuid:"{{ item.thingTypeUID }}"when:item.thingTypeUID == "openweathermap:weather-api"loop:"{{ o2_things.json }}"loop_control:label:"{{ item.thingTypeUID }} - {{ item.UID }}"- name:Copy Things data (Forecast)set_fact:owm_forecast_uuid:"{{ item.UID }}"owm_forecast_thingtypeuid:"{{ item.thingTypeUID }}"when:item.thingTypeUID == "openweathermap:weather-and-forecast" and item.bridgeUID == owm_uuidloop:"{{ o2_things.json }}"loop_control:label:"{{ item.thingTypeUID }} - {{ item.UID }}"
The next step is the Thing creation for the Forecast, which looks similar to the previous step - except that the JSON is a bit different. The change is big enough that we can’t re-use the same template, it has to be a different one.
Number:TemperaturehomeCurrentTemperature"Current temperature [%.1f %unit%]"<temperature>{channel="openweathermap:weather-and-forecast:home:current#temperature"}Number:Temperaturehomef03Temperature"Temperature in 3h [%.1f %unit%]"<temperature>{channel="openweathermap:weather-and-forecast:home:forecastHours03#temperature"}Number:Temperaturehomef06Temperature"Temperature in 6h [%.1f %unit%]"<temperature>{channel="openweathermap:weather-and-forecast:home:forecastHours06#temperature"}Number:Temperaturehomef09Temperature"Temperature in 9h [%.1f %unit%]"<temperature>{channel="openweathermap:weather-and-forecast:home:forecastHours09#temperature"}Number:Temperaturehomef12Temperature"Temperature in 12h [%.1f %unit%]"<temperature>{channel="openweathermap:weather-and-forecast:home:forecastHours12#temperature"}Number:Temperaturehomef15Temperature"Temperature in 15h [%.1f %unit%]"<temperature>{channel="openweathermap:weather-and-forecast:home:forecastHours15#temperature"}Number:Temperaturehomef18Temperature"Temperature in 18h [%.1f %unit%]"<temperature>{channel="openweathermap:weather-and-forecast:home:forecastHours18#temperature"}Number:Temperaturehomef21Temperature"Temperature in 21h [%.1f %unit%]"<temperature>{channel="openweathermap:weather-and-forecast:home:forecastHours21#temperature"}Number:Temperaturehomef24Temperature"Temperature in 24h [%.1f %unit%]"<temperature>{channel="openweathermap:weather-and-forecast:home:forecastHours24#temperature"}ImagehomeCurrentIcon"Icon"{channel="openweathermap:weather-and-forecast:home:current#icon"}Imagehomef03Icon"Icon"{channel="openweathermap:weather-and-forecast:home:forecastHours03#icon"}Imagehomef06Icon"Icon"{channel="openweathermap:weather-and-forecast:home:forecastHours06#icon"}Imagehomef09Icon"Icon"{channel="openweathermap:weather-and-forecast:home:forecastHours09#icon"}Imagehomef12Icon"Icon"{channel="openweathermap:weather-and-forecast:home:forecastHours12#icon"}Imagehomef15Icon"Icon"{channel="openweathermap:weather-and-forecast:home:forecastHours15#icon"}Imagehomef18Icon"Icon"{channel="openweathermap:weather-and-forecast:home:forecastHours18#icon"}Imagehomef21Icon"Icon"{channel="openweathermap:weather-and-forecast:home:forecastHours21#icon"}Imagehomef24Icon"Icon"{channel="openweathermap:weather-and-forecast:home:forecastHours24#icon"}Number:DimensionlesshomeCurrentHumidity"Current humidity [%d %unit%]"<humidity>{channel="openweathermap:weather-and-forecast:home:current#humidity"}Number:Dimensionlesshomef03Humidity"Humidity in 3h [%d %unit%]"<humidity>{channel="openweathermap:weather-and-forecast:home:forecastHours03#humidity"}Number:Dimensionlesshomef06Humidity"Humidity in 6h [%d %unit%]"<humidity>{channel="openweathermap:weather-and-forecast:home:forecastHours06#humidity"}Number:Dimensionlesshomef09Humidity"Humidity in 9h [%d %unit%]"<humidity>{channel="openweathermap:weather-and-forecast:home:forecastHours09#humidity"}Number:Dimensionlesshomef12Humidity"Humidity in 12h [%d %unit%]"<humidity>{channel="openweathermap:weather-and-forecast:home:forecastHours12#humidity"}Number:Dimensionlesshomef15Humidity"Humidity in 15h [%d %unit%]"<humidity>{channel="openweathermap:weather-and-forecast:home:forecastHours15#humidity"}Number:Dimensionlesshomef18Humidity"Humidity in 18h [%d %unit%]"<humidity>{channel="openweathermap:weather-and-forecast:home:forecastHours18#humidity"}Number:Dimensionlesshomef21Humidity"Humidity in 21h [%d %unit%]"<humidity>{channel="openweathermap:weather-and-forecast:home:forecastHours21#humidity"}Number:Dimensionlesshomef24Humidity"Humidity in 24h [%d %unit%]"<humidity>{channel="openweathermap:weather-and-forecast:home:forecastHours24#humidity"}Number:DimensionlesshomeCurrentCloudiness"Current cloudiness [%d %unit%]"<clouds>{channel="openweathermap:weather-and-forecast:home:current#cloudiness"}Number:Dimensionlesshomef03Cloudiness"Cloudiness in 3h [%d %unit%]"<clouds>{channel="openweathermap:weather-and-forecast:home:forecastHours03#cloudiness"}Number:Dimensionlesshomef06Cloudiness"Cloudiness in 6h [%d %unit%]"<clouds>{channel="openweathermap:weather-and-forecast:home:forecastHours06#cloudiness"}Number:Dimensionlesshomef09Cloudiness"Cloudiness in 9h [%d %unit%]"<clouds>{channel="openweathermap:weather-and-forecast:home:forecastHours09#cloudiness"}Number:Dimensionlesshomef12Cloudiness"Cloudiness in 12h [%d %unit%]"<clouds>{channel="openweathermap:weather-and-forecast:home:forecastHours12#cloudiness"}Number:Dimensionlesshomef15Cloudiness"Cloudiness in 15h [%d %unit%]"<clouds>{channel="openweathermap:weather-and-forecast:home:forecastHours15#cloudiness"}Number:Dimensionlesshomef18Cloudiness"Cloudiness in 18h [%d %unit%]"<clouds>{channel="openweathermap:weather-and-forecast:home:forecastHours18#cloudiness"}Number:Dimensionlesshomef21Cloudiness"Cloudiness in 21h [%d %unit%]"<clouds>{channel="openweathermap:weather-and-forecast:home:forecastHours21#cloudiness"}Number:Dimensionlesshomef24Cloudiness"Cloudiness in 24h [%d %unit%]"<clouds>{channel="openweathermap:weather-and-forecast:home:forecastHours24#cloudiness"}DateTimehomeCurrentTS"Timestamp Current [%1$tY-%1$tm-%1$tdT%1$tH:%1$tM:%1$tS]"<time>{channel="openweathermap:weather-and-forecast:home:current#time-stamp"}DateTimehomef03TS"Timestamp 3h [%1$tY-%1$tm-%1$tdT%1$tH:%1$tM:%1$tS]"<time>{channel="openweathermap:weather-and-forecast:home:forecastHours03#time-stamp"}DateTimehomef06TS"Timestamp 6h [%1$tY-%1$tm-%1$tdT%1$tH:%1$tM:%1$tS]"<time>{channel="openweathermap:weather-and-forecast:home:forecastHours06#time-stamp"}DateTimehomef09TS"Timestamp 9h [%1$tY-%1$tm-%1$tdT%1$tH:%1$tM:%1$tS]"<time>{channel="openweathermap:weather-and-forecast:home:forecastHours09#time-stamp"}DateTimehomef12TS"Timestamp 12h [%1$tY-%1$tm-%1$tdT%1$tH:%1$tM:%1$tS]"<time>{channel="openweathermap:weather-and-forecast:home:forecastHours12#time-stamp"}DateTimehomef15TS"Timestamp 15h [%1$tY-%1$tm-%1$tdT%1$tH:%1$tM:%1$tS]"<time>{channel="openweathermap:weather-and-forecast:home:forecastHours15#time-stamp"}DateTimehomef18TS"Timestamp 18h [%1$tY-%1$tm-%1$tdT%1$tH:%1$tM:%1$tS]"<time>{channel="openweathermap:weather-and-forecast:home:forecastHours18#time-stamp"}DateTimehomef21TS"Timestamp 21h [%1$tY-%1$tm-%1$tdT%1$tH:%1$tM:%1$tS]"<time>{channel="openweathermap:weather-and-forecast:home:forecastHours21#time-stamp"}DateTimehomef24TS"Timestamp 24h [%1$tY-%1$tm-%1$tdT%1$tH:%1$tM:%1$tS]"<time>{channel="openweathermap:weather-and-forecast:home:forecastHours24#time-stamp"}StringhomeCurrentCondition"Current condition [%s]"<sun_clouds>{channel="openweathermap:weather-and-forecast:home:current#condition"}Stringhomef03Condition"Condition in 3h [%s]"<sun_clouds>{channel="openweathermap:weather-and-forecast:home:forecastHours03#condition"}Stringhomef06Condition"Condition in 6h [%s]"<sun_clouds>{channel="openweathermap:weather-and-forecast:home:forecastHours06#condition"}Stringhomef09Condition"Condition in 9h [%s]"<sun_clouds>{channel="openweathermap:weather-and-forecast:home:forecastHours09#condition"}Stringhomef12Condition"Condition in 12h [%s]"<sun_clouds>{channel="openweathermap:weather-and-forecast:home:forecastHours12#condition"}Stringhomef15Condition"Condition in 15h [%s]"<sun_clouds>{channel="openweathermap:weather-and-forecast:home:forecastHours15#condition"}Stringhomef18Condition"Condition in 18h [%s]"<sun_clouds>{channel="openweathermap:weather-and-forecast:home:forecastHours18#condition"}Stringhomef21Condition"Condition in 21h [%s]"<sun_clouds>{channel="openweathermap:weather-and-forecast:home:forecastHours21#condition"}Stringhomef24Condition"Condition in 24h [%s]"<sun_clouds>{channel="openweathermap:weather-and-forecast:home:forecastHours24#condition"}Number:SpeedhomeCurrentWindSpeed"Current wind speed [%.1f km/h]"<wind>{channel="openweathermap:weather-and-forecast:home:current#wind-speed"}Number:Speedhomef03WindSpeed"Wind speed in 3h [%.1f km/h]"<wind>{channel="openweathermap:weather-and-forecast:home:forecastHours03#wind-speed"}Number:Speedhomef06WindSpeed"Wind speed in 6h [%.1f km/h]"<wind>{channel="openweathermap:weather-and-forecast:home:forecastHours06#wind-speed"}Number:Speedhomef09WindSpeed"Wind speed in 9h [%.1f km/h]"<wind>{channel="openweathermap:weather-and-forecast:home:forecastHours09#wind-speed"}Number:Speedhomef12WindSpeed"Wind speed in 12h [%.1f km/h]"<wind>{channel="openweathermap:weather-and-forecast:home:forecastHours12#wind-speed"}Number:Speedhomef15WindSpeed"Wind speed in 15h [%.1f km/h]"<wind>{channel="openweathermap:weather-and-forecast:home:forecastHours15#wind-speed"}Number:Speedhomef18WindSpeed"Wind speed in 18h [%.1f km/h]"<wind>{channel="openweathermap:weather-and-forecast:home:forecastHours18#wind-speed"}Number:Speedhomef21WindSpeed"Wind speed in 21h [%.1f km/h]"<wind>{channel="openweathermap:weather-and-forecast:home:forecastHours21#wind-speed"}Number:Speedhomef24WindSpeed"Wind speed in 24h [%.1f km/h]"<wind>{channel="openweathermap:weather-and-forecast:home:forecastHours24#wind-speed"}Number:LengthhomeCurrentRainVolume"Current rain volume [%.1f %unit%]"<rain>{channel="openweathermap:weather-and-forecast:home:current#rain"}Number:Lengthhomef03RainVolume"Rain volume in 3h [%.1f %unit%]"<rain>{channel="openweathermap:weather-and-forecast:home:forecastHours03#rain"}Number:Lengthhomef06RainVolume"Rain volume in 6h [%.1f %unit%]"<rain>{channel="openweathermap:weather-and-forecast:home:forecastHours06#rain"}Number:Lengthhomef09RainVolume"Rain volume in 9h [%.1f %unit%]"<rain>{channel="openweathermap:weather-and-forecast:home:forecastHours09#rain"}Number:Lengthhomef12RainVolume"Rain volume in 12h [%.1f %unit%]"<rain>{channel="openweathermap:weather-and-forecast:home:forecastHours12#rain"}Number:Lengthhomef15RainVolume"Rain volume in 15h [%.1f %unit%]"<rain>{channel="openweathermap:weather-and-forecast:home:forecastHours15#rain"}Number:Lengthhomef18RainVolume"Rain volume in 18h [%.1f %unit%]"<rain>{channel="openweathermap:weather-and-forecast:home:forecastHours18#rain"}Number:Lengthhomef21RainVolume"Rain volume in 21h [%.1f %unit%]"<rain>{channel="openweathermap:weather-and-forecast:home:forecastHours21#rain"}Number:Lengthhomef24RainVolume"Rain volume in 24h [%.1f %unit%]"<rain>{channel="openweathermap:weather-and-forecast:home:forecastHours24#rain"}Number:LengthhomeCurrentSnowVolume"Current snow volume [%.1f %unit%]"<snow>{channel="openweathermap:weather-and-forecast:home:current#snow"}Number:Lengthhomef03SnowVolume"Snow volume in 3h [%.1f %unit%]"<snow>{channel="openweathermap:weather-and-forecast:home:forecastHours03#snow"}Number:Lengthhomef06SnowVolume"Snow volume in 6h [%.1f %unit%]"<snow>{channel="openweathermap:weather-and-forecast:home:forecastHours06#snow"}Number:Lengthhomef09SnowVolume"Snow volume in 9h [%.1f %unit%]"<snow>{channel="openweathermap:weather-and-forecast:home:forecastHours09#snow"}Number:Lengthhomef012SnowVolume"Snow volume in 12h [%.1f %unit%]"<snow>{channel="openweathermap:weather-and-forecast:home:forecastHours12#snow"}Number:Lengthhomef15SnowVolume"Snow volume in 15h [%.1f %unit%]"<snow>{channel="openweathermap:weather-and-forecast:home:forecastHours15#snow"}Number:Lengthhomef18SnowVolume"Snow volume in 18h [%.1f %unit%]"<snow>{channel="openweathermap:weather-and-forecast:home:forecastHours18#snow"}Number:Lengthhomef21SnowVolume"Snow volume in 21h [%.1f %unit%]"<snow>{channel="openweathermap:weather-and-forecast:home:forecastHours21#snow"}Number:Lengthhomef24SnowVolume"Snow volume in 24h [%.1f %unit%]"<snow>{channel="openweathermap:weather-and-forecast:home:forecastHours24#snow"}