deye 600 or 800w microinverter (for example for balkonkraftwerk), how to read out data using a script? in my case a perl script?


##########################################################################################
# >> here is a solution: getDeyePowergenerationData.pl
##########################################################################################

#!/usr/bin/perl
use LWP::UserAgent;
use HTTP::Request::Common;
use Getopt::Long;
use strict;
##########################################################################
#
# getDeyePowergenerationData.pl
#
# get some data from a deye inverter like the current power, firmware, serial, wlan quality / information
#
# use the parameter --checkmk if you want to get the output in checkmk output format,
# then you can use the script as an individual script in checkmk!
# (Individual program call instead of agent access)
#
# usage:
# ./getDeyePowergenerationData.pl [--checkmk] [--debug] <ip_address> <name> <username> <password>
#
# needs some libraries:
# install them with this command: cpan DateTime::Event::Sunrise,
# or: apt-get install libdatetime-event-sunrise-per,
# apt-get install liblist-moreutils-perl
#
# (c) sys4com 2023
#
##########################################################################
my $version='s4c-2023-08-09';
use DateTime;
use DateTime::Event::Sunrise;

my ($ip_address, $name, $checkmk, $debug, $username, $password);

# Parse command line options
GetOptions(
'checkmk' => \$checkmk,
'debug' => \$debug
) or die "Usage: $0 [--checkmk] [--debug] <ip_address> <name> <username> <password>\n";

# Get the IP address and name from remaining command line arguments
($ip_address, $name, $username, $password) = @ARGV;

# Check if arguments were provided
unless (defined $ip_address && defined $name && defined $username && defined $password) {
print "Usage: $0 [--checkmk] [--debug] <ip_address> <name> <username> <password>\n";
exit;
}


# Set the URL of the page to fetch
my $url = "http://$ip_address/status.html";

my $current_power=0;
my $yield_today=0;
my $yield_total=0;
my $serial_device='';
my $serial_inverter='';
my $firmware='';
my $control=0;
my $sta_ssid=''; #wlan
my $sta_rssi=0; #wlan signal connection strength
my $sta_ip=''; #wlan ip
my $sta_mac=''; #wlan mac

# Funktion mit Breitengrad und Längengrad für Stuttgart
my $latitude = 48.7758; # Breitengrad von Stuttgart
my $longitude = 9.1829; # Längengrad von Stuttgart
my $isDaytime = is_it_daytime($latitude, $longitude);

#if ($isDaytime) {
# print "Es ist noch hell in Stuttgart.\n";
#} else {
# print "Es ist dunkel in Stuttgart.\n";
#}
#


if ($isDaytime){

# Create a new LWP::UserAgent object
my $ua = LWP::UserAgent->new();

# Create a new HTTP::Request object with authentication credentials
my $request = GET $url;
$request->authorization_basic($username, $password);

# Make the HTTP request and get the response
my $response = $ua->request($request);

# Check if the request was successful
if ($response->is_success) {
my $content = $response->content;
my @carray = split /\n/,$content;
my $i=0;
foreach my $line (@carray){
$i++;
print "$i: $line\n" if $debug;
if ($line=~ /var webdata_now_p.*\"(.*)\"/) { $current_power = $1; $control++; }
if ($line=~ /var webdata_today_e .*\"(.*)\"/) { $yield_today = $1; $control++; }
if ($line=~ /var webdata_total_e .*\"(.*)\"/) { $yield_total = $1; $control++; }
if ($line=~ /var webdata_sn.*\"(.*)\"/) { $serial_inverter = $1; $control++; $serial_inverter=~s/\s//g; }
if ($line=~ /var cover_mid.*\"(.*)\"/) { $serial_device = $1; $control++; }
if ($line=~ /var cover_ver.*\"(.*)\"/) { $firmware = $1; $control++; }
if ($line=~ /var cover_sta_ssid.*\"(.*)\"/) { $sta_ssid = $1; $control++; }
if ($line=~ /var cover_sta_rssi.*\"(.*)%\"/) { $sta_rssi = $1; $control++; }
if ($line=~ /var cover_sta_ip.*\"(.*)\"/) { $sta_ip = $1; $control++; }
if ($line=~ /var cover_sta_mac.*\"(.*)\"/) { $sta_mac = $1; $control++; }
}
} else {
print "HTTP request failed: " . $response->status_line . "\n";
}
}else{
$current_power=0;
$yield_today=0;
$yield_total=0;
$serial_inverter = 'na';
$serial_device = 'na';
$firmware = 'na';
$sta_ssid = 'na';
$sta_rssi = 0;
$sta_ip = 'na';
$sta_mac = 'na';
$control = 10;
}


if ($checkmk){
my $status=0;
my $txt_warn="";

print "<<<check_mk>>>\n";
print "version: $version\n";
print "<<<local>>>\n";
my $str_perf = "current-power=$current_power|production-today=$yield_today|production-total=$yield_total";
my $str_txt = "current-power=$current_power W, today=$yield_today kWH, total=$yield_total kWH, ".
"serial-inverter=$serial_inverter, serial-device=$serial_device, firmware=$firmware, name=$name.";
if ($control != 10){
$status = 1;
$txt_warn = " WARNING - could not get all values! ";
}
print "$status power power=$current_power current-power=$current_power W\n";
print "$status today today=$yield_today today=$yield_today kWH\n";
print "$status total total=$yield_total total=$yield_total kWH\n";
print "$status info - $txt_warn $str_txt\n";
print "$status wlan wlansignal=$sta_rssi Connected with WLAN $sta_ssid, Signalstrength=$sta_rssi%, ip=$sta_ip, mac=$sta_mac\n";
}else{
print "current power=$current_power.\n";
print "production today=$yield_today.\n";
print "production total=$yield_total.\n";
print "serial inverter=$serial_inverter.\n";
print "serial device=$serial_device.\n";
print "firmware=$firmware.\n";
print "wlan info: with WLAN $sta_ssid, Signalstrength=$sta_rssi%, ip=$sta_ip, mac=$sta_mac\n";
}


sub is_it_daytime {
my ($latitude, $longitude) = @_;

my $dt = DateTime->now(time_zone => 'local');
my $sunrise = DateTime::Event::Sunrise->new(
longitude => $longitude,
latitude => $latitude,
precise => 1,
)->sunrise_datetime($dt);

my $sunset = DateTime::Event::Sunrise->new(
longitude => $longitude,
latitude => $latitude,
precise => 1,
)->sunset_datetime($dt);

my $tolerance = 30; # Tolerance in minutes

my $sunrise_plus_tolerance = $sunrise->clone->add(minutes => $tolerance);
my $sunset_minus_tolerance = $sunset->clone->subtract(minutes => $tolerance);

if ($dt >= $sunrise_plus_tolerance && $dt <= $sunset_minus_tolerance) {
return 1; # It's daytime within the tolerance
} else {
return 0; # It's not yet daytime within the tolerance
}
}

computer2know :: thank you for your visit :: have a nice day :: © 2024