Buienradar settings
Moderator: leecollings
Re: Buienradar settings
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¶m=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(); }
#!/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¶m=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(); }
- 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
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
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!
Re: Buienradar settings
It works (on linux), and as far as I know multiple SELECT at the same time is supported.gizmocuz wrote:you are not allowed to access the database directly, and on most linux systems, this might not work i think...
-
- 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
The "normal" way is working great to....
On a Cubieboard
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
- 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
Normally the database should be 'locked' and if one process (application) is using it, it should not (could not?) be opened by a second oneSweetPants wrote:It works (on linux), and as far as I know multiple SELECT at the same time is supported.gizmocuz wrote:you are not allowed to access the database directly, and on most linux systems, this might not work i think...
maybe it would be better to request the lat/lon value via json ?
Quality outlives Quantity!
Re: Buienradar settings
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 advisegizmocuz wrote:maybe it would be better to request the lat/lon value via json ?
Re: Buienradar settings
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¶m=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";
#!/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¶m=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";
Re: Buienradar settings
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.gizmocuz wrote:You can't it's a bash script
-
- 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
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?
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
Raspberry Pi Model 2 B w/ Domoticz 3.5974 + Monit + Pi-Hole + RaZberry Z-wave module + RFXcom 433 USB
- 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
That's not possible
Probably you are going to use this device for an event or script so what's an icon
Probably you are going to use this device for an event or script so what's an icon
Quality outlives Quantity!
-
- 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
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.
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
Raspberry Pi Model 2 B w/ Domoticz 3.5974 + Monit + Pi-Hole + RaZberry Z-wave module + RFXcom 433 USB
-
- 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
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
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
-
- 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
Solved with the following command sudo perl -MCPAN -e 'install LWP::UserAgent::Cached'
-
- Posts: 6
- Joined: Monday 08 February 2016 10:15
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 4.11590
- Contact:
Re: Buienradar settings
Thanks a lot for posting your solution, that helped me as well!
Who is online
Users browsing this forum: No registered users and 1 guest