diff --git a/fhem/CHANGED b/fhem/CHANGED index 9333500c2..388d48d44 100644 --- a/fhem/CHANGED +++ b/fhem/CHANGED @@ -498,3 +498,4 @@ - bugfix: FHT "summer" fix (avoiding a lot of syncnow) - feature: FHEMWEB modules added - feature: holiday module + doc + example + holiday2we attribute + - bugfix: sunrise stuff fixed, doc missing diff --git a/fhem/FHEM/90_at.pm b/fhem/FHEM/90_at.pm index 95c16602d..286427db1 100755 --- a/fhem/FHEM/90_at.pm +++ b/fhem/FHEM/90_at.pm @@ -119,7 +119,10 @@ at_Exec($) if($count) { $def =~ s/{\d+}/{$count}/ if($def =~ m/^\+?\*{\d+}/); # Replace the count Log GetLogLevel($name,5), "redefine at command $name as $def"; + + $data{AT_RECOMPUTE} = 1; # Tell sunrise compute the next day CommandDefine(undef, "$name at $def"); # Recompute the next TRIGGERTIME + delete($data{AT_RECOMPUTE}); $attr{$name} = $oldattr; } $at_tdiff = undef; diff --git a/fhem/FHEM/99_SUNRISE_EL.pm b/fhem/FHEM/99_SUNRISE_EL.pm index c326ee7d0..b020d5e9e 100755 --- a/fhem/FHEM/99_SUNRISE_EL.pm +++ b/fhem/FHEM/99_SUNRISE_EL.pm @@ -12,7 +12,7 @@ use strict; use warnings; use Math::Trig; -sub sr($$$$); +sub sr($$$$$$); sub sunrise_rel(@); sub sunset_rel(@); sub sunrise_abs(@); @@ -38,56 +38,56 @@ SUNRISE_EL_Initialize($) my ($hash) = @_; } + ########################## -# Compute: +# Compute the _next_ event # rise: 1: event is sunrise (else sunset) -# isrel: 1: _relative_ times until the next event (else absolute for today) +# isrel: 1: relative times # seconds: second offset to event # daycheck: if set, then return 1 if the sun is visible, 0 else sub -sr($$$$) +sr($$$$$$) { - my ($rise, $seconds, $isrel, $daycheck) = @_; + my ($rise, $seconds, $isrel, $daycheck, $min, $max) = @_; my $needrise = ($rise || $daycheck) ? 1 : 0; my $needset = (!$rise || $daycheck) ? 1 : 0; + $seconds = 0 if(!$seconds); my $nt = time; my @lt = localtime($nt); my $gmtoff = _calctz($nt,@lt); # in hour - my ($rt,$st) = _sr($needrise,$needset, $lt[5]+1900,$lt[4]+1,$lt[3], $gmtoff); - my $nh = $lt[2] + $lt[1]/60 + $lt[0]/3600; + my ($rt,$st) = _sr($needrise,$needset, $lt[5]+1900,$lt[4]+1,$lt[3], $gmtoff); + my $sst = ($rise ? $rt : $st) + ($seconds/3600); + + my $nh = $lt[2] + $lt[1]/60 + $lt[0]/3600; # Current hour since midnight if($daycheck) { return 0 if($nh < $rt || $nh > $st); return 1; } - $seconds = 0 if(!$seconds); - my $sst = ($rise ? $rt : $st); - if(!$isrel) { - $sst += ($seconds/3600); - return h2hms_fmt($sst); - } - - $sst += ($seconds/3600); - my $diff = 0; - if(int(($nh-$sst)*3600) >= 0) { - $nt += 86400; # Tommorow + if($data{AT_RECOMPUTE} || # compute it for tommorow + int(($nh-$sst)*3600) >= 0) { # if called a subsec earlier + $nt += 86400; $diff = 24; @lt = localtime($nt); $gmtoff = _calctz($nt,@lt); # in hour ($rt,$st) = _sr($needrise,$needset, $lt[5]+1900,$lt[4]+1,$lt[3], $gmtoff); - - $sst = ($rise ? $rt : $st); - $sst += ($seconds/3600); + $sst = ($rise ? $rt : $st) + ($seconds/3600); } - $diff = $diff + $sst - $nh; - return h2hms_fmt($diff); + $sst = hms2h($min) if(defined($min) && (hms2h($min) > $sst)); + $sst = hms2h($max) if(defined($max) && (hms2h($max) < $sst)); + + $sst += $diff if($isrel); + $sst -= $nh if($isrel == 1); + + return h2hms_fmt($sst); } + sub _sr($$$$$$) { @@ -304,6 +304,15 @@ _calctz($@) } +sub +hms2h($) +{ + my $in = shift; + my @a = split(":", $in); + return 0 if(int(@a) < 2 || $in !~ m/^[\d:]*$/); + return $a[0]+$a[1]/60 + ($a[2] ? $a[2]/3600 : 0); +} + sub h2hms($) { @@ -324,11 +333,13 @@ h2hms_fmt($) } -sub sunrise_rel(@) { return sr(1, shift, 1, 0) } -sub sunset_rel(@) { return sr(0, shift, 1, 0) } -sub sunrise_abs(@) { return sr(1, shift, 0, 0) } -sub sunset_abs(@) { return sr(0, shift, 0, 0) } -sub isday() { return sr(1, 0, 0, 1) } +sub sunrise_rel(@) { return sr(1, shift, 1, 0, shift, shift) } +sub sunset_rel(@) { return sr(0, shift, 1, 0, shift, shift) } +sub sunrise_abs(@) { return sr(1, shift, 0, 0, shift, shift) } +sub sunset_abs(@) { return sr(0, shift, 0, 0, shift, shift) } +sub sunrise(@) { return sr(1, shift, 2, 0, shift, shift) } +sub sunset(@) { return sr(0, shift, 2, 0, shift, shift) } +sub isday() { return sr(1, 0, 0, 1, undef, undef) } sub sunrise_coord($$$) { ($long, $lat, $tz) = @_; return undef; } 1; diff --git a/fhem/HISTORY b/fhem/HISTORY index 2309ea1a3..cc4fda954 100644 --- a/fhem/HISTORY +++ b/fhem/HISTORY @@ -396,3 +396,7 @@ - pgm3: bugfix, format table for userdef - pgm3: feature X10_support, taillogorder optional with date - pgm3: HMS100CO added, fhem.html relating pgm3 updated + +- Sat May 30 2009 (Rudi) + - 99_SUNRISE_EL: sunrise/sunset called in "at" computes correctly the next + event. New "sunrise()/sunset()" calls added, min max optional parameter. diff --git a/fhem/docs/commandref.html b/fhem/docs/commandref.html index e5ecd09e6..1126757ce 100644 --- a/fhem/docs/commandref.html +++ b/fhem/docs/commandref.html @@ -2848,10 +2848,14 @@ A line ending with \ will be concatenated with the next one, so long lines define a12 at +*{sunset_rel()} { fhem("set lamp on-till 23:00") if($we) } # Switch lamp1 and lamp2 on from 7:00 till 10 minutes after sunrise - define a13 at *07:00 set lamp1,lamp2 on-till {sunrise_abs(+600)} + define a13 at *07:00 set lamp1,lamp2 on-till {sunrise(+600)} # Switch the lamp off 2 minutes after sunrise each day - define a14 at +*{sunrise_rel(+120)} set lamp on + define a14 at +{sunrise(+120)} set lamp on + + # Switch lamp1 on at sunset, not before 18:00 and not after 21:00 + define a15 at *{sunset(0,"18:00","21:00")} set lamp1 on + Notes:
@@ -2864,11 +2868,9 @@ A line ending with \ will be concatenated with the next one, so long lines
  • if the current time is greater than the time specified, then the - command will be executed tomorrow. This is why the relative forms - of the sunset/sunrise functions should be used with the relative - (+) flag
  • + command will be executed tomorrow. -
  • In order to use the sunrise_rel()/sunset_rel() functions, +
  • In order to use the sunrise()/sunset() functions, put { sunrise_coord(long, lat, "") } into your lastinclude file, as in the above example. If you are not using sunrise_coord, then the coordinates for @@ -3420,25 +3422,32 @@ A line ending with \ will be concatenated with the next one, so long lines

    SUNRISE_EL