Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. Skript - automatisch Eintrag im Google Kalender machen

    NEWS

    • Monatsrückblick – September 2025

    • Neues Video "KI im Smart Home" - ioBroker plus n8n

    • Neues Video über Aliase, virtuelle Geräte und Kategorien

    Skript - automatisch Eintrag im Google Kalender machen

    This topic has been deleted. Only users with topic management privileges can see it.
    • Atifan
      Atifan last edited by

      Hi Leute ich möchte folgendes mit Hilfe von Blockly oder NodeRed programmieren, weiss aber nicht genau wie ich das umsetzen soll.
      Evtl. hat hier einer ja ne Idee wie man das machen könnte.

      Ich möchte einen Xiaomi Zigbee Button dafür nutzen, dass wenn dieser einmal gedrückt wird, automatisch ein Eintrag in einem Google-Kalender gemacht wird.

      Ich drücke also am Tag einmal oder auch mehrmals den Button -> im IoBroker gibts den Datenpunkt "Button gedrückt ja" -> dieser Datenpunkt wird in dem Moment "true".
      Wenn das der Fall ist soll Iobroker automatisch in den Google Kalender für den "heutigen Tag" einen Eintrag machen "Button wurde heute gedrückt".

      Das ganze soll als Erinnerungsstütze dienen, so kann man immer in den Kalender schauen ob man heute schon das eine gemacht hat oder nicht.

      Hat einer ne Idee wie ich das Umsetzen kann? Ich würde so vorgehen, über Blockly einen Datenpunkt überwachen ("Button gedrückt"), und wenn der sich ändert mache "xxxx -> Daten in Kalender schreiben". Aber da weiss ich nicht wie ich das umsetzen soll.

      Habe z.B. den Adapter "ICal Kalender" installiert, falls man den dafür nutzen könnte.
      Bin für Tipps dankebar 🙂

      Grüße
      atifan

      Atifan 1 Reply Last reply Reply Quote 0
      • Atifan
        Atifan @Atifan last edited by Atifan

        Hi, ich hab mal ChatGPT gefragt und das hat mir ein Script erstellt, das ist echt mal Megagenial 🙂
        Allerdings geht schreiben nur in einen Nextcloud Kalender und nicht in den Google Kalender.
        Um in den Google Kalender zu schreiben benötigt man wohl einen Google Cloud Account und Zugriff auf die Google API, was aber kostenpflichtig ist.
        Falls einer das Script möchte, hier ist es.
        Es überwacht den Datenpunkt "0_userdata.0.Button" vom Typ Boolean. Wenn dieser "true" wird, wird ein Eintrag im Kalender gemacht.
        Es wird auch geprüft ob bereits ein Eintrag gemacht wurde, wenn das der Fall ist wird kein zweiter Eintrag gemacht.
        Im Script müssen angepasst werden: calendarUrl, username, password. Und der Eintrag den man machen möchte.
        In meinem Beispiel ist das der Text "Medikamente heute eingenommen".

        const http = require('http');
        const https = require('https');
        const url = require('url');
        
        const calendarUrl = '<ANPASSEN>';
        const username    = '<ANPASSEN>';
        const password    = '<ANPASSEN>';
        const CREATE_ON_CHECK_ERROR = false;
        
        function pad(n){ return n < 10 ? '0' + n : '' + n; }
        function caldavDateUTC(d){
            return d.getUTCFullYear().toString() +
                pad(d.getUTCMonth() + 1) +
                pad(d.getUTCDate()) + 'T' +
                pad(d.getUTCHours()) +
                pad(d.getUTCMinutes()) +
                pad(d.getUTCSeconds()) + 'Z';
        }
        function localDateNoZ(d){
            return d.getFullYear().toString() +
                pad(d.getMonth() + 1) +
                pad(d.getDate()) + 'T' +
                pad(d.getHours()) +
                pad(d.getMinutes()) +
                pad(d.getSeconds());
        }
        
        // Allgemeine HTTP-Request-Funktion
        function httpRequest(options, body, callback) {
            const parsedUrl = url.parse(options.url);
            const lib = parsedUrl.protocol === 'https:' ? https : http;
        
            const auth = Buffer.from(options.auth || '').toString('base64');
        
            const reqOptions = {
                hostname: parsedUrl.hostname,
                port: parsedUrl.port || (parsedUrl.protocol === 'https:' ? 443 : 80),
                path: parsedUrl.path,
                method: options.method || 'GET',
                headers: options.headers || {}
            };
        
            if (auth) reqOptions.headers['Authorization'] = 'Basic ' + auth;
        
            const req = lib.request(reqOptions, (res) => {
                let data = '';
                res.on('data', (chunk) => { data += chunk; });
                res.on('end', () => { callback(null, res, data); });
            });
        
            req.on('error', (err) => { callback(err); });
        
            if (body) req.write(body);
            req.end();
        }
        
        // Prüfen, ob heute schon ein Termin existiert
        function checkEventToday(callback) {
            const now = new Date();
            const localStart = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 0,0,0);
            const localEnd   = new Date(now.getFullYear(), now.getMonth(), now.getDate()+1, 0,0,0);
        
            const timeMin = caldavDateUTC(localStart);
            const timeMax = caldavDateUTC(localEnd);
        
            const xml = '<?xml version="1.0" encoding="utf-8"?>' +
        '<C:calendar-query xmlns:D="DAV:" xmlns:C="urn:ietf:params:xml:ns:caldav">' +
        '  <D:prop>' +
        '    <D:getetag/>' +
        '    <C:calendar-data/>' +
        '  </D:prop>' +
        '  <C:filter>' +
        '    <C:comp-filter name="VCALENDAR">' +
        '      <C:comp-filter name="VEVENT">' +
        '        <C:time-range start="' + timeMin + '" end="' + timeMax + '"/>' +
        '      </C:comp-filter>' +
        '    </C:comp-filter>' +
        '  </C:filter>' +
        '</C:calendar-query>';
        
            httpRequest({
                url: calendarUrl,
                method: 'REPORT',
                auth: username + ':' + password,
                headers: { 'Content-Type': 'application/xml', 'Depth': '1' }
            }, xml, function(error, response, body) {
                if (error) {
                    log("❌ Fehler beim Prüfen der Events: " + error, 'error');
                    return callback(null, CREATE_ON_CHECK_ERROR ? false : true);
                }
        
                const status = response ? response.statusCode : 'no-response';
                log("DEBUG: REPORT Status: " + status, 'debug');
        
                if (status < 200 || status >= 300) {
                    log("⚠️ Ungewöhnlicher REPORT-Status: " + status, 'warn');
                    return callback(null, CREATE_ON_CHECK_ERROR ? false : true);
                }
        
                let caldata = body.toString().replace(/\r\n[ \t]/g, '').replace(/\r\n/g, '\n');
                const summaryRegex = /(^|\n)SUMMARY(?:;[^:]*)?:\s*Medikamente heute eingenommen(\n|$)/i;
                const exists = summaryRegex.test(caldata);
        
                callback(null, exists);
            });
        }
        
        // Event erstellen
        function createEvent() {
            const now = new Date();
            const uid = Date.now() + '@iobroker';
        
            const startLocal = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 6,0,0);
            const endLocal   = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 20,0,0);
        
            const dtstamp = caldavDateUTC(new Date());
            const dtStartLocal = localDateNoZ(startLocal);
            const dtEndLocal   = localDateNoZ(endLocal);
        
            const ics =
        "BEGIN:VCALENDAR\n" +
        "VERSION:2.0\n" +
        "PRODID:-//ioBroker//DE\n" +
        "BEGIN:VEVENT\n" +
        "UID:" + uid + "\n" +
        "DTSTAMP:" + dtstamp + "\n" +
        "DTSTART;TZID=Europe/Berlin:" + dtStartLocal + "\n" +
        "DTEND;TZID=Europe/Berlin:" + dtEndLocal + "\n" +
        "SUMMARY:Medikamente heute eingenommen\n" +
        "END:VEVENT\n" +
        "END:VCALENDAR";
        
            const filename = encodeURIComponent("medikamente-" + uid + ".ics");
            const eventUrl = calendarUrl + filename;
        
            httpRequest({
                url: eventUrl,
                method: 'PUT',
                auth: username + ':' + password,
                headers: { 'Content-Type': 'text/calendar; charset=utf-8' }
            }, ics, function(error, response) {
                if (error) {
                    log("❌ Fehler beim Erstellen des Events: " + error, 'error');
                    return;
                }
                const status = response ? response.statusCode : 'no-response';
                log("DEBUG: PUT Status: " + status, 'debug');
                if (status >= 200 && status < 300) {
                    log("✅ Termin erfolgreich erstellt", 'info');
                } else {
                    log("⚠️ Unerwartete Antwort beim Erstellen: " + status, 'warn');
                }
            });
        }
        
        // Button überwachen
        on({id: '0_userdata.0.Button', change: 'any'}, function(obj) {
            if (obj.state.val === true) {
                checkEventToday(function(err, exists) {
                    if (exists) {
                        log("ℹ️ Heute existiert bereits ein 'Medikamente heute eingenommen'-Termin — kein neuer Eintrag.", 'info');
                    } else {
                        createEvent();
                    }
                });
            }
        });
        
        
        D 1 Reply Last reply Reply Quote 0
        • D
          dirkhe Developer @Atifan last edited by

          @atifan du kannst den webcal adapter nehmen, der macht genau das. Allerding ist das einrifhten der apj etwas komplizierter, habe ich aber in github dokumentiert. Ich hoffe mal, das funktioniert noch.

          Atifan bahnuhr 2 Replies Last reply Reply Quote 0
          • Atifan
            Atifan @dirkhe last edited by

            @dirkhe AH nice ok danke schaue es mir mal an 🙂

            1 Reply Last reply Reply Quote 0
            • bahnuhr
              bahnuhr Forum Testing Most Active @dirkhe last edited by

              @dirkhe sagte in Skript - automatisch Eintrag im Google Kalender machen:

              @atifan du kannst den webcal adapter nehmen, der macht genau das. Allerding ist das einrifhten der apj etwas komplizierter, habe ich aber in github dokumentiert. Ich hoffe mal, das funktioniert noch.

              Hat bei mir nicht geklappt.
              An diesem Punkt kam ich nicht weiter:
              b99f43d7-6733-4574-a8f9-26f013d870f9-image.png

              D 1 Reply Last reply Reply Quote 0
              • D
                dirkhe Developer @bahnuhr last edited by

                @bahnuhr was hat er denn gemeldet? Hast du "unsicher" zugelassen unter advanced?

                bahnuhr 1 Reply Last reply Reply Quote 0
                • bahnuhr
                  bahnuhr Forum Testing Most Active @dirkhe last edited by

                  @dirkhe sagte in Skript - automatisch Eintrag im Google Kalender machen:

                  "unsicher" zugelassen

                  unsicher, etc. kam da nicht.
                  Das Blatt sieht anders aus.

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post

                  Support us

                  ioBroker
                  Community Adapters
                  Donate

                  604
                  Online

                  32.2k
                  Users

                  80.8k
                  Topics

                  1.3m
                  Posts

                  3
                  7
                  106
                  Loading More Posts
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes
                  Reply
                  • Reply as topic
                  Log in to reply
                  Community
                  Impressum | Datenschutz-Bestimmungen | Nutzungsbedingungen
                  The ioBroker Community 2014-2023
                  logo