Buienradar settings

Moderator: leecollings

SweetPants

Re: Buienradar settings

Post by SweetPants »

Modified the script a little to get the Longitude and Latitude from the domoticz database

#!/usr/bin/perl -w

=head1 buienradar_rain.pl

Installation:

In Domoticz create a Dummy Percentage sensor.
Next go to the Devices overview, and write down the 'idx' value of the sensor.

Copy this file to another location and edit the setting below to match your situation.

cp /home/pi/domoticz/scripts/buienradar_rain_example.pl /home/pi/domoticz/buienradar_rain.pl
nano /home/pi/domoticz/buienradar_rain.pl

You might have to install perl or additional modules

DBI: CPAN
DBD::SQLite: CPAN

sudo perl -MCPAN -e shell
cpan{1] install DBI
cpan[2] install DBD::SQLite
exit


LWP: http://lwp.interglacial.com/ch01_03.htm
sudo apt-get install libjson-perl

Next add a Crontab rule:

crontab -e

Add the following line at the end:

*/5 * * * * perl /home/pi/domoticz/buienradar_rain.pl

In 5 minutes, the sensor should work

=cut

use strict;
use LWP::UserAgent;
use HTTP::Cookies;
use DBI;

my $domoticz_ip="127.0.0.1";
my $domoticz_port="8080";
my $domoticz_sensor_idx="307";
my $domoticz_user="pi";
my $domoticz_pass="<your pi password>";
my $driver="SQLite";
my $database="/home/pi/domoticz.db";


my $dbh = DBI->connect("DBI:$driver:dbname=$database",$domoticz_user, $domoticz_pass, { RaiseError => 1 }) or die $DBI::errstr;
my ($lat, $long) = split(/;/,$dbh->selectrow_array( qq(SELECT sValue FROM preferences WHERE Key = 'Location')));


my $duration=15;

my @user_agents = (
'Mozilla/4.0 (compatible; MSIE 4.01; Windows 98)'
);

my $ua = LWP::UserAgent->new(
#Set agent name, we are not a script! :)
agent => $user_agents[rand @user_agents],
cookie_jar => HTTP::Cookies->new(),
);

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();

my $startTime=($hour*60)+$min;
my $endTime=$startTime+$duration;

my $url = "http://gps.buienradar.nl/getrr.php?lat=$lat&lon=$long";
my $response = $ua->get($url);

unless ($response->is_success) {
# hmm, let's retry
$response = $ua->get($url);

unless ($response->is_success) {
# still no luck; sleep and retry
sleep 1;
$response = $ua->get($url);

unless ($response->is_success) {
print "Could not connect to buienradar.nl.\n";
exit 0;
}
}
}

my $data = $response->content;

unless ($data =~ /\A((\d{3})?\|\d{2}:\d{2}\r?\n)+\z/) {
print "Could not parse the data returned by buienradar.nl.\n";
exit 0;
}

my $total_rain_predictions=0;
my $total_rain_values=0;

while ($data =~ s/\A(\d{3})?\|(\d{2}:\d{2})\r?\n//) {
my ($value, $mtime) = ($1, $2);

if (defined $value) {

my @hour_min = split(':', $mtime);
my $mhour = $hour_min[0];
my $mmin = $hour_min[1];
my $calc_time=($mhour*60)+$mmin;
if (($calc_time>=$startTime)&&($calc_time<=$endTime)) {
$value =~ s/\A0+(\d)/$1/;
$total_rain_predictions+=$value;
$total_rain_values+=1;
}
}
}

my $result = "0.0";

if ($total_rain_values!=0) {
my $rain_0_100=($total_rain_predictions/$total_rain_values)*0.392156862745098;
$result = sprintf("%.2f", $rain_0_100);
}

$url = "http://$domoticz_ip:$domoticz_port/json.htm?type=command&param=udevice&idx=$domoticz_sensor_idx&nvalue=0&svalue=$result";
$response = $ua->get($url);

unless ($response->is_success) {
print "Error sending data to domoticz!\n";
exit 0;
}
$data = $response->content;

if (index($data, "\"OK\"") == -1) {
print "Error sending data to domoticz!\n";
exit 0;
}

print "OK, precip=$result\n";

if ($dbh) { $dbh->disconnect(); }
User avatar
gizmocuz
Posts: 2352
Joined: Thursday 11 July 2013 18:59
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Top of the world
Contact:

Re: Buienradar settings

Post by gizmocuz »

you are not allowed to access the database directly, and on most linux systems, this might not work i think...

I leave the script as it is, its not to hard to add 2 values (latitude/longitude), and the overhead of installing perl sqlite is then also not needed

Bdw (for others), this is for the script inside the domoticz/scripts/buienradar_rain_example.pl

it is only suitable for dutch (and Belgium i suppose) users
Quality outlives Quantity!
SweetPants

Re: Buienradar settings

Post by SweetPants »

gizmocuz wrote:you are not allowed to access the database directly, and on most linux systems, this might not work i think...
It works (on linux), and as far as I know multiple SELECT at the same time is supported.
Derik
Posts: 1601
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: Buienradar settings

Post by Derik »

The "normal" way is working great to.... :D :D
On a Cubieboard
Xu4: Beta Extreme antenna RFXcomE,WU Fi Ping ip P1 Gen5 PVOutput Harmony HUE SolarmanPv OTG Winddelen Alive ESP Buienradar MySensors WOL Winddelen counting RPi: Beta SMAspot RFlinkTest Domoticz ...Different backups
User avatar
gizmocuz
Posts: 2352
Joined: Thursday 11 July 2013 18:59
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Top of the world
Contact:

Re: Buienradar settings

Post by gizmocuz »

SweetPants wrote:
gizmocuz wrote:you are not allowed to access the database directly, and on most linux systems, this might not work i think...
It works (on linux), and as far as I know multiple SELECT at the same time is supported.
Normally the database should be 'locked' and if one process (application) is using it, it should not (could not?) be opened by a second one

maybe it would be better to request the lat/lon value via json ?
Quality outlives Quantity!
SweetPants

Re: Buienradar settings

Post by SweetPants »

gizmocuz wrote:maybe it would be better to request the lat/lon value via json ?
That's another option, will get into that this evening. I want to keep my scripts as easy as possible to save me from editing them every time I change something somewhere else. Thanks for the advise
SweetPants

Re: Buienradar settings

Post by SweetPants »

Changed the script to use Domoticz config JSON

#!/usr/bin/perl -w

=head1 buienradar_rain.pl

Installation:

In Domoticz create a Dummy Percentage sensor.
Next go to the Devices overview, and write down the 'idx' value of the sensor.

Copy this file to another location and edit the setting below to match your situation.

cp /home/pi/domoticz/scripts/buienradar_rain_example.pl /home/pi/domoticz/buienradar_rain.pl
nano /home/pi/domoticz/buienradar_rain.pl

You might have to install perl or additional modules

DBI: CPAN
DBD::SQLite: CPAN
JSON: CPAN

sudo perl -MCPAN -e shell

cpan{1] install DBI
cpan[2] install DBD::SQLite
cpan[3] install JSON

exit

LWP: http://lwp.interglacial.com/ch01_03.htm
sudo apt-get install libjson-perl

Next add a Crontab rule:

crontab -e

Add the following line at the end:

*/5 * * * * perl /home/pi/domoticz/buienradar_rain.pl

In 5 minutes, the sensor should work

=cut

use strict;
use LWP::UserAgent;
use HTTP::Cookies;
use DBI;
use JSON;
use Data::Dumper;

my $domoticz_ip="127.0.0.1";
my $domoticz_port="8080";
my $domoticz_sensor_idx="307";
my ($long, $lat);
my ($url, $response, $data);

my $duration=15;

my @user_agents = ( 'Mozilla/4.0 (compatible; MSIE 4.01; Windows 98)' );

my $ua = LWP::UserAgent->new(
#Set agent name, we are not a script! :)
agent => $user_agents[rand @user_agents],
cookie_jar => HTTP::Cookies->new(),
);

# Get config from Domoticz
$url = "http://127.0.0.1:8080/json.htm?type=com ... =getconfig";
$response = $ua->get($url);

unless ($response->is_success)
{
print "Could not connect to Domoticz.\n"; exit 0;
}

# Extract longitude and lattitude from JSON string
$data = decode_json($response->content);

$long = $data->{Longitude};
$lat = $data->{Latitude};

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();

my $startTime=($hour*60)+$min;
my $endTime=$startTime+$duration;

$url = "http://gps.buienradar.nl/getrr.php?lat=$lat&lon=$long";
$response = $ua->get($url);

unless ($response->is_success) {
# hmm, let's retry
$response = $ua->get($url);

unless ($response->is_success) {
# still no luck; sleep and retry
sleep 1;
$response = $ua->get($url);

unless ($response->is_success) {
print "Could not connect to buienradar.nl.\n";
exit 0;
}
}
}

$data = $response->content;

unless ($data =~ /\A((\d{3})?\|\d{2}:\d{2}\r?\n)+\z/) {
print "Could not parse the data returned by buienradar.nl.\n";
exit 0;
}

my $total_rain_predictions=0;
my $total_rain_values=0;

while ($data =~ s/\A(\d{3})?\|(\d{2}:\d{2})\r?\n//) {
my ($value, $mtime) = ($1, $2);

if (defined $value) {

my @hour_min = split(':', $mtime);
my $mhour = $hour_min[0];
my $mmin = $hour_min[1];
my $calc_time=($mhour*60)+$mmin;
if (($calc_time>=$startTime)&&($calc_time<=$endTime)) {
$value =~ s/\A0+(\d)/$1/;
$total_rain_predictions+=$value;
$total_rain_values+=1;
}
}
}

my $result = "0.0";

if ($total_rain_values!=0) {
my $rain_0_100=($total_rain_predictions/$total_rain_values)*0.392156862745098;
$result = sprintf("%.2f", $rain_0_100);
}

$url = "http://$domoticz_ip:$domoticz_port/json.htm?type=command&param=udevice&idx=$domoticz_sensor_idx&nvalue=0&svalue=$result";
$response = $ua->get($url);

unless ($response->is_success) {
print "Error sending data to domoticz!\n";
exit 0;
}
$data = $response->content;

if (index($data, "\"OK\"") == -1) {
print "Error sending data to domoticz!\n";
exit 0;
}

print "OK, precip=$result\n";
jjnj

Re: Buienradar settings

Post by jjnj »

How do you use this script on Windows?
User avatar
gizmocuz
Posts: 2352
Joined: Thursday 11 July 2013 18:59
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Top of the world
Contact:

Re: Buienradar settings

Post by gizmocuz »

You can't it's a bash script
Quality outlives Quantity!
SweetPants

Re: Buienradar settings

Post by SweetPants »

gizmocuz wrote:You can't it's a bash script
But you probably can use it if you install ActiveState Perl on your Windows PC, install the right Perl Modules using Perls Packet Manager (ppm) and change the first line to where your Perl is installed. Make sure Perl is in your PATH when calling the script.
dutchnld
Posts: 20
Joined: Friday 23 October 2015 22:18
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.5974
Location: Amsterdam, Netherlands
Contact:

Re: Buienradar settings

Post by dutchnld »

Hi, I got this up and running by reading through this thread.
Works great, but I'd like to change the percentage Icon to a different Icon.
But for this type of device I can't change is.

Does anyone know how to workaround this?
-----
Raspberry Pi Model 2 B w/ Domoticz 3.5974 + Monit + Pi-Hole + RaZberry Z-wave module + RFXcom 433 USB
User avatar
gizmocuz
Posts: 2352
Joined: Thursday 11 July 2013 18:59
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Top of the world
Contact:

Re: Buienradar settings

Post by gizmocuz »

That's not possible
Probably you are going to use this device for an event or script so what's an icon
Quality outlives Quantity!
dutchnld
Posts: 20
Joined: Friday 23 October 2015 22:18
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.5974
Location: Amsterdam, Netherlands
Contact:

Re: Buienradar settings

Post by dutchnld »

In this case you're right. I like the overview to be nice with relevant logo's though.

But in other cases like dummy switches, I'd would be very useful.
Pity that's not possible.
-----
Raspberry Pi Model 2 B w/ Domoticz 3.5974 + Monit + Pi-Hole + RaZberry Z-wave module + RFXcom 433 USB
assenzuid
Posts: 135
Joined: Friday 13 November 2015 9:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: The Netherlands, Emmen Area
Contact:

Re: Buienradar settings

Post by assenzuid »

When i run the script I get below error

perl buienradar_rain.pl
Can't locate LWP/UserAgent.pm in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.14.2 /usr/local/share/perl/5.14.2 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.14 /usr/share/perl/5.14 /usr/local/lib/site_perl .) at buienradar_rain.pl line 2.
BEGIN failed--compilation aborted at buienradar_rain.pl line 2.

I have installed Perl following this quide: http://lwp.interglacial.com/ch01_03.htm
assenzuid
Posts: 135
Joined: Friday 13 November 2015 9:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: The Netherlands, Emmen Area
Contact:

Re: Buienradar settings

Post by assenzuid »

Solved with the following command sudo perl -MCPAN -e 'install LWP::UserAgent::Cached'
JaapKoppejan
Posts: 6
Joined: Monday 08 February 2016 10:15
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.11590
Contact:

Re: Buienradar settings

Post by JaapKoppejan »

Thanks a lot for posting your solution, that helped me as well!
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests