Get weather info from internet to Domoticz !

In this subforum you can show projects you have made, or you are busy with. Please create your own topic.

Moderator: leecollings

Post Reply
User avatar
Marq
Posts: 17
Joined: Sunday 26 April 2015 12:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: The Netherlands
Contact:

Get weather info from internet to Domoticz !

Post by Marq »

'
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
01.png
01.png (4.47 KiB) Viewed 3696 times
write down the name of the User Variable, and the corresponding Idx (in this example 18)
02.png
02.png (4.42 KiB) Viewed 3696 times
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&param=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. ?>
- 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:

Code: Select all

*/10 * * * * /home/pi/domoticz/scripts/getweatherinfo.php
- do your thing !
- 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
Pino77
Posts: 4
Joined: Friday 04 September 2015 8:39
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8742
Location: The Netherlands
Contact:

Re: Get weather info from internet to Domoticz !

Post by Pino77 »

Thanks Marq for sharing. I have tried it. But whe i tired to run the php script I get this:

pi@zwave ~/domoticz/scripts $ chmod +x getweatherinfo.php
pi@zwave ~/domoticz/scripts $ ./getweatherinfo.php
./getweatherinfo.php: line 1: 1.: command not found
./getweatherinfo.php: line 2: ?php: No such file or directory
./getweatherinfo.php: line 3: syntax error near unexpected token `('
./getweatherinfo.php: line 3: `3. // this script is scheduled to run (via crontab) every 10 minutes'
pi@zwave ~/domoticz/scripts $

Do I need to remove the line numbers?
Raspberry Pi 3 with RFlink and RaZberry
BBI_home
Posts: 25
Joined: Monday 08 December 2014 13:53
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

crontab script not found error

Post by BBI_home »

After some starting problems I’ve managed to implement a Plugwise read script successfully which I can run manual.
Unfortunately I’m still not able to let it run as a crontab.

Some background information:
The scripts starts with:

#!/bin/bash
#Parameters section

#Folders and files
Scriptfolder=/home/pi/domoticz/scripts/

#The tempFile should preferably be on a ramdisk to avoid SD card wear
tempFile=/var/tmp/ram0/tempPlugwiseRead

Ram disk info: df
tmpfs 1024 0 1024 0% /var/ram0/tempPlugwiseRead
looks good also


By running my script manually:
pi@bbihome ~ $ ./domoticz/scripts/plugwise_read
everything look oke too

But now the hard part:
In my crontab I’ve put:
*/1 * * * * home/pi/domoticz/scripts/plugwise_read.sh
( one minute just to check)

And then I receive an error mail:

From pi@bbihome Mon Feb 22 12:05:01 2016
Return-path: <pi@bbihome>
Envelope-to: pi@bbihome
Delivery-date: Mon, 22 Feb 2016 12:05:01 +0100
Received: from pi by bbihome with local (Exim 4.80)
(envelope-from <pi@bbihome>)
id 1aXoIX-0007U3-EB
for pi@bbihome; Mon, 22 Feb 2016 12:05:01 +0100
From: root@bbihome (Cron Daemon)
To: pi@bbihome
Subject: Cron <pi@bbihome> home/pi/domoticz/scripts/plugwise_read.sh
Content-Type: text/plain; charset=UTF-8
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/home/pi>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=pi>
Message-Id: <E1aXoIX-0007U3-EB@bbihome>
Date: Mon, 22 Feb 2016 12:05:01 +0100

/bin/sh: 1: home/pi/domoticz/scripts/plugwise_read.sh: not found


Any suggestions?
SweetPants

Re: Get weather info from internet to Domoticz !

Post by SweetPants »

use /home/pi/domoticz/scripts/plugwise_read.sh instead of home/pi/domoticz/scripts/plugwise_read.sh
chmod +x /home/pi/domoticz/scripts/plugwise_read.sh
BBI_home
Posts: 25
Joined: Monday 08 December 2014 13:53
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Get weather info from internet to Domoticz !

Post by BBI_home »

/home is clear
but then the next one:
chmod: cannot access `/home/pi/domoticz/scripts/plugwise_read.sh': No such file or directory
User avatar
Westcott
Posts: 423
Joined: Tuesday 09 December 2014 17:04
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: UK - Glos
Contact:

Re: Get weather info from internet to Domoticz !

Post by Westcott »

What do you get with an -
ls /home/pi/domoticz/scripts
Zwave - Sigma Z+ stick, Fibaro, Horstmann, Neo Coolcam, EUROtronic
RFlink - IR detectors and temperatures
Wifi - YeeLights, ESP32s, Anoop sockets
Zigbee - lots with zigbee2mqtt and ZbBridge
BBI_home
Posts: 25
Joined: Monday 08 December 2014 13:53
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Get weather info from internet to Domoticz !

Post by BBI_home »

It's running :D without using .sh

so use /home/pi/domoticz/scripts/plugwise_read instead of /home/pi/domoticz/scripts/plugwise_read.sh
and use chmod +x /home/pi/domoticz/scripts/plugwise_read instead of chmod +x /home/pi/domoticz/scripts/plugwise_read.sh
User avatar
Marq
Posts: 17
Joined: Sunday 26 April 2015 12:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: The Netherlands
Contact:

Re: Get weather info from internet to Domoticz !

Post by Marq »

Pino77 wrote:Thanks Marq for sharing. I have tried it. But whe i tired to run the php script I get this:

pi@zwave ~/domoticz/scripts $ chmod +x getweatherinfo.php
pi@zwave ~/domoticz/scripts $ ./getweatherinfo.php
./getweatherinfo.php: line 1: 1.: command not found
./getweatherinfo.php: line 2: ?php: No such file or directory
./getweatherinfo.php: line 3: syntax error near unexpected token `('
./getweatherinfo.php: line 3: `3. // this script is scheduled to run (via crontab) every 10 minutes'
pi@zwave ~/domoticz/scripts $

Do I need to remove the line numbers?
Yes, you need to remove the line numbers.
Pino77
Posts: 4
Joined: Friday 04 September 2015 8:39
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8742
Location: The Netherlands
Contact:

Re: Get weather info from internet to Domoticz !

Post by Pino77 »

Hi Marq,
thanks for your help and patience, without line numbers I get this:

pi@zwave ~/domoticz/scripts $ ./getweatherinfo.php
-bash: ./getweatherinfo.php: /usr/bin/php: bad interpreter: No such file or directory
pi@zwave ~/domoticz/scripts $

How do I install PHP on the raspberry pi? (I use the Domoticz image for pi version 2.3530)
Raspberry Pi 3 with RFlink and RaZberry
User avatar
Marq
Posts: 17
Joined: Sunday 26 April 2015 12:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: The Netherlands
Contact:

Re: Get weather info from internet to Domoticz !

Post by Marq »

@Pino77

to install php5: 'sudo apt-get install php5'
Pino77
Posts: 4
Joined: Friday 04 September 2015 8:39
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8742
Location: The Netherlands
Contact:

Re: Get weather info from internet to Domoticz !

Post by Pino77 »

@Marq, dank voor je hulp maar ik lijk niet veel verder te komen:

pi@zwave ~ $ sudo apt-get install php5
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
apache2-mpm-prefork apache2-utils apache2.2-bin apache2.2-common
libapache2-mod-php5 libapr1 libaprutil1 libaprutil1-dbd-sqlite3
libaprutil1-ldap libonig2 libqdbm14 lsof php5-cli php5-common ssl-cert
Suggested packages:
www-browser apache2-doc apache2-suexec apache2-suexec-custom php-pear
openssl-blacklist
The following NEW packages will be installed:
apache2-mpm-prefork apache2-utils apache2.2-bin apache2.2-common
libapache2-mod-php5 libapr1 libaprutil1 libaprutil1-dbd-sqlite3
libaprutil1-ldap libonig2 libqdbm14 lsof php5 php5-cli php5-common ssl-cert
0 upgraded, 16 newly installed, 0 to remove and 0 not upgraded.
Need to get 6,721 kB/7,492 kB of archives.
After this operation, 22.2 MB of additional disk space will be used.
Do you want to continue [Y/n]? y
Err http://mirrordirector.raspbian.org/raspbian/ wheezy/main php5-common armhf 5.4.39-0+deb7u2
404 Not Found
Err http://mirrordirector.raspbian.org/raspbian/ wheezy/main apache2.2-bin armhf 2.2.22-13+deb7u4
404 Not Found
Err http://mirrordirector.raspbian.org/raspbian/ wheezy/main apache2-utils armhf 2.2.22-13+deb7u4
404 Not Found
Err http://mirrordirector.raspbian.org/raspbian/ wheezy/main apache2.2-common armhf 2.2.22-13+deb7u4
404 Not Found
Err http://mirrordirector.raspbian.org/raspbian/ wheezy/main apache2-mpm-prefork armhf 2.2.22-13+deb7u4
404 Not Found
Err http://mirrordirector.raspbian.org/raspbian/ wheezy/main libapache2-mod-php5 armhf 5.4.39-0+deb7u2
404 Not Found
Err http://mirrordirector.raspbian.org/raspbian/ wheezy/main php5 all 5.4.39-0+deb7u2
404 Not Found
Err http://mirrordirector.raspbian.org/raspbian/ wheezy/main php5-cli armhf 5.4.39-0+deb7u2
404 Not Found
Err http://mirrordirector.raspbian.org/raspbian/ wheezy/main ssl-cert all 1.0.32
404 Not Found
Failed to fetch http://mirrordirector.raspbian.org/rasp ... _armhf.deb 404 Not Found
Failed to fetch http://mirrordirector.raspbian.org/rasp ... _armhf.deb 404 Not Found
Failed to fetch http://mirrordirector.raspbian.org/rasp ... _armhf.deb 404 Not Found
Failed to fetch http://mirrordirector.raspbian.org/rasp ... _armhf.deb 404 Not Found
Failed to fetch http://mirrordirector.raspbian.org/rasp ... _armhf.deb 404 Not Found
Failed to fetch http://mirrordirector.raspbian.org/rasp ... _armhf.deb 404 Not Found
Failed to fetch http://mirrordirector.raspbian.org/rasp ... u2_all.deb 404 Not Found
Failed to fetch http://mirrordirector.raspbian.org/rasp ... _armhf.deb 404 Not Found
Failed to fetch http://mirrordirector.raspbian.org/rasp ... 32_all.deb 404 Not Found
E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?
pi@zwave ~ $
Raspberry Pi 3 with RFlink and RaZberry
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests