FHEM Forum

FHEM - Hausautomations-Systeme => Unterstützende Dienste => Thema gestartet von: stenny73 am 17 Oktober 2013, 16:32:09

Titel: Frage zu Modul Twilight
Beitrag von: stenny73 am 17 Oktober 2013, 16:32:09
Hallo

Ich möchte eine Beleuchtung mit Twilight steuern.

Zum Testen habe ich mal ein paar Dummys gesetzt in dem ich jetzt Twilight mit einbinden will. Die Lampe soll ausgehen wenn dier dummy "Beleuchtung" "aus" ist und Twilight unter einen Wert x fällt. Ich finde jedoch nichts wie ich hier einen Schwellwert eintragen kann....

So sieht der erste Ansatz aus....

define Beleuchtung_Innen_aus notify myTwilight:twilight.100 {if (Value("Beleutung") eq "aus") {fhem("set Beleuchtung_Innen aus")}}

Danke
Titel: Antw:Frage zu Modul Twilight
Beitrag von: Puschel74 am 17 Oktober 2013, 19:02:17
Hallo,

ich hab sowas bei uns im Einsatz.

myTwilight {
my $light = ReadingsVal("myTwilight", "light", 99);
  if ($light < 2 ) {
    fhem ("set Dunkel on") if (Value("Dunkel") ne "on");
fhem ("set Mittel off") if (Value("Mittel") ne "off");
fhem ("set Hell off") if (Value("Hell") ne "off");
  }
  if ($light > 2 && $light < 4 ) {
    fhem ("set Dunkel off") if (Value("Dunkel") ne "off");
fhem ("set Mittel on") if (Value("Mittel") ne "on");
fhem ("set Hell off") if (Value("Hell") ne "off");
  }
  if ($light > 4 ) {
    fhem ("set Dunkel off") if (Value("Dunkel") ne "off");
fhem ("set Mittel off") if (Value("Mittel") ne "off");
fhem ("set Hell on") if (Value("Hell") ne "on");
  }
}


Damit schalte ich mir 3 Dummys. Auf diese prüfe ich dann in etwaigen notifys - aber das brauchst du ja nicht.
Die Abfrage nach dem Twilight-Wert sollte dir helfen.

Grüße
Titel: Antw:Frage zu Modul Twilight
Beitrag von: Dietmar63 am 17 Oktober 2013, 19:39:19
ich schalte folgendermaßen an:
define TwilightMessage1       notify Twilight.*:aktEvent:.*ss_weather.*     set StehlampeTisch on;; set StehlampeTV on
so könnte man bei anderen Ereignisse dann auch ausschalten.

mit
define TwilightMessage1       notify Twilight.*:aktEvent:.*                  {Log 3, "Nachricht von @: %"}
kannst du die verfügbaren Ereignisse anzeigen lassen.

ich glaube mit
define Beleuchtung_Innen_aus notify myTwilight:twilight.100 {if (Value("Beleutung") eq "aus") {fhem("set Beleuchtung_Innen aus")}}
wirst du keinen Erfolg haben, weil dieses Ereignis nur bei völliger Helligkeit gesendet wird.

define Beleuchtung_Innen_aus notify myTwilight:twilight:.* {if (Value("Beleutung") eq "aus") {fhem("set Beleuchtung_Innen aus")}}
Dann wirst du bei jeder Veränderung von twilight in myTwilight benachrichtigt. Den konkreten Schwellwert von twilight mußt du noch in die Abfrage des if mit aufnehmen.

Das Reading twilight_weather rechnet noch einen Bewölkungsfaktor mit ein, ist dann ggf. früher unter 100%. Die Berechnunsformel findest du in der commandref.

Titel: Antw:Frage zu Modul Twilight
Beitrag von: stenny73 am 18 Oktober 2013, 09:40:54
Danke erstmal für die Antworten....

Das mit dem Code und den Dummys ist ja nicht schlecht. Nur ich verstehe erst einmal nicht
wie das gehen soll.
Ich habe ja
define myTwilight Twilight x y 3 z
schon definiert. Den zusätzlichen Code

myTwilight {
my $light = ReadingsVal("myTwilight", "light", 99);
  if ($light < 2 ) {
    fhem ("set Dunkel on") if (Value("Dunkel") ne "on");
fhem ("set Mittel off") if (Value("Mittel") ne "off");
fhem ("set Hell off") if (Value("Hell") ne "off");
  }
  if ($light > 2 && $light < 4 ) {
    fhem ("set Dunkel off") if (Value("Dunkel") ne "off");
fhem ("set Mittel on") if (Value("Mittel") ne "on");
fhem ("set Hell off") if (Value("Hell") ne "off");
  }
  if ($light > 4 ) {
    fhem ("set Dunkel off") if (Value("Dunkel") ne "off");
fhem ("set Mittel off") if (Value("Mittel") ne "off");
fhem ("set Hell on") if (Value("Hell") ne "on");
  }
}

kann ich ja so nicht direkt in die *.cfg einfügen.
Wie müste ich das dann machen?

stenn73
Titel: Antw:Frage zu Modul Twilight
Beitrag von: Dietmar63 am 19 Oktober 2013, 06:50:52
Muss in die 99_Utils

Gesendet von meinem HTC Desire S mit Tapatalk-4

Titel: Antw:Frage zu Modul Twilight
Beitrag von: stenny73 am 19 Oktober 2013, 12:32:52
Wenn ich das am Anfang der 99_Utils.pm eintrage


myTwilight {
my $light = ReadingsVal("myTwilight", "light", 99);
  if ($light < 2 ) {
    fhem ("set Dunkel on") if (Value("Dunkel") ne "on");
fhem ("set Mittel off") if (Value("Mittel") ne "off");
fhem ("set Hell off") if (Value("Hell") ne "off");
  }
  if ($light > 2 && $light < 4 ) {
    fhem ("set Dunkel off") if (Value("Dunkel") ne "off");
fhem ("set Mittel on") if (Value("Mittel") ne "on");
fhem ("set Hell off") if (Value("Hell") ne "off");
  }
  if ($light > 4 ) {
    fhem ("set Dunkel off") if (Value("Dunkel") ne "off");
fhem ("set Mittel off") if (Value("Mittel") ne "off");
fhem ("set Hell on") if (Value("Hell") ne "on");
  }
}

##############################################
# $Id: 99_Utils.pm 3595 2013-08-05 05:38:48Z tobiasfaust $
package main;

use strict;
use warnings;
use POSIX;

sub
Utils_Initialize($$)
{
  my ($hash) = @_;
}

sub
time_str2num($)
{
  my ($str) = @_;
  my @a;
  if($str ne "") {
    @a = split("[- :]", $str);
    return mktime($a[5],$a[4],$a[3],$a[2],$a[1]-1,$a[0]-1900,0,0,-1);
  } else {
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
    return mktime($sec, $min, $hour, $mday, $mon, $year, 0, 0, -1);
  }
}

sub
min($@)
{
  my ($min, @vars) = @_;
  for (@vars) {
    $min = $_ if $_ lt $min;
  }           
  return $min;
}

sub
max($@)
{
  my ($max, @vars) = @_;
  for (@vars) {
    $max = $_ if $_ gt $max;
  }           
  return $max;
}

sub
minNum($@)
{             
  my ($min, @vars) = @_;
  for (@vars) {
    $min = $_ if $_ < $min;
  }           
  return $min;
}

sub
maxNum($@)
{             
  my ($max, @vars) = @_;
  for (@vars) {
    $max = $_ if $_ > $max;
  }           
  return $max;
}


sub
abstime2rel($)
{
  my ($h,$m,$s) = split(":", shift);
  $m = 0 if(!$m);
  $s = 0 if(!$s);
  my $t1 = 3600*$h+60*$m+$s;

  my @now = localtime;
  my $t2 = 3600*$now[2]+60*$now[1]+$now[0];
  my $diff = $t1-$t2;
  $diff += 86400 if($diff <= 0);

  return sprintf("%02d:%02d:%02d", $diff/3600, ($diff/60)%60, $diff%60);
}


######## trim #####################################################
# What  : cuts blankspaces from the beginning and end of a string
# Call  : { trim(" Hello ") }
# Source: http://www.somacon.com/p114.php ,
#         http://www.fhemwiki.de/wiki/TRIM-Funktion-Anfangs/EndLeerzeichen_aus_Strings_entfernen
sub trim($)
{
   my $string = shift;
   $string =~ s/^\s+//;
   $string =~ s/\s+$//;
   return $string;
}

######## ltrim ####################################################
# What  : cuts blankspaces from the beginning of a string
# Call  : { ltrim(" Hello") }
# Source: http://www.somacon.com/p114.php ,
#         http://www.fhemwiki.de/wiki/TRIM-Funktion-Anfangs/EndLeerzeichen_aus_Strings_entfernensub ltrim($)
sub ltrim($)
{
   my $string = shift;
   $string =~ s/^\s+//;
   return $string;
}

######## rtrim ####################################################
# What  : cuts blankspaces from the end of a string
# Call  : { rtrim("Hello ") }
# Source: http://www.somacon.com/p114.php ,
#         http://www.fhemwiki.de/wiki/TRIM-Funktion-Anfangs/EndLeerzeichen_aus_Strings_entfernensub ltrim($)
sub rtrim($)
{
   my $string = shift;
   $string =~ s/\s+$//;
   return $string;
}

######## UntoggleDirect ###########################################
# What  : For devices paired directly, converts state 'toggle' into 'on' or 'off'
# Call  : { UntoggleDirect("myDevice") }
#         define untoggle_myDevice notify myDevice { UntoggleDirect("myDevice") }
# Source: http://www.fhemwiki.de/wiki/FS20_Toggle_Events_auf_On/Off_umsetzen
sub UntoggleDirect($)
{
my ($obj) = shift;
Log 4, "UntoggleDirect($obj)";
if (Value($obj) eq "toggle"){
   if (OldValue($obj) eq "off") {
     {fhem ("setstate ".$obj." on")}
   }
   else {
     {fhem ("setstate ".$obj." off")}
   }
}
else {
   {fhem "setstate ".$obj." ".Value($obj)}

}


######## UntoggleIndirect #########################################
# What  : For devices paired indirectly, switches the target device 'on' or 'off' also when a 'toggle' was sent from the source device
# Call  : { UntoggleIndirect("mySensorDevice","myActorDevice","50%") }
#         define untoggle_mySensorDevice_myActorDevice notify mySensorDevice { UntoggleIndirect("mySensorDevice","myActorDevice","50%%") }
# Source: http://www.fhemwiki.de/wiki/FS20_Toggle_Events_auf_On/Off_umsetzen
sub UntoggleIndirect($$$)
{
  my ($sender, $actor, $dimvalue) = @_;
  Log 4, "UntoggleIndirect($sender, $actor, $dimvalue)";
  if (Value($sender) eq "toggle")
  {
    if (Value($actor) eq "off") {fhem ("set ".$actor." on")}
    else {fhem ("set ".$actor." off")}
  }
  ## workaround for dimming currently not working with indirect pairing
  ## (http://culfw.de/commandref.html: "TODO/Known BUGS - FS20 dim commands should not repeat.")
  elsif (Value($sender) eq "dimup") {fhem ("set ".$actor." dim100%")}
  elsif (Value($sender) eq "dimdown") {fhem ("set ".$actor." ".$dimvalue)}
  elsif (Value($sender) eq "dimupdown")
  {
    if (Value($actor) eq $dimvalue) {fhem ("set ".$actor." dim100%")}
       ## Heuristic above doesn't work if lamp was dimmed, then switched off, then switched on, because state is "on", but the lamp is actually dimmed.
    else {fhem ("set ".$actor." ".$dimvalue)}
    sleep 1;
  }
  ## end of workaround
  else {fhem ("set ".$actor." ".Value($sender))}

  return;
}


1;

=pod
=begin html

<a name="Utils"></a>
<h3>Utils</h3>
<ul>
<br/>
This is a collection of functions that can be used module-independant in all your own development<br/>
</br>
<pre>
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# IMPORTANT: do not insert your own functions inside
# the file 99_Utils.pm!
#
# This file will be overwritten during an FHEM update and all
# your own inserts will be lost.
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#
# To avoid this, we recommend following procedure:
#
# 1. Create your own file 99_myUtils.pm from the template below
# 2. Put this file inside the ./FHEM directory
# 3. Put your own functions into this new file
#
<br/>
<code>
# start-of-template
package main;

use strict;
use warnings;
use POSIX;

sub
myUtils_Initialize($$)
{
my ($hash) = @_;
}

# start with your own functions below this line


# behind your last function, we need the following
1;
# end-of-template
</code>
</pre>
</br>
<b>Defined functions</b><br/><br/>
<ul>
<li><b>abstime2rel()</b><br>???</li><br/>
<li><b>ltrim()</b><br>returns string without leading spaces</li><br/>
<li><b>max()</b><br>returns the highest value from a given list (sorted alphanumeric)</li><br/>
<li><b>maxNum()</b><br>returns the highest value from a given list (sorted numeric)</li><br/>
<li><b>min()</b><br>returns the lowest value from a given list (sorted alphanumeric)</li><br/>
<li><b>minNum()</b><br>returns the lowest value from a given list (sorted numeric)</li><br/>
<li><b>rtrim()</b><br>returns string without trailing spaces</li><br/>
<li><b>time_str2num()</b><br>???</li><br/>
<li><b>trim()</b><br>returns string without leading and without trailing spaces</li><br/>
<li><b>UntoggleDirect()</b><br>For devices paired directly, converts state 'toggle' into 'on' or 'off'</li><br/>
<li><b>UntoggleIndirect()</b><br>For devices paired indirectly, switches the target device 'on' or 'off' <br/>
also when a 'toggle' was sent from the source device</li><br/>
</ul>
</ul>
=end html
=cut




erhalte ich eine Fehlermeldung:

syntax error at ./FHEM/99_Utils.pm line 22, near "package main" BEGIN not safe after errors--compilation aborted at ./FHEM/99_Utils.pm line 24.

Titel: Antw:Frage zu Modul Twilight
Beitrag von: Dietmar63 am 19 Oktober 2013, 14:13:03
Was steht denn genau in Zeile 24?
Eventuell musst du die Funktion nach weiter unten vor oder nach der ersten sub verschieben.
Titel: Antw:Frage zu Modul Twilight
Beitrag von: Puschel74 am 19 Oktober 2013, 14:28:18
Hallo,

ich hab das eigentlich nicht in der 99_(my)Utils.pm sondern als notify in meiner fhem.cfg

Grüße
Titel: Antw:Frage zu Modul Twilight
Beitrag von: stenny73 am 19 Oktober 2013, 18:26:39
Also irgendwie verzweifele ich daran  :-X

Bei mir wäre wenn es eingefügt ist ab Zeile 20
##############################################
# $Id: 99_Utils.pm 3595 2013-08-05 05:38:48Z tobiasfaust $
package main;


Also der Anfang der normalen 99_Utils

Wenn ich das in einer cfg einfüge bekomme ich

Unknown command myTwilight, try help.
Unknown command my, try help.
Unknown command if, try help.
Unknown command fhem, try help.
Unknown command fhem, try help.
Unknown command fhem, try help.
Unknown command }, try help.
Unknown command if, try help.
Unknown command fhem, try help.
Unknown command fhem, try help.
Unknown command fhem, try help.
Unknown command }, try help.
Unknown command if, try help.
Unknown command fhem, try help.
Unknown command fhem, try help.
Unknown command fhem, try help.
Unknown command }, try help.
Unknown command }, try help.
Titel: Antw:Frage zu Modul Twilight
Beitrag von: Dietmar63 am 19 Oktober 2013, 19:00:24
Die Lösung Perlcode in die .cfg zu bauen führt zu riesigen .cfg .
Die Dinge die du bekommen hast sind nicht vollständig gewesen.

packe dies in die fhem.cfg
define Beleuchtung_Innen_aus notify myTwilight:light.* { myTwilight() }
mit diesem notify wird myTwilight() aufgerufen.
Die dummys musst du auch noch anlegen. Wenn du willst kannst du den Helligkeitstatus in einem dummy ablegen. Dann muss der folgende Code umgebaut werden.

Packe dies in die 99_utis. Du hast es richtig gemacht, aber der Code war nicht vollständig.
es fehlt am Anfang ein sub. Wenn du dir die 99_utils ansiehst, findest du immer Sequenzen, die mit sub xxx($) { anfangen und weiter unten mit "}" enden. Das sind Funktionen. Und myTwilight ist auch eine. Du darfst den neuen Code in einfach in eine solche Sequenz kopieren. Dann bekommst du ein großes Durcheinder und es hagelt Fehlermeldungen.

sub myTwilight {
  my $light = ReadingsVal("myTwilight", "light", 99);
  if ($light < 2 ) {
    fhem ("set Dunkel on") if (Value("Dunkel") ne "on");
   fhem ("set Mittel off") if (Value("Mittel") ne "off");
   fhem ("set Hell off") if (Value("Hell") ne "off");
  }
  if ($light > 2 && $light < 4 ) {
    fhem ("set Dunkel off") if (Value("Dunkel") ne "off");
   fhem ("set Mittel on") if (Value("Mittel") ne "on");
   fhem ("set Hell off") if (Value("Hell") ne "off");
  }
  if ($light > 4 ) {
    fhem ("set Dunkel off") if (Value("Dunkel") ne "off");
   fhem ("set Mittel off") if (Value("Mittel") ne "off");
   fhem ("set Hell on") if (Value("Hell") ne "on");
  }
}


ps: ein kleiner Grundkurs in Perl könnte nicht schaden, wenn man mit fhem viel machen will.
Titel: Antw:Frage zu Modul Twilight
Beitrag von: stenny73 am 19 Oktober 2013, 19:10:09
Danke erstmal.

Werde den Code soweit einfügen.

Und der Hinweis auf Perl zu lernen wurde vorher schon umgesetzt.
Es wurden schon 2Bücher bestellt - diese treffen hoffentlich balt ein......


Danke
Titel: Antw:Frage zu Modul Twilight
Beitrag von: Dietmar63 am 19 Oktober 2013, 19:18:53
im Netz gibt es reichlich Seiten und pdf dazu.
Titel: Antw:Frage zu Modul Twilight
Beitrag von: stenny73 am 19 Oktober 2013, 19:21:42
Schon richtig. Aber da ich häufig unterwegs bin ist es da manchmal einfacher mit einem Buch.
Mir hat es schon oft genug den Akku platt gemacht.... Und das war dann auch blöd.

Gesendet von meinem HTC One mit Tapatalk-4