Hauptmenü

DOIF Templates

Begonnen von _Markus_, 24 Februar 2018, 16:51:20

Vorheriges Thema - Nächstes Thema

_Markus_

Ich habe mir mit dem neuen uiTable Feature einige DOIF Templates aufgebaut.
Ziel der Templates ist Regeln über das UI zu konfigurieren (ohne DEF Editor).

Im folgenden liste ich die Templates auf.
Vielleicht möchte sie jemanden nutzen, korrigieren oder verbessern.

Das erste Template ist das Countdown_Template. Es führt eine Aktion aus, nachdem ein bestimmtes Licht-Devices "on" ist und eine bestimmte Zeit abgelaufen ist.
Das Master-Devices fängt im Namen mit "Master_" an. Das Licht-Device fängt mit "Licht_" an:
defmod Countdown_Template DOIF ([$SELF:trgr] \
and Value([$SELF:P_master_device]) eq "on" \
and Value([$SELF:P_device]) ne "off"\
and [$SELF:P_enabled] eq "on")\
(set [$SELF:P_device]:FILTER=STATE!=off off)\
\
DOELSEIF ([$SELF:P_off_between_enabled] eq "sunset"\
and [{sunset(0,"[$SELF:P_off_between_from]","[$SELF:P_off_between_to]")}])\
(set [$SELF:P_device]:FILTER=STATE!=off off)\
\
DOELSEIF ([$SELF:P_off_between_enabled] eq "sunrise"\
and [{sunrise(0,"[$SELF:P_off_between_from]","[$SELF:P_off_between_to]")}])\
(set [$SELF:P_device]:FILTER=STATE!=off off)\
\
DOELSEIF ([$SELF:P_device] or [$SELF:P_wait_timer])\
({fhem("attr $SELF DOIF_Readings trgr:\x5b[$SELF:P_device]\x5d");;;;\
my $wt = "[$SELF:P_wait_timer]";;;; my $mult = 1;;;;\
$mult = 60*60 if(index($wt,"h")>-1);;;;\
$mult = 60 if(index($wt,"m")>-1);;;;\
$wt =~ s/\D//g;;;; $wt = $wt*$mult;;;;\
fhem("attr $SELF wait $wt:1:1:1:1")})\
\
DOELSE\
(setstate $SELF sleeping)
attr Countdown_Template DOIF_Readings trgr:[Licht_Studio]
attr Countdown_Template checkReadingEvent 1
attr Countdown_Template cmdState countdown,countdown,sleeping|sunset off|sunrise off|config|sleeping
attr Countdown_Template disable 0
attr Countdown_Template do always
attr Countdown_Template event-on-change-reading .*
attr Countdown_Template group Templates
attr Countdown_Template icon helper_doif
attr Countdown_Template room Rule Templates
attr Countdown_Template selftrigger wait
attr Countdown_Template stateFormat {sprintf("is %s, master %s, %s, %s: %s, wait: %s (%s), off-btw: %s, %s, %s", ,\
ReadingsVal("$name","P_enabled", "???"),\
ReadingsVal("$name","P_master_device", "???"),\
AttrVal("$name","DOIF_Readings","???"),\
ReadingsVal("$name","P_device","???"),\
Value(ReadingsVal("$name","P_device","???")),\
ReadingsVal("$name","P_wait_timer","???"),\
ReadingsVal("$name","state", "???"),\
ReadingsVal("$name","P_off_between_enabled", "???"),\
ReadingsVal("$name","P_off_between_from", "???"),\
ReadingsVal("$name","P_off_between_to", "???"))}
attr Countdown_Template templateName Countdown_Template
attr Countdown_Template uiTable WID([$SELF:P_master_device],[@"^(Master_)"])|\
WID([$SELF:P_device],[@"^(Licht_)"])|\
WID([$SELF:P_wait_timer],"select,1m,5m,15m,30m,1h,2h,4h,6h,12h,24h")||\
WID([$SELF:P_enabled],"select,off,on")\
|"Switch off between"|\
WID([$SELF:P_off_between_from],"time")|\
WID([$SELF:P_off_between_to],"time")|\
WID([$SELF:P_off_between_enabled],"select,off,sunset,sunrise")
attr Countdown_Template wait 300:1:1:1:1



Das zweite Template ist für Bewegungsmelder deren Name mit "Bewegungsmelder_" startet. Geschaltet werden Devices mit dem Namen "^(Licht_|Zwischenstecker_|Tablet)":
defmod Motion_Template DOIF ([$SELF:trgr] eq "motion"\
and [?$SELF:trgr:sec] < [?$SELF:P_off_timer]\
and Value([?$SELF:P_master_device]) eq "on"\
and ReadingsVal([?$SELF:P_motion_device], "brightness", 0) < [?$SELF:P_brightness,999]\
and (Value([?$SELF:P_target_device]) eq [?$SELF:P_target_device_off_state]\
or [?$SELF:P_target_device_off_state,"n/a"] eq "n/a")\
and [?[?$SELF:P_between_from,"00:00"]-[?$SELF:P_between_to,"23:59"]] \
and [?$SELF:P_enabled] eq "on")\
   (set [$SELF:P_target_device] [$SELF:P_cmd])\
   ({fhem("set [$SELF:P_target_device] [$SELF:P_cmd_off]") if("[$SELF:P_wait_timer]" ne "n/a")})\
   \
DOELSEIF ([$SELF:P_motion_device] or [$SELF:P_wait_timer])\
({ fhem("attr $SELF DOIF_Readings trgr:\x5b[$SELF:P_motion_device]\x5d");;;;\
my $wt = "[$SELF:P_wait_timer]";;;; my $mult = 1;;;;\
$wt = 0 if($wt eq "n/a");;;;\
$mult = 60*60 if(index($wt,"h")>-1);;;;\
$mult = 60 if(index($wt,"m")>-1);;;;\
$wt =~ s/\D//g;;;; $wt = $wt*$mult;;;;\
fhem("attr $SELF wait 0,$wt:0:1")}})\
\
DOELSE
attr Motion_Template DOIF_Readings trgr:[Bewegungsmelder_Treppe]
attr Motion_Template checkReadingEvent 1
attr Motion_Template cmdState threshold|config|sleeping
attr Motion_Template disable 0
attr Motion_Template do always
attr Motion_Template event-on-change-reading .*
attr Motion_Template group Templates
attr Motion_Template icon helper_doif
attr Motion_Template room Rule Templates
attr Motion_Template selftrigger wait
attr Motion_Template stateFormat {sprintf("is %s, mas %s (%s), mot %s: %s (%s), trg %s: %s, brg %s (%s), off-st %s, tim %s, from %s, till %s, cmd %s for %s, then %s (%s)", \
ReadingsVal("$name","P_enabled", "???"),\
ReadingsVal("$name","P_master_device", "???"),\
Value(ReadingsVal("$name","P_master_device","???")),\
ReadingsVal("$name","P_motion_device","???"),\
ReadingsVal(ReadingsVal("$name","P_motion_device","???"),"state","???"),\
ReadingsAge("$name","trgr","???"),\
ReadingsVal("$name","P_target_device","???"),\
ReadingsVal(ReadingsVal("$name","P_target_device","???"),"state","???"),\
ReadingsVal("$name","P_brightness", "???"),\
ReadingsVal(ReadingsVal("$name","P_motion_device","???"),"brightness","???"),\
ReadingsVal("$name","P_target_device_off_state", "???"),\
ReadingsVal("$name","P_off_timer", "???"),\
ReadingsVal("$name","P_between_from", "???"),\
ReadingsVal("$name","P_between_to", "???"),\
ReadingsVal("$name","P_cmd", "???"),\
ReadingsVal("$name","P_wait_timer", "???"),\
ReadingsVal("$name","P_cmd_off", "???"),\
ReadingsVal("$name","state", ""))}
attr Motion_Template templateName Power_Treshold_Template
attr Motion_Template uiTable "Master Device</br>".WID([$SELF:P_master_device],[@"^(Master_)"])|\
"Motion Device</br>".WID([$SELF:P_motion_device],[@"^Bewegungsmelder_"])|\
"Target Device</br>".WID([$SELF:P_target_device],[@"^(Licht_|Zwischenstecker_|Tablet)"])|\
"Command</br>".WID([$SELF:P_cmd],"textFieldNL")|\
"off after</br>".WID([$SELF:P_wait_timer],"select,n/a,1m,5m,15m,30m,1h,2h,4h,6h,12h,24h")\
"Been off for seconds</br>".WID([$SELF:P_off_timer],"selectnumbers,1,1,30,0,lin")|\
"Below Brightness</br>".WID([$SELF:P_brightness],"textFieldNL")|\
"Between...</br>".WID([$SELF:P_between_from],"time")|\
"...and</br>".WID([$SELF:P_between_to],"time")|""\
""|\
"Trg Dev off-state (can be n/a)</br>".WID([$SELF:P_target_device_off_state],"textFieldNL")|\
"Off-Command</br>".WID([$SELF:P_cmd_off],"textFieldNL")|\
""|\
"on/off</br>".WID([$SELF:P_enabled],"select,off,on")
attr Motion_Template wait 0,0:0:1


Ein Power-Threshold Device:
defmod Power_Threshold_Template DOIF ([$SELF:trgr] <= [$SELF:P_threshold_off]\
and Value([$SELF:P_master_device]) eq "on" \
and Value([$SELF:P_device]) ne "off"\
and [$SELF:P_enabled] eq "on")\
   (set [$SELF:P_device] [$SELF:P_cmd])\
   \
DOELSEIF ([$SELF:P_device] or [$SELF:P_wait_timer])\
({  fhem("attr $SELF DOIF_Readings trgr:\x5b[$SELF:P_device]:power\x5d");;;;\
my $wt = "[$SELF:P_wait_timer]";;;; my $mult = 1;;;;\
$mult = 60*60 if(index($wt,"h")>-1);;;;\
$mult = 60 if(index($wt,"m")>-1);;;;\
$wt =~ s/\D//g;;;; $wt = $wt*$mult;;;;\
fhem("attr $SELF wait $wt:1:1")})\
\
DOELSE
attr Power_Threshold_Template DOIF_Readings trgr:[Zwischenstecker_Beamer_PCA301:power]
attr Power_Threshold_Template checkReadingEvent 1
attr Power_Threshold_Template cmdState threshold|config|sleeping
attr Power_Threshold_Template disable 0
attr Power_Threshold_Template do always
attr Power_Threshold_Template event-on-change-reading .*
attr Power_Threshold_Template group Templates
attr Power_Threshold_Template icon helper_doif
attr Power_Threshold_Template room Rule Templates
attr Power_Threshold_Template selftrigger wait
attr Power_Threshold_Template stateFormat {sprintf("is %s, master %s, %s: %s, thr %s, %s, cmd %s (%s)", \
ReadingsVal("$name","P_enabled", "???"),\
ReadingsVal("$name","P_master_device", "???"),\
ReadingsVal("$name","P_device","???"),\
ReadingsVal(ReadingsVal("$name","P_device","???"),"power","???"),\
ReadingsVal("$name","P_threshold_off", "???"),\
ReadingsVal("$name","P_wait_timer", "???"),\
ReadingsVal("$name","P_cmd", "???"),\
ReadingsVal("$name","state", ""))}
attr Power_Threshold_Template templateName Power_Treshold_Template
attr Power_Threshold_Template uiTable "Master"|"Device"|"off at threshold"|"wait for"|"command"|"on/off"\
WID([$SELF:P_master_device],[@"^(Master_)"])|\
WID([$SELF:P_device],[@"^Zwischenstecker_":power])|\
WID([$SELF:P_threshold_off],"textFieldNL")|\
WID([$SELF:P_wait_timer],"select,1m,5m,15m,30m,1h,2h,4h,6h,12h,24h")|\
WID([$SELF:P_cmd],"textFieldNL")|\
WID([$SELF:P_enabled],"select,off,on")
attr Power_Threshold_Template wait 300:1:1


Und ein Timer Template, das auch sunset und sunrise berücksichtigt, sowie die Anwesenheit bestimmter Personengruppen:
defmod Timer_Sun_Template DOIF ([{sunrise(0,"[$SELF:P_begin_min]","[$SELF:P_begin_max]")}|So Mo Di Mi Do] and !Value("NRW_Ferientag")\
and Value([$SELF:P_master_device]) eq "on" \
and Value([$SELF:P_device]) ne "[$SELF:P_cmd_on]"\
and ([$SELF:P_enabled,"off"] eq "on" or [$SELF:P_enabled,"off"] eq "sunrise")\
and ([$SELF:P_presence,"no presence"] eq "no presence" \
or [$SELF:P_presence,"no presence"] eq "sunset" \
or Value([$SELF:P_presence_group]) eq "home"))\
   (set [$SELF:P_device] [$SELF:P_cmd_on,"on"])\
   \
DOELSEIF ([{sunset(0,"[$SELF:P_end_min]","[$SELF:P_end_max]")}|So Mo Di Mi Do] and !Value("NRW_Ferientag")\
and Value([$SELF:P_master_device]) eq "on" \
and Value([$SELF:P_device]) ne "[$SELF:P_cmd_off]"\
and ([$SELF:P_enabled,"off"] eq "on" or [$SELF:P_enabled,"off"] eq "sunset")\
and ([$SELF:P_presence,"no presence"] eq "no presence" \
or [$SELF:P_presence,"no presence"] eq "sunrise" \
or Value([$SELF:P_presence_group]) eq "home"))\
   (set [$SELF:P_device] [$SELF:P_cmd_off,"off"])\
   \
DOELSEIF ([{sunrise(0,"[$SELF:P_begin_holiday_min]","[$SELF:P_begin_holiday_max]")}|Fr Sa] \
or ([{sunrise(0,"[$SELF:P_begin_holiday_min]","[$SELF:P_begin_holiday_max]")}] and Value("NRW_Ferientag"))\
and Value([$SELF:P_master_device]) eq "on" \
and Value([$SELF:P_device]) ne "[$SELF:P_cmd_on]"\
and ([$SELF:P_enabled_holiday,"off"] eq "on" or [$SELF:P_enabled_holiday,"off"] eq "sunrise")\
and ([$SELF:P_presence,"no presence"] eq "no presence" \
or [$SELF:P_presence,"no presence"] eq "sunset" \
or Value([$SELF:P_presence_group]) eq "home"))\
   (set [$SELF:P_device] [$SELF:P_cmd_on,"on"])\
   \
DOELSEIF ([{sunset(0,"[$SELF:P_end_holiday_min]","[$SELF:P_end_holiday_max]")}|Fr Sa] \
or ([{sunset(0,"[$SELF:P_end_holiday_min]","[$SELF:P_endholiday_max]")}] and Value("NRW_Ferientag"))\
and Value([$SELF:P_master_device]) eq "on" \
and Value([$SELF:P_device]) ne "[$SELF:P_cmd_off]"\
and ([$SELF:P_enabled_holiday,"off"] eq "on" or [$SELF:P_enabled_holiday,"off"] eq "sunset")\
and ([$SELF:P_presence,"no presence"] eq "no presence" \
or [$SELF:P_presence,"no presence"] eq "sunrise" \
or Value([$SELF:P_presence_group]) eq "home"))\
   (set [$SELF:P_device] [$SELF:P_cmd_off,"off"])
attr Timer_Sun_Template checkReadingEvent 1
attr Timer_Sun_Template cmdState on_week|off_week|on_we|off_we
attr Timer_Sun_Template disable 0
attr Timer_Sun_Template do always
attr Timer_Sun_Template event-on-change-reading .*
attr Timer_Sun_Template group Templates
attr Timer_Sun_Template icon helper_doif
attr Timer_Sun_Template room Rule Templates
attr Timer_Sun_Template selftrigger wait
attr Timer_Sun_Template stateFormat {sprintf("master %s, %s: %s, pres: %s (%s), sr-cmd %s, ss-cmd %s, wk: %s (%s-%s, %s-%s), we: %s (%s-%s, %s-%s)",\
ReadingsVal("$name","P_master_device", "???"),\
ReadingsVal("$name","P_device","???"),\
Value(ReadingsVal("$name","P_device","???")),\
ReadingsVal("$name","P_presence","???"),\
ReadingsVal("$name","P_presence_group","???"),\
ReadingsVal("$name","P_cmd_on","???"),\
ReadingsVal("$name","P_cmd_off","???"),\
ReadingsVal("$name","P_enabled","???"),\
ReadingsVal("$name","P_begin_min","???"),\
ReadingsVal("$name","P_begin_max", "???"),\
ReadingsVal("$name","P_end_min", "???"),\
ReadingsVal("$name","P_end_max", "???"),\
ReadingsVal("$name","P_enabled_holiday","???"),\
ReadingsVal("$name","P_begin_holiday_min","???"),\
ReadingsVal("$name","P_begin_holiday_max", "???"),\
ReadingsVal("$name","P_end_holiday_min", "???"),\
ReadingsVal("$name","P_end_holiday_max", "???"),\
ReadingsVal("$name","state", "???"))}
attr Timer_Sun_Template templateName Timer_Sun_Template
attr Timer_Sun_Template uiTable "master device</br>".WID([$SELF:P_master_device],[@"^Master_"])|\
"presence</br>".WID([$SELF:P_presence],"select,no presence,sunrise,sunset,presence")|\
|\
"earliest sunrise"|"latest sunrise"|"earliest sunset"|"latest sunset"|"on/off"\
\
\
"target device</br>".WID([$SELF:P_device],[@"^(Licht_|Rollo_)"])|\
"presence group</br>".WID([$SELF:P_presence_group],[@"^(rgr_|rr_)"])|\
"WK:"|\
WID([$SELF:P_begin_min],"time")|\
WID([$SELF:P_begin_max],"time")|\
WID([$SELF:P_end_min],"time")|\
WID([$SELF:P_end_max],"time")|\
WID([$SELF:P_enabled],"select,off,sunrise,sunset,on")\
\
"sunrise cmd</br>".WID([$SELF:P_cmd_on],"textFieldNL")|\
"sunset cmd</br>".WID([$SELF:P_cmd_off],"textFieldNL")|\
"WE:"|\
WID([$SELF:P_begin_holiday_min],"time")|\
WID([$SELF:P_begin_holiday_max],"time")|\
WID([$SELF:P_end_holiday_min],"time")|\
WID([$SELF:P_end_holiday_max],"time")|\
WID([$SELF:P_enabled_holiday],"select,off,sunrise,sunset,on")
attr Timer_Sun_Template wait 300:1:1:1:1



Wenn ich Änderungen an den Templates vornehmen übernehme ich die Änderungen mittels eines perl-Scripts in alle anderen Devices. Erstes Argument ist der Template-Name, zweites Argument der Name der anderen Devices als Regex:
sub copyTemplate($$) {
my ($source,$target) = @_;
my @devices = devspec2array($target);
my $source_hash = $defs{$source};

exit if (!$source_hash);

my $res = 1;

  foreach my $device (@devices){
Log 1, ("copyTemplate: $source -> $device");

my $target_hash = $defs{$device};

$target_hash->{DEF} = $source_hash->{DEF};
my $eocr = $attr{$source}{"event-on-change-reading"};
fhem("attr $device uiTable $attr{$source}{uiTable}");
fhem("attr $device templateName $attr{$source}{templateName}");
fhem("attr $device stateFormat $attr{$source}{stateFormat}");
fhem("attr $device selftrigger $attr{$source}{selftrigger}");
fhem("attr $device icon $attr{$source}{icon}");
fhem("attr $device do $attr{$source}{do}");
fhem("attr $device cmdState $attr{$source}{cmdState}");
fhem("attr $device checkReadingEvent $attr{$source}{checkReadingEvent}");
fhem("attr $device event-on-change-reading $eocr");

}

return $res;
}


Für neue Rules kopiere ich schlicht die Template.

Viele Grüße
Markus