calview_add_readings

Begonnen von Hans Franz, 04 März 2016, 15:39:28

Vorheriges Thema - Nächstes Thema

Hans Franz

Da ich für meine Zwecke einige zusätzliche Readings  wie Wochentag und Dauer in CALVIEW benötigte:


#99_myUtils
use DateTime;
sub calview_add_readings {
my ($mdev) = @_;
my $readings = $defs{$mdev}->{READINGS};
foreach my $key (keys %{$readings}) {
if ($key =~ /t_\d\d\d_bdate/) {
my $mbdate = $readings->{$key}{VAL};
my $medate = $readings->{substr($key,0,5)."_edate"}{VAL};
my $r1 = substr($key,0,5)."_weekday_start ".mday_of_week($mbdate);
my $r2 = substr($key,0,5)."_weekday_end ".mday_of_week($medate);
my $r3 = substr($key,0,5)."_daysuntil ".mdays_until($mbdate);
my $r4 = substr($key,0,5)."_daysbetween ".mdays_between($mbdate,$medate);
fhem("setreading $mdev $r1");
fhem("setreading $mdev $r2") if ($mbdate ne $medate);
fhem("setreading $mdev $r3");
fhem("setreading $mdev $r4") if ($mbdate ne $medate);
}
if ($key =~ /t_\d\d\d_summary/ and $readings->{$key}{VAL} =~ /^(\d\d)\.(\d\d)\.(\d\d\d\d)/) {
my $mbdate = $readings->{substr($key,0,5)."_bdate"}{VAL};
my $birthdaysummary = $readings->{$key}{VAL};
my $birthday = mdays_between($mbdate,substr($birthdaysummary,0,10));
my $r5 = substr($key,0,5)."_age ".int($birthday/365);
fhem("setreading $mdev $r5");
}
}
}

sub mday_of_week($){
    my ($dow_datum) = @_;
    my ($d,$m,$y) = split(/\./,$dow_datum);
    my $dt = DateTime->new(year => $y, month => $m, day => $d);
    my @weekdays = qw(Tag Montag Dienstag Mittwoch Donnerstag Freitag Samstag Sonntag);
    return @weekdays[$dt->day_of_week()];
}   
sub mdays_until($){
    my ($du_datum) = @_;
    my $dt1 = DateTime->now;
    my ($d,$m,$y) = split(/\./,$du_datum);
    my $dt2 = DateTime->new(year => $y,month => $m,day => $d);
    my $ret = $dt1->delta_days($dt2);
    return $ret->in_units('days');
}
sub mdays_between($$){
    my ($du_datum1,$du_datum2) = @_;
    my ($d1,$m1,$y1) = split(/\./,$du_datum1);
    my $dt1 = DateTime->new(year => $y1,month => $m1,day => $d1);
    my ($d2,$m2,$y2) = split(/\./,$du_datum2);
    my $dt2 = DateTime->new(year => $y2,month => $m2,day => $d2);
    my $ret = $dt1->delta_days($dt2);
    return $ret->in_units('days');
}

Auruf mit: {calview_add_readings("<calview-device>")}
Readings mit _age tauchen nur auf, wenn das _summary-Reading mit einem Datum im TT.MM.JJJJ-Format beginnt.

Notify auf z.B. c_term des CALVIEW-Device (auf:.* ist keine gute Idee  ;) ).

Gruß
Hans
Raspi
CUL, Nano-CUL
FHT8V, FHT80B, S300TH
WM1000WZ, ELRO
LW12, LD382,DS18B20

SirMarco

Magst du einem Anfänger auf die Sprünge helfen?

Einen Kalender habe ich angelegt und wird auch korrekt abgerufen. Erstelle ich eine Programmdatei 99_myUtils.pm und trage dort deine Anweisungen ein?

Gruss
Marco

Hans Franz

Hallo Marco,
Ja,genau. Hier im Wiki ist es besser erklärt als ich es könnte.

Gruß
Hans
Raspi
CUL, Nano-CUL
FHT8V, FHT80B, S300TH
WM1000WZ, ELRO
LW12, LD382,DS18B20

SirMarco

Das habe ich nun zweimal durchgespielt, mir ist noch nicht ganz klar wer was wo hier abruft:

{calview_add_readings("<calview-device>")}

Übernimmt das ein notify?


Hans Franz

Yep. DEF des notify:
(CalViewDevice1|CalViewDevice2):c-term.* {
calview_add_readings($NAME);
}


Gruß
Hans
Raspi
CUL, Nano-CUL
FHT8V, FHT80B, S300TH
WM1000WZ, ELRO
LW12, LD382,DS18B20

SirMarco

Servus Hans

Meine Kenntnisse sind anscheinend doch nicht die Besten. Ich stehe an, vielleicht kannst du helfen. Mein Ziel ist es neben dem Datum auch den Wochentag in der readingsGroup anzuzeigen. Folgendes habe ich gemacht:

Kalender eingefügt: (keine Fehler)

define Gartenkalender Calendar ical url http://owa.mail-ch.ch/owa/calendar/a***********************************************/calendar.ics
attr Gartenkalender hideOlderThan 10
attr Gartenkalender room 02_Beta



Calview: (keine Fehler)

define myCalView CALVIEW Gartenkalender 1
attr myCalView maxreadings 10
attr myCalView modes next
attr myCalView room 02_Beta


List Auszug:
Internals:
   CFGFN
   DEF        Gartenkalender 1
   INTERVAL   43200
   KALENDER   Gartenkalender
   NAME       myCalView
   NR         821
   NTFY_ORDER 50-myCalView
   STATE      t: 2 td: 0 tm: 1
   TYPE       CALVIEW
   Readings:
     2016-04-21 09:48:34   c-term          2
     2016-04-21 09:48:34   c-today         0
     2016-04-21 09:48:34   c-tomorrow      1
     2016-04-21 09:48:34   state           t: 2 td: 0 tm: 1
     2016-04-21 09:48:34   t_001_bdate     22.04.2016
     2016-04-21 09:48:34   t_001_btime     00:00:00
     2016-04-21 09:48:34   t_001_edate     23.04.2016
     2016-04-21 09:48:34   t_001_etime     00:00:00
     2016-04-21 09:48:34   t_001_mode      next
     2016-04-21 09:48:34   t_001_source    Gartenkalender
     2016-04-21 09:48:34   t_001_summary   Gartenabfall
     2016-04-21 09:48:34   t_002_bdate     23.04.2016
     2016-04-21 09:48:34   t_002_btime     00:00:00
     2016-04-21 09:48:34   t_002_edate     24.04.2016
     2016-04-21 09:48:34   t_002_etime     00:00:00
     2016-04-21 09:48:34   t_002_mode      next
     2016-04-21 09:48:34   t_002_source    Gartenkalender
     2016-04-21 09:48:34   t_002_summary   Buchbaum schneiden
     2016-04-21 09:48:34   tomorrow_001_bdate morgen
     2016-04-21 09:48:34   tomorrow_001_btime 00:00:00
     2016-04-21 09:48:34   tomorrow_001_edate 23.04.2016
     2016-04-21 09:48:34   tomorrow_001_etime 00:00:00
     2016-04-21 09:48:34   tomorrow_001_mode next
     2016-04-21 09:48:34   tomorrow_001_source Gartenkalender
     2016-04-21 09:48:34   tomorrow_001_summary Gartenabfall
Attributes:
   maxreadings 10
   modes      next
   room       02_Beta





readingsGroup erstellt: Dieses habe ich nach Beispiel von dem Beitrag Modul für Terminansicht CALVIEW https://forum.fhem.de/index.php/topic,19922.0.html

define rg_myCalView readingsGroup <Datum>,<Zeit>,<Termin>,<Ort>,<Wochentag>
attr rg_myCalView nonames 1
attr rg_myCalView room 02_Beta





Und ich denke hier stehe ich an. Das notify füllt die readingGroup mit den Readings aus CALVIEW? Richtig?
Hier habe ich das Problem das notify zu verstehen, das WIKI bringt mich auch grad nicht weiter. Wo ist der Bezug zu deiner 99_myUtils.pm? Meine 99_myUtils.pm sieht so aus: (Wurde ohne Fehler gespeichert)

##############################################
# $Id: myUtilsTemplate.pm 7570 2015-01-14 18:31:44Z rudolfkoenig $
#
# Save this file as 99_myUtils.pm, and create your own functions in the new
# file. They are then available in every Perl expression.

package main;

use strict;
use warnings;
use POSIX;

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

# Enter you functions below _this_ line.
use DateTime;
sub calview_add_readings {
my ($mdev) = @_;
my $readings = $defs{$mdev}->{READINGS};
foreach my $key (keys %{$readings}) {
if ($key =~ /t_\d\d\d_bdate/) {
my $mbdate = $readings->{$key}{VAL};
my $medate = $readings->{substr($key,0,5)."_edate"}{VAL};
my $r1 = substr($key,0,5)."_weekday_start ".mday_of_week($mbdate);
my $r2 = substr($key,0,5)."_weekday_end ".mday_of_week($medate);
my $r3 = substr($key,0,5)."_daysuntil ".mdays_until($mbdate);
my $r4 = substr($key,0,5)."_daysbetween ".mdays_between($mbdate,$medate);
fhem("setreading $mdev $r1");
fhem("setreading $mdev $r2") if ($mbdate ne $medate);
fhem("setreading $mdev $r3");
fhem("setreading $mdev $r4") if ($mbdate ne $medate);
}
if ($key =~ /t_\d\d\d_summary/ and $readings->{$key}{VAL} =~ /^(\d\d)\.(\d\d)\.(\d\d\d\d)/) {
my $mbdate = $readings->{substr($key,0,5)."_bdate"}{VAL};
my $birthdaysummary = $readings->{$key}{VAL};
my $birthday = mdays_between($mbdate,substr($birthdaysummary,0,10));
my $r5 = substr($key,0,5)."_age ".int($birthday/365);
fhem("setreading $mdev $r5");
}
}
}

sub mday_of_week($){
    my ($dow_datum) = @_;
    my ($d,$m,$y) = split(/\./,$dow_datum);
    my $dt = DateTime->new(year => $y, month => $m, day => $d);
    my @weekdays = qw(Tag Montag Dienstag Mittwoch Donnerstag Freitag Samstag Sonntag);
    return @weekdays[$dt->day_of_week()];
}   
sub mdays_until($){
    my ($du_datum) = @_;
    my $dt1 = DateTime->now;
    my ($d,$m,$y) = split(/\./,$du_datum);
    my $dt2 = DateTime->new(year => $y,month => $m,day => $d);
    my $ret = $dt1->delta_days($dt2);
    return $ret->in_units('days');
}
sub mdays_between($$){
    my ($du_datum1,$du_datum2) = @_;
    my ($d1,$m1,$y1) = split(/\./,$du_datum1);
    my $dt1 = DateTime->new(year => $y1,month => $m1,day => $d1);
    my ($d2,$m2,$y2) = split(/\./,$du_datum2);
    my $dt2 = DateTime->new(year => $y2,month => $m2,day => $d2);
    my $ret = $dt1->delta_days($dt2);
    return $ret->in_units('days');
}
1;



Notify:

define ntf_myCalView notify (myCalView):c-term.*  { calview_add_readings(myCalView);
}
attr ntf_myCalView room 02_Beta


Der Log sagt folgendes beim starten:
ntf_myCalView return value: Unknown command {, try help.

list myCalView:


Internals:
   DEF        (myCalView):c-term.*  { calview_add_readings(myCalView)
   NAME       ntf_myCalView
   NR         99
   NTFY_ORDER 50-ntf_myCalView
   REGEXP     (myCalView):c-term.*
   STATE      2016-04-21 10:15:07
   TYPE       notify
   Readings:
     2016-04-21 10:09:40   state           active
Attributes:
   room       02_Beta


Magst Du mir Auszüge deiner fhem.cfg zur Verfügung stellen um es besser zu verstehen und meinen Fehler zu finden?

Da wäre mir sehr geholfen

Dank Dir

Gruss
Marco

Hans Franz

#6
Ändere 'mal die DEF des notify in
myCalView:c-term.*  { calview_add_readings("myCalView")}
oder:
myCalView:c-term.*  { calview_add_readings($NAME)}
Die Klammer ist jetzt überflüssig und der explizite Devicename muss in Anführungszeichen. Besser also: $NAME
und dann noch:
set myCalView update
in die Eingabezeile

Jetzt sollten die Readings auftauchen.

Gruß
Hans
Edit:
Noch die DEF der ReadingsGroup des Ferienkalenders:
NRW_Ferien_RG:!noch:,<von:>,<>,<bis:>,<>,<>,<Dauer:>,<>
CalView_Ferien:t_001_daysuntil,t_001_weekday_start,t_001_bdate,t_001_weekday_end,t_001_edate,t_001_summary,t_001_daysbetween
CalView_Ferien:t_002_daysuntil,t_002_weekday_start,t_002_bdate,t_002_weekday_end,t_002_edate,t_002_summary,t_002_daysbetween,$sum(1..3:7)
CalView_Ferien:t_003_daysuntil,t_003_weekday_start,t_003_bdate,t_003_weekday_end,t_003_edate,t_003_summary,t_003_daysbetween,$sum(1..4:7)
CalView_Ferien:t_004_daysuntil,t_004_weekday_start,t_004_bdate,t_004_weekday_end,t_004_edate,t_004_summary,t_004_daysbetween,$sum(1..5:7)
CalView_Ferien:t_005_daysuntil,t_005_weekday_start,t_005_bdate,t_005_weekday_end,t_005_edate,t_005_summary,t_005_daysbetween,$sum(1..6:7)
Raspi
CUL, Nano-CUL
FHT8V, FHT80B, S300TH
WM1000WZ, ELRO
LW12, LD382,DS18B20

SirMarco

Herzlichen Dank!

notify angepasst:
Internals:
   CFGFN
   DEF        myCalView:c-term.* {calview_add_readings("myCalView")}
   NAME       ntf_myCalView
   NOTIFYDEV  myCalView
   NR         164
   NTFY_ORDER 50-ntf_myCalView
   REGEXP     myCalView:c-term.*
   STATE      2016-04-21 12:16:03
   TYPE       notify
   Readings:
     2016-04-21 12:07:44   state           active
Attributes:
   room       02_Beta


Internals:
   DEF        Gartenkalender 1
   INTERVAL   43200
   KALENDER   Gartenkalender
   NAME       myCalView
   NR         97
   NTFY_ORDER 50-myCalView
   STATE      t: 3 td: 0 tm: 1
   TYPE       CALVIEW
   Readings:
     2016-04-21 12:16:03   c-term          3
     2016-04-21 12:16:03   c-today         0
     2016-04-21 12:16:03   c-tomorrow      1
     2016-04-21 12:16:03   state           t: 3 td: 0 tm: 1
     2016-04-21 12:16:03   t_001_bdate     22.04.2016
     2016-04-21 12:16:03   t_001_btime     00:00:00
     2016-04-21 12:16:03   t_001_daysbetween 1
     2016-04-21 12:16:03   t_001_daysuntil 1
     2016-04-21 12:16:03   t_001_edate     23.04.2016
     2016-04-21 12:16:03   t_001_etime     00:00:00
     2016-04-21 12:16:03   t_001_mode      next
     2016-04-21 12:16:03   t_001_source    Gartenkalender
     2016-04-21 12:16:03   t_001_summary   Gartenabfall
     2016-04-21 12:16:03   t_001_weekday_end Samstag
     2016-04-21 12:16:03   t_001_weekday_start Freitag
     2016-04-21 12:16:03   t_002_bdate     23.04.2016
     2016-04-21 12:16:03   t_002_btime     00:00:00
     2016-04-21 12:16:03   t_002_daysbetween 1
     2016-04-21 12:16:03   t_002_daysuntil 2
     2016-04-21 12:16:03   t_002_edate     24.04.2016
     2016-04-21 12:16:03   t_002_etime     00:00:00
     2016-04-21 12:16:03   t_002_mode      next
     2016-04-21 12:16:03   t_002_source    Gartenkalender
     2016-04-21 12:16:03   t_002_summary   Buchbaum schneiden
     2016-04-21 12:16:03   t_002_weekday_end Sonntag
     2016-04-21 12:16:03   t_002_weekday_start Samstag
     2016-04-21 12:16:03   t_003_bdate     24.04.2016
     2016-04-21 12:16:03   t_003_btime     12:30:00
     2016-04-21 12:16:03   t_003_daysuntil 3
     2016-04-21 12:16:03   t_003_edate     24.04.2016
     2016-04-21 12:16:03   t_003_etime     13:30:00
     2016-04-21 12:16:03   t_003_mode      next
     2016-04-21 12:16:03   t_003_source    Gartenkalender
     2016-04-21 12:16:03   t_003_summary   Test Termin
     2016-04-21 12:16:03   t_003_weekday_start Sonntag
     2016-04-21 12:16:03   tomorrow_001_bdate morgen
     2016-04-21 12:16:03   tomorrow_001_btime 00:00:00
     2016-04-21 12:16:03   tomorrow_001_edate 23.04.2016
     2016-04-21 12:16:03   tomorrow_001_etime 00:00:00
     2016-04-21 12:16:03   tomorrow_001_mode next
     2016-04-21 12:16:03   tomorrow_001_source Gartenkalender
     2016-04-21 12:16:03   tomorrow_001_summary Gartenabfall
Attributes:
   maxreadings 10
   modes      next
   room       02_Beta


Eins habe ich noch:
Wie bekomme ich hin das die readingsGroup "dynamisch" ist? WEnn ich zum testen deine DEF's übernehme sieht es so aus:

<von:>,<>,<bis:>,<>,<>,<Dauer:>,<>
myCalView:t_001_daysuntil,t_001_weekday_start,t_001_bdate,t_001_weekday_end,t_001_edate,t_001_summary,t_001_daysbetween
myCalView:t_002_daysuntil,t_002_weekday_start,t_002_bdate,t_002_weekday_end,t_002_edate,t_002_summary,t_002_daysbetween,$sum(1..3:7)
myCalView:t_003_daysuntil,t_003_weekday_start,t_003_bdate,t_003_weekday_end,t_003_edate,t_003_summary,t_003_daysbetween,$sum(1..4:7)
myCalView:t_004_daysuntil,t_004_weekday_start,t_004_bdate,t_004_weekday_end,t_004_edate,t_004_summary,t_004_daysbetween,$sum(1..5:7)
myCalView:t_005_daysuntil,t_005_weekday_start,t_005_bdate,t_005_weekday_end,t_005_edate,t_005_summary,t_005_daysbetween,$sum(1..6:7)


(https://picload.org/image/rgdriild/rg_mycalview.jpg)

Momentan sind nur 3 Termine im Kalender

Danke vielmal

Gruss
Marco

Hans Franz

Dynamisch wird die ReadingsGroup mit dem von dir genannten Notify von Christian. Habe ich aber nie benutzt.
c_term wird wohl als maximale Zeilenzahl (for-Schleife) genommen, um die DEF der ReadingsGroup zu modifizieren. Müsste ich mir anschauen. Bekommst du aber sicher bis dahin selber hin ;)

Gruß
Hans
Raspi
CUL, Nano-CUL
FHT8V, FHT80B, S300TH
WM1000WZ, ELRO
LW12, LD382,DS18B20

SirMarco

#9
Ich früher?   8)

Ne das werde ich nicht schaffen, dafür fehlt doch etwas. Schade das es nicht komplett funktioniert. Finde es mit dem Wochentag einfach noch übersichtlicher

Dank dir

Gruss
Marco

Edit:

einen Teil habe ich doch schon geschafft:

der Wochentag wird angezeigt mit folgendem notify:
myCalView:c-term.* {calview_add_readings("$NAME")}
{
my $i;
my $modtext = "<Datum>,<Zeit>,<Termin>,<Ort>,<Wochentag> ";

for($i= 1;$i<= ReadingsVal("myCalView","c-term", 0);$i++){
$modtext .= "myCalView:t_".sprintf('%03d',$i)."_bdate,t_".sprintf('%03d',$i)."_btime,t_".sprintf('%03d',$i)."_summary,t_".sprintf('%03d',$i)."_location,t_".sprintf('%03d',$i)."_weekday_start";}

fhem("modify rg_myCalView $modtext");
}


myCalView:c-term.* {calview_add_readings("$NAME")}
{
my $i;
my $modtext = "<Datum>,<Zeit>,<Termin>,<Ort>,<Wochentag> ";

for($i= 1;$i<= ReadingsVal("myCalView","c-term", 0);$i++){
$modtext .= "myCalView:t_".sprintf('%03d',$i)."_bdate,t_".sprintf('%03d',$i)."_btime,t_".sprintf('%03d',$i)."_summary,t_".sprintf('%03d',$i)."_location,t_".sprintf('%03d',$i)."_weekday_start";}

fhem("modify rg_myCalView $modtext");
}




Jetzt wäre nur noch schon wenn die erste Spalte mit myCalView ausgeblendet wird

(https://picload.org/image/rgdrwdpo/rg_mycalview_1.jpg)

Hans Franz

Hey, sieht doch gut aus. Was funktioniert denn nicht?
attr  rg_myCalView nonames 1
Und die erste Spalte ist weg. Die Überschrift (Termin oder Ort) muß dann auch noch angespasst werden.

Gruß
Hans
Raspi
CUL, Nano-CUL
FHT8V, FHT80B, S300TH
WM1000WZ, ELRO
LW12, LD382,DS18B20

SirMarco

Es funktioniert!

Du bist mein Retter...!  :) Nun noch das ganze formatieren


Herzlichen Dank!

Gruss
Marco