Get weather info from internet to Domoticz !
Posted: Friday 19 February 2016 17:47
'
Just wanted to share with you a way to retrieve current weather data from buienradar.nl and make it available in Domiticz
please note:
- this is for the raspberry pi
- buienradar.nl (yes, you guessed it) is a Dutch site
On a high level the steps are:
1. create a user variable in Domoticz
2. write a PHP script, the script retrieves the weather data (XML), formats it, and writes to the user variable
3. make the PHP script executable, and schedule it to run periodically
1. Create a user variable in Domoticz
(this variable shall retain ALL the weather info, semi-colon (;) separated. I prefer this method because it will not 'pollute' the Domoticz GUI)
in Domoticz, go to the menu Setup > More Options > User variables
create a new variable, eg. 'weatherinfo', type String, and give it some value, then Add it write down the name of the User Variable, and the corresponding Idx (in this example 18) 2. write a PHP script
the data from buienradar.nl is free available, and doesn't need a license, API key or whatsoever
the data is organized according to weather stations, which are physically spread across The Netherlands
to see which info is available, just type 'xml.buienradar.nl' in the address of your webbrowser
the info is refreshed every 10 minutes
find the weather station nearest to you (in this example Gilze-Rijen), and write down the 'stationcode' (in this example 6350), see the xml file
to create the PHP script:
- open a CLI, eg. Putty, and log in
- go to the folder domoticz/scripts: cd domoticz/scripts
- open a text editor to create the PHP script: eg. 'nano getweatherinfo.php'
- copy this code to the text editor:
- change the code, so it complies with your needs/situation:
- info to be passed to Domoticz (line 5, 18-24)
- station code (line 9)
- save the script (eg. nano: 'Ctrl+O')
- close the editor (eg. nano: 'Ctrl+X')
3. make the PHP script executable, and schedule it to run periodically
- with CLI still open, make the PHP script executable: chmod +x getweatherinfo.php
- check if the script is working: type './getweatherinfo.php', wait for the reply in the CLI, and take a look at the (updated) user variable in Domoticz
- schedule the script to run every 10 minutes, to do so:
- from the CLI, open crontab: 'crontab -e'
- append this line:
- do your thing !
- tip: to break down your new user variable into useful data, use this piece of lua code:\
Just wanted to share with you a way to retrieve current weather data from buienradar.nl and make it available in Domiticz
please note:
- this is for the raspberry pi
- buienradar.nl (yes, you guessed it) is a Dutch site
On a high level the steps are:
1. create a user variable in Domoticz
2. write a PHP script, the script retrieves the weather data (XML), formats it, and writes to the user variable
3. make the PHP script executable, and schedule it to run periodically
1. Create a user variable in Domoticz
(this variable shall retain ALL the weather info, semi-colon (;) separated. I prefer this method because it will not 'pollute' the Domoticz GUI)
in Domoticz, go to the menu Setup > More Options > User variables
create a new variable, eg. 'weatherinfo', type String, and give it some value, then Add it write down the name of the User Variable, and the corresponding Idx (in this example 18) 2. write a PHP script
the data from buienradar.nl is free available, and doesn't need a license, API key or whatsoever
the data is organized according to weather stations, which are physically spread across The Netherlands
to see which info is available, just type 'xml.buienradar.nl' in the address of your webbrowser
the info is refreshed every 10 minutes
find the weather station nearest to you (in this example Gilze-Rijen), and write down the 'stationcode' (in this example 6350), see the xml file
to create the PHP script:
- open a CLI, eg. Putty, and log in
- go to the folder domoticz/scripts: cd domoticz/scripts
- open a text editor to create the PHP script: eg. 'nano getweatherinfo.php'
- copy this code to the text editor:
Code: Select all
1. #!/usr/bin/php
2. <?php
3. // this script is scheduled to run (via crontab) every 10 minutes
4. $url = 'xml.buienradar.nl';
5. $todomoticz = "http://yourusername:[email protected]:8080/json.htm?type=command¶m=updateuservariable&idx=18&vname=weatherinfo&vtype=2&vvalue=";
6.
7. $xml = new SimpleXMLElement('http://xml.buienradar.nl/', null, true);
8. foreach ($xml->weergegevens->actueel_weer->weerstations->children() as $station) {
9. if ($station->stationcode == '6350') {
10. break; // stop the foreach loop if station is found
11. }
12. }
13. // $weerdata = $xml->weergegevens->actueel_weer->weerstations->weerstation[9]->children();
14. // $weerdata contains luchtvochtigheid, temperatuurGC, windsnelheidBF, windsnelheidMS,
15. // windrichting, windrichtingGR, luchtdruk, zichtmeters, windstotenMS,
16, // regenMMPU, temperatuur10cm
17. $weerdata = $station->children();
18. $pl = $weerdata->stationnaam;
19. $rh = intval($weerdata->luchtvochtigheid);
20. $tm = floatval($weerdata->temperatuurGC);
21. $ws = intval($weerdata->windsnelheidBF);
22. $wr = $weerdata->windrichting;
23. $ld = intval($weerdata->luchtdruk);
24. $todomoticz .= $pl . ";" . $tm . ";" . $rh . ";" . $ws . ";" . $wr . ";" . $ld;
25. $todomoticz = str_replace(' ', '_', $todomoticz);
26.
27. $json = file_get_contents($todomoticz); // send json command to domoticz, and store the reply from domoticz
28. $parsed = json_decode($json); // process the reply
29. echo("result of script: " . $parsed->status . "\n"); // report to user
30. ?>
- info to be passed to Domoticz (line 5, 18-24)
- station code (line 9)
- save the script (eg. nano: 'Ctrl+O')
- close the editor (eg. nano: 'Ctrl+X')
3. make the PHP script executable, and schedule it to run periodically
- with CLI still open, make the PHP script executable: chmod +x getweatherinfo.php
- check if the script is working: type './getweatherinfo.php', wait for the reply in the CLI, and take a look at the (updated) user variable in Domoticz
- schedule the script to run every 10 minutes, to do so:
- from the CLI, open crontab: 'crontab -e'
- append this line:
Code: Select all
*/10 * * * * /home/pi/domoticz/scripts/getweatherinfo.php
- tip: to break down your new user variable into useful data, use this piece of lua code:\
Code: Select all
local tabel={} i = 1 records = uservariables['weatherinfo']
for str in string.gmatch(records, '([^;]+)' ) do tabel[i] = str i = i + 1 end