Kalendereintrag gezielt (und formatiert) auslesen

Begonnen von KernSani, 01 Oktober 2016, 00:46:13

Vorheriges Thema - Nächstes Thema

KernSani

Hi zusammen,

aufgrund dieser Anregung im Forum habe ich eine kleine Routine gebastelt, mit der man gezielt Kalender-Einträge des Calendar Moduls nach eigenen Wünschen formatiert auslesen kann.

Details zur Benutzung stehen unten, als Beispiel:
{myUtils_getCalentry("AbfallKal", 5}
gibt mir den 5. "upcoming" Eintrag meines Abfallkalenders zurück, also etwa:
10.10.16 00:00 Bio
ein Aufruf wie dieser,
{myUtils_getCalentry("TerminKal", 5, "<b>%startdate%</b>: %starttime%-%endtime% - %summary%")}
gibt mir so etwas zurück:
<b>10.10.2016</b>: 08:00:00-09:00:00 - Zahnarzt


##############################################################################
# returns a formatted entry from Calendar module
##############################################################################
# usage: myUtils_getCalentry(<Devicename>, <Seq>, [<format>])
# <Seq> is the calender entry (i.e. 5 would give you the 5th entry)
# <format> is optional and can contain any of the format constants of the calendar module (like uid, full, ... ) default is text.
# <format> additionally supports startdate, starttime, enddate and endtime
# Example: {myUtils_getCalentry("AbfallKal", 5, "<b>%start% - %end%</b>: %summary%")}
# Example: {myUtils_getCalentry("TerminKal", 5, "<b>%startdate%</b>: %starttime%-%endtime% - %summary%")}

sub myUtils_getCalentry($$@) {
my($name,$seq,@format) = @_;
my $format = $format[0];

# set default format if not given
if (!$format) {
$format = "%text%";
}

#get the variables
my @vars = ( $format =~ /%(.+?)%/g );

# get the Calendar
my $cal = fhem("get $name uid next $seq");
my @lines = split /\s/,$cal;
# get the variable content and replace $format string
foreach my $v (@vars) {
my $s = $v;
if ($v ~~ ["starttime","startdate"]) {$s = "start"}
elsif ($v ~~ ["endtime","enddate"]) {$s = "end"}

my $entry = fhem("get $name $s ".$lines[$seq-1]);

if ($v ~~ ["starttime","endtime"]) {$entry = substr($entry,11,8)}
elsif ($v ~~ ["startdate","enddate"]) {$entry = substr($entry,0,10)}

$format =~ s/%$v%/$entry/g;
}

return $format;
}
RasPi: RFXTRX, HM, zigbee2mqtt, mySensors, JeeLink, miLight, squeezbox, Alexa, Siri, ...