From 9f005d050e5c5430fbc67e49e5f20f1402366c30 Mon Sep 17 00:00:00 2001 From: Marko Oldenburg Date: Mon, 19 Dec 2022 15:29:06 +0100 Subject: [PATCH] add warnCount reading --- 59_Weather.pm | 42 ++++++++++++++++++++++++++++++++++++------ OpenWeatherMapAPI.pm | 39 +++++++++++++-------------------------- 2 files changed, 49 insertions(+), 32 deletions(-) diff --git a/59_Weather.pm b/59_Weather.pm index 789bb65..04ac65c 100755 --- a/59_Weather.pm +++ b/59_Weather.pm @@ -329,7 +329,7 @@ sub Weather_ReturnWithError { return; } -sub Weather_DeleteReadings { +sub Weather_DeleteForecastReadings { my $hash = shift; my $name = $hash->{NAME}; @@ -350,6 +350,23 @@ sub Weather_DeleteReadings { return; } +sub Weather_DeleteAlertsReadings { + my $hash = shift; + my $alertsLimit = shift // 0; + + my $name = $hash->{NAME}; + my $alertsConfig = Weather_ForcastConfig($hash); + my $alertsLimitNoAlerts = 0; + + $alertsLimit = $alertsLimitNoAlerts + if ( !$alertsConfig->{alerts} ); + + CommandDeleteReading( undef, + $name . ' ' . 'warn_([' . $alertsLimit . '-9]|[0-9]{2})_.*' ); + + return; +} + sub Weather_RetrieveCallbackFn { my $name = shift; @@ -550,7 +567,6 @@ sub Weather_WriteReadings { { my $i = 0; foreach my $warn ( @{ $dataRef->{alerts} } ) { - $i++; my $w = "warn_" . $i . "_"; while ( my ( $r, $v ) = each %{$warn} ) { @@ -559,7 +575,18 @@ sub Weather_WriteReadings { && ref( $dataRef->{$r} ) ne 'ARRAY' ); } + $i++; } + + Weather_DeleteAlertsReadings( $hash, + scalar( @{ $dataRef->{alerts} } ) ); + readingsBulkUpdate( $hash, 'warnCount', + scalar( @{ $dataRef->{alerts} } ) ); + } + else { + Weather_DeleteAlertsReadings($hash); + readingsBulkUpdate( $hash, 'warnCount', + scalar( @{ $dataRef->{alerts} } ) ); } ### state @@ -817,13 +844,13 @@ sub Weather_Attr { $hash->{fhem}->{api}->setForecast(); } - InternalTimer( gettimeofday() + 1, \&Weather_DeleteReadings, - $hash ); + InternalTimer( gettimeofday() + 0.5, + \&Weather_DeleteForecastReadings, $hash ); } when ('forecastLimit') { - InternalTimer( gettimeofday() + 1, \&Weather_DeleteReadings, - $hash ); + InternalTimer( gettimeofday() + 0.5, + \&Weather_DeleteForecastReadings, $hash ); } when ('alerts') { @@ -833,6 +860,9 @@ sub Weather_Attr { elsif ( $cmd eq 'del' ) { $hash->{fhem}->{api}->setAlerts(); } + + InternalTimer( gettimeofday() + 0.5, + \&Weather_DeleteAlertsReadings, $hash ); } } diff --git a/OpenWeatherMapAPI.pm b/OpenWeatherMapAPI.pm index b2ef12c..b30cff4 100644 --- a/OpenWeatherMapAPI.pm +++ b/OpenWeatherMapAPI.pm @@ -157,6 +157,7 @@ my %codes = ( 602 => 13, 611 => 46, 612 => 46, + 613 => 46, 615 => 5, 616 => 5, 620 => 14, @@ -358,7 +359,7 @@ sub _CreateExcludeString { my @exclude = qw/alerts minutely hourly daily/; my @forecast = split( ',', $forecast ); - my @alerts = ( $alerts ? ',alerts' : '' ); + my @alerts = ( $alerts ? 'alerts' : '' ); my %in_forecast = map { $_ => 1 } @forecast, @alerts; my @diff = grep { not $in_forecast{$_} } @exclude; @@ -486,7 +487,9 @@ sub _ProcessingRetrieveData { sprintf( "%.1f", $data->{current}->{visibility} ) + 0.5 ), - 'uvi' => $data->{current}->{uvi}, + 'uvi' => $data->{current}->{uvi}, + 'timezone' => $data->{timezone}, + 'timezone_offset' => $data->{timezone_offset}, }; if ( ref( $data->{hourly} ) eq "ARRAY" @@ -503,15 +506,13 @@ sub _ProcessingRetrieveData { 'pubDate' => strftimeWrapper( "%a, %e %b %Y %H:%M", localtime( - ( $data->{hourly}->[$i]->{dt} ) - - 3600 + $data->{hourly}->[$i]->{dt} ) ), 'day_of_week' => strftime( "%a, %H:%M", localtime( - ( $data->{hourly}->[$i]->{dt} ) - - 3600 + $data->{hourly}->[$i]->{dt} ) ), 'temperature' => int( @@ -609,42 +610,31 @@ sub _ProcessingRetrieveData { 'pubDate' => strftimeWrapper( "%a, %e %b %Y %H:%M", localtime( - ( $data->{daily}->[$i]->{dt} ) - - 3600 + $data->{daily}->[$i]->{dt} ) ), 'day_of_week' => strftime( "%a, %H:%M", localtime( - ( $data->{daily}->[$i]->{dt} ) - - 3600 + $data->{daily}->[$i]->{dt} ) ), 'sunrise' => strftime( "%H:%M", localtime( - ( - $data->{daily}->[$i] - ->{sunrise} - ) - 3600 + $data->{daily}->[$i]->{sunrise} ) ), 'sunset' => strftime( "%a, %H:%M", localtime( - ( - $data->{daily}->[$i] - ->{sunset} - ) - 3600 + $data->{daily}->[$i]->{sunset} ) ), 'moonrise' => strftime( "%a, %H:%M", localtime( - ( - $data->{daily}->[$i] - ->{moonrise} - ) - 3600 + $data->{daily}->[$i]->{moonrise} ) ), 'moon_phase' => @@ -652,10 +642,7 @@ sub _ProcessingRetrieveData { 'moonset' => strftime( "%a, %H:%M", localtime( - ( - $data->{daily}->[$i] - ->{moonset} - ) - 3600 + $data->{daily}->[$i]->{moonset} ) ), 'temperature' => int(