FHEM Forum

FHEM => Codeschnipsel => Thema gestartet von: mapovi am 25 April 2014, 21:48:41

Titel: HUE Tageslichtabhängig: Ein weiteres Werk
Beitrag von: mapovi am 25 April 2014, 21:48:41
Ich habe eine Funktion geschrieben, die die Togglebefehle von meinen FS20 Tastern annimmt und eine angemessenen Farbe (je nach Modi) für meine HUE Lampen auswählt.
0 - Kein Farbwechsel, nur Toggle
1 - Zufallsfarbe RGB
2 - Tageslichtabhängig (nach Geschmack)

Im Mode 2 wird die Farbtemperatur abhängig von Zeitschlitzen gesetzt, die eine relativ konstante Farb Temperatur haben, z.B. Nachmittag oder Abend. Die Helligkeit wird vom Twilight Helligkeitswert beeinflusst, kann aber individuell angepasst werden. Als Input für die Funktion sind zu der Angabe des Modis mehrere Devicesnamen oder Strukturen möglich.

Dabei habe ich untenliegenden Code in die 99_Utils.pm ausgelagert.

Ich würde mich freuen, wenn jemand noch Verbesserungs- oder Erweiterungsvorschläge hätte.

Aufgerufen wird die Funktion so:


define rc2_1_schalter1 notify FS20_Taster_2_1 {setColorProgram('2',('kueche_decke'))}


Hier der Code aus der 99_Utils.pm

sub toComparable {
   my ($date) = @_;
   my ($H,$M,$S) = $date =~ m{^([0-9]{2}):([0-9]{2}):([0-9]{2})}
      or die;
   return "$H$M$S";
}

my %lightsources = ('candle', 1500,
'sodium-vapor', 2000,
'bulb40', 2600,
'bulb60', 2700,
'bulb100', 2800,
'bulb200', 3000,
'halogen', 2750,
'fluorescent', 4000,
'morning-evening', 5000,
'forenoon-afternoon', 5500,
'noon-cloudy', 5800,
'cloudy', 7000,
'fog', 8000,
'bluehour', 10000,
'night', 15000
);

sub setColorProgram($@)
{
# $mode string: 0 - on/off no color change, 1 - random color (rgb), 2 - twilight dependend color and brightness (ct, pct)
# @lamps array: names of the hue devices

my ($mode, @lamps) = @_;
foreach (@lamps) {
if (OldValue($_) eq "off") {
if ($mode eq '0') {
}
elsif ($mode eq '1')
{
my ($r, $g, $b) = (int(rand(256)), int(rand(256)), int(rand(256)));
{fhem("set $_ rgb ".sprintf("%02x%02x%02x", $r, $g, $b))};;
}
elsif ($mode eq '2') {
{fhem("set $_ pct ".getBrightnessTwilight())};;
{fhem("set $_ color ".getColorTwilight())};;
}
{fhem ("setstate ".$_." on")};;
} else {
{fhem("set $_ off")};;
{fhem ("setstate ".$_." off")};;
}
}
}

sub getBrightnessTwilight()
{
# Returns the brightness calculated with the twilight module named "twilight" in pct

my $licht = ReadingsVal("twilight","light","6");
my $val = 0;
# Maximum daylight
if($licht eq 6){
$val = 10;
}
# Weather twilight
elsif($licht eq 5){
$val = 100;
}
# Twilight
elsif($licht eq 4){
$val = 70;
}
# Civil twilight
elsif($licht eq 3){
$val = 70;
}
# Nautical twilight
elsif($licht eq 2){
$val = 80;
}
# Astronomical twilight
elsif($licht eq 1){
$val = 90;
}
# Night
elsif($licht eq 0){
$val = 100;
}
return $val
}

sub getColorTwilight()
{
# Returns the color temperature calculated with the twilight module named "twilight" in ct.

my $t_now = localtime->strftime('%H%M%S');
my $t_sunrise = toComparable(ReadingsVal("twilight","sr","06:00:00"));
my $t_bluehour_sunrise = toComparable(ReadingsVal("twilight","sr_civil","06:00:00"));
my $t_sunset = toComparable(ReadingsVal("twilight","ss","06:00:00"));
my $t_bluehour_sunset = toComparable(ReadingsVal("twilight","ss_civil","06:00:00"));
my $t_begin = '000000';
my $t_end = '235900';
my $t_noon = '120000';
my $t_beforenoon = '100000';
my $t_afternoon = '140000';
my $colortemp = 3000;

# Night till blue hour in the morning
if ($t_now >= $t_begin && $t_now < $t_bluehour_sunrise) {
$colortemp = $lightsources{'halogen'};
}
# Blue hour till sunrise
elsif ($t_now >= $t_bluehour_sunrise && $t_now < $t_sunrise) {
$colortemp = $lightsources{'halogen'};
}
# Sunrise till forenoon
elsif ($t_now >= $t_sunrise && $t_now < $t_beforenoon) {
$colortemp = $lightsources{'morning-evening'};
}
# Forenoon till noon
elsif ($t_now >= $t_beforenoon && $t_now < $t_noon) {
$colortemp = $lightsources{'forenoon-afternoon'};
}
# Noon till afternoon
elsif ($t_now >= $t_noon && $t_now < $t_afternoon) {
$colortemp = $lightsources{'noon-cloudy'};
}
# Afternoon till evening
elsif ($t_now >= $t_afternoon && $t_now < $t_bluehour_sunrise) {
$colortemp = $lightsources{'forenoon-afternoon'};
}
# Evening till sunset
elsif ($t_now >= $t_sunrise && $t_now < $t_sunset) {
$colortemp = $lightsources{'morning-evening'};
}
# Sunset till blue hour
elsif ($t_now >= $t_sunset && $t_now < $t_bluehour_sunset) {
$colortemp = $lightsources{'halogen'};
}
# Blue hour till night
elsif ($t_now >= $t_bluehour_sunset) {
$colortemp = $lightsources{'halogen'};
}
return $colortemp
}
Titel: Antw:HUE Tageslichtabhängig: Ein weiteres Werk
Beitrag von: relox am 17 August 2014, 19:08:08
Hallo,

super Idee und würde ich auch gerne sofort ausprobieren und daran mitarbeiten/verbessern. Leider hab ich null Ahnung wie es bei mir zum laufen bekommen, habe erst seit 2 Wochen einen RaspPi und seit diesem Wochenende Fhem drauf. Hue ist bereits eingebunden und funktioniert auch...
Würde mich echt freuen, wenn du mir dabei helfen würdest! Verspreche es auch weiter zu entwickeln, zum Beispiel je nach Bewohner Farben wer zu Hause ist und ob überhaupt jemand da ist usw...

Beste Grüße
Felix