Ankündigung

Einklappen
Keine Ankündigung bisher.

Item mit Zeitberechnung bspw für Verbrauchsauswertung als Systemitems bereitstellen

Einklappen
X
 
  • Filter
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge

    [Featurewunsch] Item mit Zeitberechnung bspw für Verbrauchsauswertung als Systemitems bereitstellen

    Hallo,

    für die zeitliche Auswertung u.a. von Verbrauch verwende ich eine Logik, die mit folgendes Items "füllt":
    Code:
    %YAML 1.1
    ---
    time:
        date:
            current_month:
                type: foo
            last_month:
                type: foo
            last_month_but_one:
                type: foo
            last_month_but_two:
                type: foo
        year:
            current:
                type: num
        month:
            since:
                year:
                    type: num
                    dbstr:
                        type: str
                        eval_trigger: time.month.since.year
                        eval: str(sh.time.month.since.year()) + 'm'
            until:
                year:
                    type: num
                    dbstr:
                        type: str
                        eval_trigger: time.month.until.year
                        eval: str(sh.time.month.until.year()) + 'm'
            list:
                type: str
                value: Januar Februar März April Mai Juni Juli August September Oktober November Dezember
                visu_acl: r
            current:
                type: num
                cache: yes
                current_name:
                    type: str
                    eval: sh.time.month.list().split()[int(sh.time.month.current() - 1)]
                    eval_trigger: time.month.current
                    cache: yes
        week:
            since:
                month:
                    type: num
                year:
                    type: num
            until:
                month:
                    type: num
                year:
                    type: num
        day:
            since:
                week:
                    type: num
                    dbstr:
                        type: str
                        eval_trigger: time.day.since.week
                        eval: str(sh.time.day.since.week()) + 'd'
                month:
                    type: num
                    dbstr:
                        type: str
                        eval_trigger: time.day.since.month
                        eval: str(sh.time.day.since.month()) + 'd'
                year:
                    type: num
                    dbstr:
                        type: str
                        eval_trigger: time.day.since.year
                        eval: str(sh.time.day.since.year()) + 'd'
            until:
                week:
                    type: num
                    dbstr:
                        type: str
                        eval_trigger: time.day.since.week
                        eval: str(sh.time.day.since.week()) + 'd'
                month:
                    type: num
                    dbstr:
                        type: str
                        eval_trigger: time.day.until.month
                        eval: str(sh.time.day.until.month()) + 'd'
                year:
                    type: num
                    dbstr:
                        type: str
                        eval_trigger: time.day.until.year
                        eval: str(sh.time.day.until.year()) + 'd'
        hour:
            since:
                midnight:
                    type: num
                    cache: yes
                    dbstr:
                        type: str
                        cache: yes
                        eval_trigger: time.hour.since.midnight
                        eval: str(sh.time.hour.since.midnight()) + 'h'
                week:
                    type: num
                    dbstr:
                        type: str
                        cache: yes
                        eval_trigger: time.hour.since.week
                        eval: str(sh.time.hour.since.week()) + 'h'
                month:
                    type: num
                    cache: yes
                    dbstr:
                        type: str
                        cache: yes
                        eval_trigger: time.hour.since.month
                        eval: str(sh.time.hour.since.month()) + 'h'
                year:
                    type: num
                    cache: yes
                    dbstr:
                        type: str
                        cache: yes
                        eval_trigger: time.hour.since.year
                        eval: str(sh.time.hour.since.year()) + 'h'
            until:
                midnight:
                    type: num
                    dbstr:
                        type: str
                        eval_trigger: time.hour.until.midnight
                        eval: str(sh.time.hour.until.midnight()) + 'h'
                week:
                    type: num
                    dbstr:
                        type: str
                        eval_trigger: time.hour.until.week
                        eval: str(sh.time.hour.until.week()) + 'h'
                month:
                    type: num
                    dbstr:
                        type: str
                        eval_trigger: time.hour.until.month
                        eval: str(sh.time.hour.until.month()) + 'h'
                year:
                    type: num
                    dbstr:
                        type: str
                        eval_trigger: time.hour.until.year
                        eval: str(sh.time.hour.until.year()) + 'h'
        minute:
            since:
                hour:
                    type: num
                    dbstr:
                        type: str
                        eval_trigger: time.minute.since.hour
                        eval: str(sh.time.minute.since.hour()) + 'i'
                midnight:
                    type: num
                    cache: yes
                    dbstr:
                        type: str
                        cache: yes
                        eval_trigger: time.minute.since.midnight
                        eval: str(sh.time.minute.since.midnight()) + 'i'
                week:
                    type: num
                    cache: yes
                    dbstr:
                        type: str
                        cache: yes
                        eval_trigger: time.minute.since.week
                        eval: str(sh.time.minute.since.week()) + 'i'
                month:
                    type: num
                    cache: yes
                    dbstr:
                        type: str
                        cache: yes
                        eval_trigger: time.minute.since.month
                        eval: str(sh.time.minute.since.month()) + 'i'
                year:
                    type: num
                    cache: yes
                    dbstr:
                        type: str
                        cache: yes
                        eval_trigger: time.minute.since.year
                        eval: str(sh.time.minute.since.year()) + 'i'
            until:
                hour:
                    type: num
                    dbstr:
                        type: str
                        eval_trigger: time.minute.until.hour
                        eval: str(sh.time.minute.until.hour()) + 'i'
                midnight:
                    type: num
                    dbstr:
                        type: str
                        eval_trigger: time.minute.until.midnight
                        eval: str(sh.time.minute.until.midnight()) + 'i'
                week:
                    type: num
                    dbstr:
                        type: str
                        eval_trigger: time.minute.until.week
                        eval: str(sh.time.minute.until.week()) + 'i'
                month:
                    type: num
                    dbstr:
                        type: str
                        eval_trigger: time.minute.until.month
                        eval: str(sh.time.minute.until.month()) + 'i'
                year:
                    type: num
                    dbstr:
                        type: str
                        eval_trigger: time.minute.until.year
                        eval: str(sh.time.minute.until.year()) + 'i'
        second:
            since:
                minute:
                    type: num
                hour:
                    type: num
                midnight:
                    type: num
                week:
                    type: num
                month:
                    type: num
                year:
                    type: num
            until:
                minute:
                    type: num
                hour:
                    type: num
                midnight:
                    type: num
                week:
                    type: num
                month:
                    type: num
                year:
                    type: num
    Die Logik selbst:
    Code:
    #/logics/zeit.py
    #!/usr/bin/env python
    
    from dateutil.relativedelta import relativedelta
    
    sh_now = sh.now()
    debug = False
    
    # Funktionen                    
    
    #def leap_year(year):
    #    if (year % 400 == 0) or ((year % 4 == 0) and not (year % 100 == 0)):
    #        return True
    #    else:
    #        return False
    
    def days_of_month(month, year):
        if month in [1, 3, 5, 7, 8, 10, 12]:
            days = 31
        elif month in [4, 6, 9, 11]:
            days = 30
        elif (year % 400 == 0) or ((year % 4 == 0) and not (year % 100 == 0)):  #Schaltjahr
            days = 29
        else:
            days = 28
        return days
    
    def days_of_year(year):
        period_end = datetime.datetime(year,12,31)
        days_of_year = (period_end - datetime.datetime(period_end.year, 1, 1)).days + 1
        return(days_of_year)
    
    def day_of_year(year,month,day):
        period_end = datetime.datetime(year,month,day)
        day_of_year = (period_end - datetime.datetime(period_end.year, 1, 1)).days + 1
        return(day_of_year)
    
    if debug == True:
        print("RUNNING LOGIC OF TIME - REMOVE AFTER DEBUG")
        print(sh_now.hour) #Stunde
        print(sh_now.minute) #Minute
        print(sh_now.second) #Sekunde
        print(sh_now.day)  #Tag
        print(sh_now.month) #Monat
        print(sh_now.isoweekday()) #Wochentag
        print(sh.now().isocalendar()[1]) #Kalenderwoche
        
    # Sekunde/Minute
    sh.time.second.since.minute(sh_now.second)
    sh.time.second.until.minute(60 - sh_now.second - 1)
    
    # Minute/Stunde
    sh.time.minute.since.hour(sh_now.minute)
    sh.time.minute.until.hour(60 - sh_now.minute - 1)
    
    # Stunde/Tag
    sh.time.hour.since.midnight(sh_now.hour)
    sh.time.hour.until.midnight(24 - sh_now.hour - 1)
    
    # Tag/Woche
    sh.time.day.since.week(sh_now.isoweekday())
    sh.time.day.until.week(7 - sh_now.isoweekday())
    
    # Stunde/Woche
    sh.time.hour.since.week(sh.time.hour.since.midnight() + (24 * (sh.time.day.since.week() - 1)))
    sh.time.hour.until.week(sh.time.hour.until.midnight() + (24 * sh.time.day.until.week()))
    
    # Kalenderwoche/Jahr
    sh.time.week.since.year(sh.now().isocalendar()[1])
    
    # Monat/Jahr
    sh.time.month.since.year(sh_now.month)
    sh.time.month.until.year(12-sh_now.month)
    
    # Sekunde/Stunde
    sh.time.second.since.hour(sh.time.second.since.minute() + (60 * sh.time.minute.since.hour()))
    sh.time.second.until.hour(sh.time.second.until.minute() + (60 * sh.time.minute.until.hour()))
    
    # Sekunde/Tag
    sh.time.second.since.midnight(sh.time.second.since.minute() + (3600 * sh.time.hour.since.midnight()))
    sh.time.second.until.midnight(sh.time.second.until.minute() + (3600 * sh.time.hour.until.midnight()))
    
    # Minute/Tag
    sh.time.minute.since.midnight(sh.time.minute.since.hour() + (60 * sh.time.hour.since.midnight()))
    sh.time.minute.until.midnight(sh.time.minute.until.hour() + (60 * sh.time.hour.until.midnight()))
    
    # Minute/Woche
    sh.time.minute.since.week(sh.time.minute.since.hour() + (60 * sh.time.hour.since.week()))
    sh.time.minute.until.week(sh.time.minute.until.hour() + (60 * sh.time.hour.until.week()))
    
    # Sekunde/Woche
    sh.time.second.since.week(sh.time.second.since.minute() + (60 * sh.time.minute.since.week()))
    sh.time.second.until.week(sh.time.second.until.minute() + (60 * sh.time.minute.until.week()))
    
    # Tage/Monat
    sh.time.day.since.month(sh_now.day - 1)
    sh.time.day.until.month(days_of_month(sh_now.month,sh_now.year) - sh.time.day.since.month() - 1)
    
    # Tage/Jahr
    sh.time.day.since.year(day_of_year(sh_now.year,sh_now.month,sh_now.day) - 1)
    sh.time.day.until.year(days_of_year(sh_now.year) - sh.time.day.since.year() - 1)
    
    # Stunde/Monat
    sh.time.hour.since.month((24 * sh.time.day.since.month()) + sh.time.hour.since.midnight())
    sh.time.hour.until.month((24 * days_of_month(sh_now.month,sh_now.year)) - sh.time.hour.since.month() - 1)
    
    # Stunde/Jahr
    sh.time.hour.since.year((24 * sh.time.day.since.year()) + sh.time.hour.since.midnight())
    sh.time.hour.until.year((24 * days_of_year(sh_now.year)) - sh.time.hour.since.year() - 1)
    
    # Minute/Monat
    sh.time.minute.since.month((60 * sh.time.hour.since.month()) + sh.time.minute.since.hour())
    sh.time.minute.until.month(sh.time.minute.since.month() - (60 * sh.time.hour.until.month()) - 1)
    
    # Minute/Jahr
    sh.time.minute.since.year((60 * sh.time.hour.since.year()) + sh.time.minute.since.hour())
    sh.time.minute.until.year((60 * sh.time.hour.until.year()) + sh.time.minute.until.hour())
    
    # Sekunde/Monat
    sh.time.second.since.month((60 * sh.time.minute.since.month()) + sh.time.second.since.minute())
    sh.time.second.until.month((60 * sh.time.minute.until.month()) + sh.time.second.until.minute())
    
    # Sekunde/Jahr
    sh.time.second.since.year((60 * sh.time.minute.since.year()) + sh.time.second.since.minute())
    sh.time.second.until.year((60 * sh.time.minute.until.year()) + sh.time.second.until.minute())
    
    # Aktueller Monat
    sh.time.month.current(sh_now.month)
    
    # Aktuelles Jahr
    sh.time.year.current(sh_now.year)
    
    # Aktuelles Monat/Jahr
    today_date = datetime.date.today()
    sh.time.date.current_month(today_date.strftime("%m/%Y"))
    
    # Letzter Monat/Jahr
    last_month = today_date - relativedelta(months=1)
    sh.time.date.last_month(last_month.strftime("%m/%Y"))
    
    # Vorletzter Monat/Jahr
    last_month_but_one = today_date - relativedelta(months=2)
    sh.time.date.last_month_but_one(last_month_but_one.strftime("%m/%Y"))
    
    # VorVorletzter Monat/Jahr
    last_month_but_two = today_date - relativedelta(months=3)
    sh.time.date.last_month_but_two(last_month_but_two.strftime("%m/%Y"))
    Damit kann man dann Datenbankabfragen für gestern, vorgestern, letzten Monat etc mache.
    bspw. so.

    Code:
        gestern:
            type: num
            visu_acl: ro
            eval: round((sh...db('max', sh.time.minute.since.midnight.dbstr(), sh.time.minute.since.midnight.dbstr()) - sh...db('max', str(sh.time.minute.since.midnight() + 1440) + 'i', str(sh.time.minute.since.midnight() + 1440) + 'i')), 2)
            crontab: 1 0 * * = 1
            database: init
    Meine Frage ist, ob es sinnvoll wäre, diese Items als Systemitems oder als Zeitmodul zur Verfügung zu stellen?
    Was meint ihr? Msinn bmx psilo

    #2
    Ich glaube Msinn hat im neuen Release einen (sinnvollen) Teil davon als API Funktion inkludiert
    Zuletzt geändert von psilo; 01.03.2020, 13:27.

    Kommentar


      #3
      https://github.com/smarthomeNG/smart...shtime.py#L309

      Kommentar

      Lädt...
      X