Skip to content
  • Home
  • Aktuell
  • Tags
  • 0 Ungelesen 0
  • Kategorien
  • Unreplied
  • Beliebt
  • GitHub
  • Docu
  • Hilfe
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Standard: (Kein Skin)
  • Kein Skin
Einklappen
ioBroker Logo

Community Forum

donate donate
  1. ioBroker Community Home
  2. Deutsch
  3. Skripten / Logik
  4. JavaScript
  5. Omlet Hühnerstall webhook API

NEWS

  • Monatsrückblick Januar/Februar 2026 ist online!
    BluefoxB
    Bluefox
    16
    1
    271

  • Jahresrückblick 2025 – unser neuer Blogbeitrag ist online! ✨
    BluefoxB
    Bluefox
    17
    1
    4.6k

  • Neuer Blogbeitrag: Monatsrückblick - Dezember 2025 🎄
    BluefoxB
    Bluefox
    13
    1
    1.3k

Omlet Hühnerstall webhook API

Geplant Angeheftet Gesperrt Verschoben JavaScript
34 Beiträge 3 Kommentatoren 233 Aufrufe 2 Watching
  • Älteste zuerst
  • Neuste zuerst
  • Meiste Stimmen
Antworten
  • In einem neuen Thema antworten
Anmelden zum Antworten
Dieses Thema wurde gelöscht. Nur Nutzer mit entsprechenden Rechten können es sehen.
  • J Offline
    J Offline
    jwerlsdf
    schrieb am zuletzt editiert von
    #25

    Hi, das sieht sehr gut aus. Ja tatsächlich habe ich seit gestern den Stecker von der Tür gezogen. Dadurch habe ich auch keine Infos mehr in der app. Ich werde nachher gerne die Tür an die Steckdose wieder stecken, sobald ich von der Arbeit zurück bin. Da sollten hoffentlich bei dir aktuelle Infos reinkommen.

    1 Antwort Letzte Antwort
    0
    • J Offline
      J Offline
      jwerlsdf
      schrieb am zuletzt editiert von
      #26

      Die wichtigsten FUnktionen wären für mich das Licht an/ausschalten und die Tür auf/zu machen zu können. SO wie ich das dann verstanden habe, kann ich die Konfiguration ebenfalls ändern? Dann wäre die dritte funktion die Uhrzeiten zu ändern (open and close Time).

      OliverIOO 1 Antwort Letzte Antwort
      0
      • J jwerlsdf

        Die wichtigsten FUnktionen wären für mich das Licht an/ausschalten und die Tür auf/zu machen zu können. SO wie ich das dann verstanden habe, kann ich die Konfiguration ebenfalls ändern? Dann wäre die dritte funktion die Uhrzeiten zu ändern (open and close Time).

        OliverIOO Offline
        OliverIOO Offline
        OliverIO
        schrieb am zuletzt editiert von
        #27

        @jwerlsdf

        so hier zum probieren für die erste runde für licht und tor
        token noch eintragen
        den device id hab ich jetzt nicht versteckt, ohne den token kann da niemand was mit anfangen
        vor dem start bitte noch die beiden datenpunkte vom typ boolean anlegen und die datenpunktnamen dann am anfang in der configuration area eintragen.
        prüfe bitte mal ob du bei den zusätzlichen npm module auch axios eingetragen hast.
        eigentlich ist das nicht notwendig, da der javascriptadapter selbst axios verwendet, aber um für die zukunft sauber zu sein ist das besser

        meine aktuellen experimente für das licht hat den status 204 zurückgegeben.
        der doku nach bedeutet das

        Action has been queued to be sent to the device

        Keine Ahnung ob jetzt alles abgespult wird wenn du es anschaltest.

        // -- configuration area
        const token = 'apitoken';
        const outdoorDeviceId = 'laTnMsWSJxBMGgJD';
        const dp_light = '0_userdata.0.omlet.light'; // datenpunkt mit typ bool anlegen und namen hier eintragen
        const dp_door = '0_userdata.0.omlet.door'; // datenpunkt mit typ bool anlegen und namen hier eintragen
        // --
        
        const axios = require("axios/dist/browser/axios.cjs");
        const BASE_URL = 'https://x107.omlet.co.uk/api/v1'; //kann hier geprüft werden https://smart.omlet.de/developers/api
        let omlet = undefined;
        
        async function connect() {
            console.log("start main2");
            omlet = await omletAPIClient(token);
            await getDevices(true);
        
            //console.log(JSON.stringify(await getDevices(), null, 2));
        }
        async function getDevices(info = false) {
            try {
                const devices = await getRequest('/device');
                if (devices && info) {
                    devices.forEach(device => {
                        console.log(`Device name: ${device.name}, deviceId: ${device.deviceId}`);
                    });
                }
                return devices;
            } catch (error) {
                console.log(error);
                return [];
            }
        }
        async function doAction(deviceId, actionName) {
            const devices = await getDevices();
            const device = devices.find(device => device.deviceId === deviceId);
            if (device) {
                const action = device.actions.find(action => action.actionName === actionName);
                if (action) {
                    await postRequest(action.url);
                }
            }
        }
        async function lightOn(deviceId) {
            await doAction(deviceId, 'on');
        }
        async function lightOff(deviceId) {
            await doAction(deviceId, 'off');
        }
        async function doorOpen(deviceId) {
            await doAction(deviceId, 'open');
        }
        async function doorClose(deviceId) {
            await doAction(deviceId, 'close');
        }
        function omletAPIClient(token) {
            return axios.create({
                baseURL: BASE_URL,
                headers: {
                    'Authorization': `Bearer ${token}`,
                    'Content-Type': 'application/json',
                },
            });
        }
        async function getRequest(url) {
            try {
                const data = await omlet.get(url);
                console.debug(`get request: ${url},status: ${data.status}`);
                return data.data;
            } catch (e) {
                console.log(e);
            }
        }
        async function postRequest(url) {
            try {
                const data = await omlet.post(url);
                console.debug(`post request: ${url},status: ${data.status}`);
                return data.data;
            } catch (e) {
                console.log(e);
            }
        }
        async function main() {
            await connect();
            on({ id: dp_door, change: 'ne' }, async (evt) => {
                //log(evt);
                if (evt.state.val) {
                    await doorOpen(outdoorDeviceId);
                } else {
                    await doorClose(outdoorDeviceId);
                }
            });
            on({ id: dp_light, change: 'ne' }, async (evt) => {
                //log(evt);
                if (evt.state.val) {
                    await lightOn(outdoorDeviceId);
                } else {
                    await lightOff(outdoorDeviceId);
                }
            });
        
            // await lightOn(outdoorDeviceId);
            // await lightOff(outdoorDeviceId);
        }
        main();
        

        Meine Adapter und Widgets
        TVProgram, SqueezeboxRPC, OpenLiga, RSSFeed, MyTime,, pi-hole2, vis-json-template, skiinfo, vis-mapwidgets, vis-2-widgets-rssfeed
        Links im Profil

        1 Antwort Letzte Antwort
        2
        • J Offline
          J Offline
          jwerlsdf
          schrieb am zuletzt editiert von
          #28

          Vielen vielen lieben Dank. Es funktioniert hervorragend! :)
          Die Umbenennung mache ich dann über die VIS da false für tür zu etwas verwirrend ist.

          Würdest du mir noch bitte folgende Funktionen einbauen?
          -Tür stop
          -Uhrzeiten einstellbar Manuel oder Zeiteingabe für Tür auf/zu ggf zusätzlich Astrofunktion (wobei ich letzteres auch selbst mit einem zweiten Script lösen könnte.... dann soll einfach alles auf manual laufen.

          OliverIOO 1 Antwort Letzte Antwort
          0
          • J jwerlsdf

            Vielen vielen lieben Dank. Es funktioniert hervorragend! :)
            Die Umbenennung mache ich dann über die VIS da false für tür zu etwas verwirrend ist.

            Würdest du mir noch bitte folgende Funktionen einbauen?
            -Tür stop
            -Uhrzeiten einstellbar Manuel oder Zeiteingabe für Tür auf/zu ggf zusätzlich Astrofunktion (wobei ich letzteres auch selbst mit einem zweiten Script lösen könnte.... dann soll einfach alles auf manual laufen.

            OliverIOO Offline
            OliverIOO Offline
            OliverIO
            schrieb am zuletzt editiert von OliverIO
            #29

            @jwerlsdf

            weiter gehts mit der nächsten version.
            vorgehensweise wie oben.
            es sind ein paar mehr datenpunkte dazugekommen
            bitte vorher die datenpunkte anlegen. typ und detailanweisung im zeilenkommentar beachten

            const dp_doorstop = '0_userdata.0.omlet.doorstop'; // datenpunkt mit typ bool und rolle button anlegen und namen hier eintragen
            const dp_doortimeopen = '0_userdata.0.omlet.doortimeopen'; // datenpunkt mit typ string, zeit im 24h format im format 06:00 oder 20:30
            const dp_doortimeclose = '0_userdata.0.omlet.doortimeclose'; // datenpunkt mit typ string, zeit im 24h format im format 06:00 oder 20:30
            const dp_devicedata = '0_userdata.0.omlet.devicedata'; //// datenpunkt mit typ string, hier wird nach jeder aktualisierung der aktuelle device status abgerufen und eingetragen
            

            wenn im datenpunkt dp_doortimeopen und dp_doortimeclose etwas eingetragen wird, dann sollte die jeweilige zeit gesetzt werden.
            im Anschluss wird die Konfiguration gleich abgerufen und im Datenpunkt dp_devicedata abgespeichert, sowie auch dp_doortimeopen und dp_doortimeclose wieder aktualisiert
            der button doorstop führt die entsprechende aktion aus.

            um mit dem angeschlossenen gerät nicht zu viel unruhe zu erzeugen, habe ich (hoffentlich) nur die lesenden aktionen ausgeführt. die schreibenden aktionen habe ich mal ausgelassen, was aber auch mangels test zu höheren fehlerrisiko neigt. aber mal schauen.
            bitte möglichst genau darüber berichten

            // -- configuration area
            const token = 'apitoken';
            const outdoorDeviceId = 'laTnMsWSJxBMGgJD';
            const dp_light = '0_userdata.0.omlet.light'; // datenpunkt mit typ bool anlegen und namen hier eintragen
            const dp_door = '0_userdata.0.omlet.door'; // datenpunkt mit typ bool anlegen und namen hier eintragen
            const dp_doorstop = '0_userdata.0.omlet.doorstop'; // datenpunkt mit typ bool und rolle button anlegen und namen hier eintragen
            const dp_doortimeopen = '0_userdata.0.omlet.doortimeopen'; // datenpunkt mit typ string, zeit im 24h format im format 06:00 oder 20:30
            const dp_doortimeclose = '0_userdata.0.omlet.doortimeclose'; // datenpunkt mit typ string, zeit im 24h format im format 06:00 oder 20:30
            const dp_devicedata = '0_userdata.0.omlet.devicedata'; //// datenpunkt mit typ string, hier wird nach jeder aktualisierung der aktuelle device status abgerufen und eingetragen
            
            
            // --
            
            const axios = require("axios/dist/browser/axios.cjs");
            const BASE_URL = 'https://x107.omlet.co.uk/api/v1'; //kann hier geprüft werden https://smart.omlet.de/developers/api
            let omlet = undefined;
            
            async function connect() {
                console.log("start main2");
                omlet = await omletAPIClient(token);
                await getDevices(true);
            
                //console.log(JSON.stringify(await getDevices(), null, 2));
            }
            async function getDevices(info = false) {
                try {
                    const devices = await getRequest('/device');
                    if (devices && info) {
                        devices.forEach(device => {
                            console.log(`Device name: ${device.name}, deviceId: ${device.deviceId}`);
                        });
                    }
                    return devices;
                } catch (error) {
                    console.log(error);
                    return [];
                }
            }
            async function getDevice(deviceId) {
                try {
                    const device = await getRequest(`/device/${deviceId}`);
                    return device;
                } catch (error) {
                    console.log(error);
                    return {};
                }
            }
            async function getConfiguration(deviceId) {
                try {
                    const configuration = await getRequest(`/device/${deviceId}/configuration`);
                    return configuration;
                } catch (error) {
                    console.log(error);
                    return {};
                }
            }
            async function setConfiguration(deviceId, configuration) {
                try {
                    const result = await patchRequest(`/device/${deviceId}/configuration`, configuration);
            
                    return result;
                } catch (error) {
                    console.log(error);
                    return {};
                }
            }
            async function getDeviceInfo(deviceId) {
                const device = await getDevice(deviceId);
            
                setState(dp_devicedata, JSON.stringify(device, null, 2), true);
                setState(dp_doortimeopen, device.configuration.door.openTime, true);
                setState(dp_doortimeclose, device.configuration.door.closeTime, true);
            }
            async function doAction(deviceId, actionName) {
                const devices = await getDevices();
                const device = devices.find(device => device.deviceId === deviceId);
                if (device) {
                    const action = device.actions.find(action => action.actionName === actionName);
                    if (action) {
                        await postRequest(action.url);
                        await getDeviceInfo(outdoorDeviceId);
                    }
                }
            }
            async function lightOn(deviceId) {
                await doAction(deviceId, 'on');
            }
            async function lightOff(deviceId) {
                await doAction(deviceId, 'off');
            }
            async function doorOpen(deviceId) {
                await doAction(deviceId, 'open');
            }
            async function doorClose(deviceId) {
                await doAction(deviceId, 'close');
            }
            async function doorStop(deviceId) {
                await doAction(deviceId, 'stop');
            }
            async function setDoorTimeClose(deviceId, time) {
                const configuration = getConfiguration(deviceId);
                const newConfiguration = {
                    ...configuration,
                    door: {
                        ...configuration.door,
                        closeTime: time
                    }
                };
                await setConfiguration(deviceId, newConfiguration);
                await getDeviceInfo(outdoorDeviceId);
            }
            async function setDoorTimeOpen(deviceId, time) {
                const configuration = getConfiguration(deviceId);
                const newConfiguration = {
                    ...configuration,
                    door: {
                        ...configuration.door,
                        openTime: time
                    }
                };
                await setConfiguration(deviceId, newConfiguration);
                await getDeviceInfo(outdoorDeviceId);
            }
            function omletAPIClient(token) {
                return axios.create({
                    baseURL: BASE_URL,
                    headers: {
                        'Authorization': `Bearer ${token}`,
                        'Content-Type': 'application/json',
                    },
                });
            }
            function isValidTime24h(value) {
                return /^(?:[01]\d|2[0-3]):[0-5]\d$/.test(value);
            }
            async function getRequest(url) {
                try {
                    const data = await omlet.get(url);
                    console.debug(`get request: ${url},status: ${data.status}`);
                    return data.data;
                } catch (e) {
                    console.log(e);
                }
            }
            async function postRequest(url) {
                try {
                    const data = await omlet.post(url);
                    console.debug(`post request: ${url},status: ${data.status}`);
                    return data.data;
                } catch (e) {
                    console.log(e);
                }
            }
            async function patchRequest(url, data) {
                try {
                    const result = await omlet.patch(url, data);
                    console.debug(`patch request: ${url},status: ${result.status}`);
                    return result.data;
                } catch (e) {
                    console.log(e);
                }
            }
            function watchTimeState(id, label) {
                on({ id: id, change: 'ne', ack: false }, async (obj) => {
                    const newVal = String(obj.state.val || '').trim();
                    const oldVal = obj.oldState && obj.oldState.val != null
                        ? String(obj.oldState.val)
                        : '';
                    if (isValidTime24h(newVal)) {
                        log(`${label}: gültige Zeit gesetzt -> ${newVal}`, 'debug');
                        if (id === dp_doortimeopen) {
                            await setDoorTimeOpen(outdoorDeviceId, newVal);
                        }
                        if (id === dp_doortimeclose) {
                            await setDoorTimeClose(outdoorDeviceId, newVal);
                        }
                        return;
                    }
                    log(`${label}: ungültige Zeit "${newVal}" - setze auf "${oldVal}" zurück`, 'warn');
            
                    // ungültige Eingabe zurücksetzen
                    setState(id, oldVal, true);
                });
            }
            async function main() {
                await connect();
                await getDeviceInfo(outdoorDeviceId);
                on({ id: dp_door, change: 'ne', ack: false }, async (evt) => {
                    //log(evt);
                    if (evt.state.val) {
                        await doorOpen(outdoorDeviceId);
                    } else {
                        await doorClose(outdoorDeviceId);
                    }
                });
                on({ id: dp_light, change: 'ne', ack: false }, async (evt) => {
                    //log(evt);
                    if (evt.state.val) {
                        await lightOn(outdoorDeviceId);
                    } else {
                        await lightOff(outdoorDeviceId);
                    }
                });
                on({ id: dp_doorstop, change: 'any', ack: false }, async (/* evt */) => {
                    //log(evt);
                    doorStop(outdoorDeviceId);
                    setState(dp_doorstop, false, true);
                });
                watchTimeState(dp_doortimeopen, 'Öffnungszeit');
                watchTimeState(dp_doortimeclose, 'Schließzeit');
                // await lightOn(outdoorDeviceId);
                // await lightOff(outdoorDeviceId);
            
            }
            main();
            

            für astro hat der iobroker ja bereits ereignisse eingebaut,
            wenn die dann aufgerufen werden musst du dann einfach nur die Funktionen doorOpen und doorClose mit der deviceId (die in der Variable outdoorDeviceId steht) aufrufen

            Sind die Funktionen soweit verständlich? Brauchst du mehr Detaildokumentation für spätere Anpassungen?

            Meine Adapter und Widgets
            TVProgram, SqueezeboxRPC, OpenLiga, RSSFeed, MyTime,, pi-hole2, vis-json-template, skiinfo, vis-mapwidgets, vis-2-widgets-rssfeed
            Links im Profil

            1 Antwort Letzte Antwort
            1
            • J Offline
              J Offline
              jwerlsdf
              schrieb am zuletzt editiert von jwerlsdf
              #30

              Hi, danke für das 2te Skript. FUnktionen sind verständlich. Eine Funktion fehlt mir:

                  "door": {
                    "openMode": "manual",
                    "openDelay": 0,
                    "openTime": "06:00",
                    "closeMode": "manual",
              

              In der APP kann ich beim Türstation zwischen 3 Einstellungen auswählen:
              Lichtssensor
              Zeit
              Manuell.

              Aktuell habe ich wie du es siehst auf Manuell gestellt. In den Datenpunkten werden die Uhrzeiten angezeigt. Meine Frau/ Ich wissen aber nicht, ob es nun auf Manuell Zeit... ist.
              Kannst du mir noch die FUnktion einbauen, welche aktuelle dieser 3 Funktion ich habe und das ich zwischen diesen drei Umschalten kann?
              Dann wäre das Skript für mich perfekt.

              OliverIOO 1 Antwort Letzte Antwort
              0
              • J jwerlsdf

                Hi, danke für das 2te Skript. FUnktionen sind verständlich. Eine Funktion fehlt mir:

                    "door": {
                      "openMode": "manual",
                      "openDelay": 0,
                      "openTime": "06:00",
                      "closeMode": "manual",
                

                In der APP kann ich beim Türstation zwischen 3 Einstellungen auswählen:
                Lichtssensor
                Zeit
                Manuell.

                Aktuell habe ich wie du es siehst auf Manuell gestellt. In den Datenpunkten werden die Uhrzeiten angezeigt. Meine Frau/ Ich wissen aber nicht, ob es nun auf Manuell Zeit... ist.
                Kannst du mir noch die FUnktion einbauen, welche aktuelle dieser 3 Funktion ich habe und das ich zwischen diesen drei Umschalten kann?
                Dann wäre das Skript für mich perfekt.

                OliverIOO Offline
                OliverIOO Offline
                OliverIO
                schrieb am zuletzt editiert von OliverIO
                #31

                @jwerlsdf

                schau mal ob das funktioniert.
                schau dir mal die zeilen 68-76 an.
                An dieser Stelle wird die Konfiguration des devices gelesen und die Werte in die Datenpunkte geschrieben. Wenn du weitere Datenpunkte benötigst, kannst du die selbst hinzufügen (zumindest wenn du sie nur lesen willst).
                Für beschreibbare Datenpunkt muss man etwas mehr aufwand treiben.
                Wie der aktuelle Stand der Konfiguration aussieht, kannst du dir im Datenpunkt dp_devicedata anschauen. Da ist configuration ein Unterpunkt. Der wird auch immer aktuell geschrieben

                // -- configuration area
                const token = 'apitoken';
                const outdoorDeviceId = 'laTnMsWSJxBMGgJD';
                const dp_light = '0_userdata.0.omlet.light'; // datenpunkt mit typ bool anlegen und namen hier eintragen
                const dp_door = '0_userdata.0.omlet.door'; // datenpunkt mit typ bool anlegen und namen hier eintragen
                const dp_doorstop = '0_userdata.0.omlet.doorstop'; // datenpunkt mit typ bool und rolle button anlegen und namen hier eintragen
                const dp_doortimeopen = '0_userdata.0.omlet.doortimeopen'; // datenpunkt mit typ string, zeit im 24h format im format 06:00 oder 20:30
                const dp_doortimeclose = '0_userdata.0.omlet.doortimeclose'; // datenpunkt mit typ string, zeit im 24h format im format 06:00 oder 20:30
                const dp_devicedata = '0_userdata.0.omlet.devicedata'; //// datenpunkt mit typ string, hier wird nach jeder aktualisierung der aktuelle device status abgerufen und eingetragen
                const dp_doormodeopen = "0_userdata.0.omlet.doormodeopen"; //// datenpunkt mit typ string, hier wird nach jeder aktualisierung der aktuelle device status abgerufen und eingetragen
                const dp_doormodeclose = "0_userdata.0.omlet.doormodeclose"; //// datenpunkt mit typ string, hier wird nach jeder aktualisierung der aktuelle device status abgerufen und eingetragen
                
                // --
                
                const axios = require("axios/dist/browser/axios.cjs");
                const BASE_URL = 'https://x107.omlet.co.uk/api/v1'; //kann hier geprüft werden https://smart.omlet.de/developers/api
                let omlet = undefined;
                
                async function connect() {
                    console.log("start main2");
                    omlet = await omletAPIClient(token);
                    await getDevices(true);
                
                    //console.log(JSON.stringify(await getDevices(), null, 2));
                }
                async function getDevices(info = false) {
                    try {
                        const devices = await getRequest('/device');
                        if (devices && info) {
                            devices.forEach(device => {
                                console.log(`Device name: ${device.name}, deviceId: ${device.deviceId}`);
                            });
                        }
                        return devices;
                    } catch (error) {
                        console.log(error);
                        return [];
                    }
                }
                async function getDevice(deviceId) {
                    try {
                        const device = await getRequest(`/device/${deviceId}`);
                        return device;
                    } catch (error) {
                        console.log(error);
                        return {};
                    }
                }
                async function getConfiguration(deviceId) {
                    try {
                        const configuration = await getRequest(`/device/${deviceId}/configuration`);
                        return configuration;
                    } catch (error) {
                        console.log(error);
                        return {};
                    }
                }
                async function setConfiguration(deviceId, configuration) {
                    try {
                        const result = await patchRequest(`/device/${deviceId}/configuration`, configuration);
                
                        return result;
                    } catch (error) {
                        console.log(error);
                        return {};
                    }
                }
                async function getDeviceInfo(deviceId) {
                    const device = await getDevice(deviceId);
                
                    setState(dp_devicedata, JSON.stringify(device, null, 2), true);
                    setState(dp_doortimeopen, device.configuration.door.openTime, true);
                    setState(dp_doortimeclose, device.configuration.door.closeTime, true);
                    setState(dp_doormodeopen, device.configuration.door.openMode, true);
                    setState(dp_doormodeclose, device.configuration.door.closeMode, true);
                }
                async function doAction(deviceId, actionName) {
                    const devices = await getDevices();
                    const device = devices.find(device => device.deviceId === deviceId);
                    if (device) {
                        const action = device.actions.find(action => action.actionName === actionName);
                        if (action) {
                            await postRequest(action.url);
                            await getDeviceInfo(outdoorDeviceId);
                        }
                    }
                }
                async function lightOn(deviceId) {
                    await doAction(deviceId, 'on');
                }
                async function lightOff(deviceId) {
                    await doAction(deviceId, 'off');
                }
                async function doorOpen(deviceId) {
                    await doAction(deviceId, 'open');
                }
                async function doorClose(deviceId) {
                    await doAction(deviceId, 'close');
                }
                async function doorStop(deviceId) {
                    await doAction(deviceId, 'stop');
                }
                async function setDoorTimeClose(deviceId, time) {
                    const configuration = getConfiguration(deviceId);
                    const newConfiguration = {
                        ...configuration,
                        door: {
                            ...configuration.door,
                            closeTime: time
                        }
                    };
                    await setConfiguration(deviceId, newConfiguration);
                    await getDeviceInfo(outdoorDeviceId);
                }
                async function setDoorTimeOpen(deviceId, time) {
                    const configuration = getConfiguration(deviceId);
                    const newConfiguration = {
                        ...configuration,
                        door: {
                            ...configuration.door,
                            openTime: time
                        }
                    };
                    await setConfiguration(deviceId, newConfiguration);
                    await getDeviceInfo(outdoorDeviceId);
                }
                function omletAPIClient(token) {
                    return axios.create({
                        baseURL: BASE_URL,
                        headers: {
                            'Authorization': `Bearer ${token}`,
                            'Content-Type': 'application/json',
                        },
                    });
                }
                function isValidTime24h(value) {
                    return /^(?:[01]\d|2[0-3]):[0-5]\d$/.test(value);
                }
                async function getRequest(url) {
                    try {
                        const data = await omlet.get(url);
                        console.debug(`get request: ${url},status: ${data.status}`);
                        return data.data;
                    } catch (e) {
                        console.log(e);
                    }
                }
                async function postRequest(url) {
                    try {
                        const data = await omlet.post(url);
                        console.debug(`post request: ${url},status: ${data.status}`);
                        return data.data;
                    } catch (e) {
                        console.log(e);
                    }
                }
                async function patchRequest(url, data) {
                    try {
                        const result = await omlet.patch(url, data);
                        console.debug(`patch request: ${url},status: ${result.status}`);
                        return result.data;
                    } catch (e) {
                        console.log(e);
                    }
                }
                function watchTimeState(id, label) {
                    on({ id: id, change: 'ne', ack: false }, async (obj) => {
                        const newVal = String(obj.state.val || '').trim();
                        const oldVal = obj.oldState && obj.oldState.val != null
                            ? String(obj.oldState.val)
                            : '';
                        if (isValidTime24h(newVal)) {
                            log(`${label}: gültige Zeit gesetzt -> ${newVal}`, 'debug');
                            if (id === dp_doortimeopen) {
                                await setDoorTimeOpen(outdoorDeviceId, newVal);
                            }
                            if (id === dp_doortimeclose) {
                                await setDoorTimeClose(outdoorDeviceId, newVal);
                            }
                            return;
                        }
                        log(`${label}: ungültige Zeit "${newVal}" - setze auf "${oldVal}" zurück`, 'warn');
                
                        // ungültige Eingabe zurücksetzen
                        setState(id, oldVal, true);
                    });
                }
                async function main() {
                    await connect();
                    await getDeviceInfo(outdoorDeviceId);
                    on({ id: dp_door, change: 'ne', ack: false }, async (evt) => {
                        //log(evt);
                        if (evt.state.val) {
                            await doorOpen(outdoorDeviceId);
                        } else {
                            await doorClose(outdoorDeviceId);
                        }
                    });
                    on({ id: dp_light, change: 'ne', ack: false }, async (evt) => {
                        //log(evt);
                        if (evt.state.val) {
                            await lightOn(outdoorDeviceId);
                        } else {
                            await lightOff(outdoorDeviceId);
                        }
                    });
                    on({ id: dp_doorstop, change: 'any', ack: false }, async (/* evt */) => {
                        //log(evt);
                        doorStop(outdoorDeviceId);
                        setState(dp_doorstop, false, true);
                    });
                    watchTimeState(dp_doortimeopen, 'Öffnungszeit');
                    watchTimeState(dp_doortimeclose, 'Schließzeit');
                    // await lightOn(outdoorDeviceId);
                    // await lightOff(outdoorDeviceId);
                
                }
                main();
                

                Meine Adapter und Widgets
                TVProgram, SqueezeboxRPC, OpenLiga, RSSFeed, MyTime,, pi-hole2, vis-json-template, skiinfo, vis-mapwidgets, vis-2-widgets-rssfeed
                Links im Profil

                1 Antwort Letzte Antwort
                0
                • J Offline
                  J Offline
                  jwerlsdf
                  schrieb am zuletzt editiert von jwerlsdf
                  #32
                  // -- configuration area
                  const token = 'XXXXXXXXXX';
                  const outdoorDeviceId = 'laTnMsWSJxBMGgJD';
                  const dp_light = '0_userdata.0.omlet.light'; // datenpunkt mit typ bool anlegen und namen hier eintragen
                  const dp_door = '0_userdata.0.omlet.door'; // datenpunkt mit typ bool anlegen und namen hier eintragen
                  const dp_doorstop = '0_userdata.0.omlet.doorstop'; // datenpunkt mit typ bool und rolle button anlegen und namen hier eintragen
                  const dp_doortimeopen = '0_userdata.0.omlet.doortimeopen'; // datenpunkt mit typ string, zeit im 24h format im format 06:00 oder 20:30
                  const dp_doortimeclose = '0_userdata.0.omlet.doortimeclose'; // datenpunkt mit typ string, zeit im 24h format im format 06:00 oder 20:30
                  const dp_devicedata = '0_userdata.0.omlet.devicedata'; //// datenpunkt mit typ string, hier wird nach jeder aktualisierung der aktuelle device status abgerufen und eingetragen
                  const dp_doormodeopen = "0_userdata.0.omlet.doormodeopen"; //// datenpunkt mit typ string, hier wird nach jeder aktualisierung der aktuelle device status abgerufen und eingetragen
                  const dp_doormodeclose = "0_userdata.0.omlet.doormodeclose"; //// datenpunkt mit typ string, hier wird nach jeder aktualisierung der aktuelle device status abgerufen und eingetragen
                  const dp_doorcloseLightLevel = "0_userdata.0.omlet.LightLevelclose"; //// datenpunkt mit typ number
                  const dp_dooropenLightLevel = "0_userdata.0.omlet.LightLevelopen";  //// datenpunkt mit typ number
                  const dp_dooropenDelay = "0_userdata.0.omlet.Delayopen";  //// datenpunkt mit typ number
                  const dp_doorcloseDelay = "0_userdata.0.omlet.Delayclose" //// datenpunkt mit typ number
                  
                  
                  // --
                   
                  const axios = require("axios/dist/browser/axios.cjs");
                  const BASE_URL = 'https://x107.omlet.co.uk/api/v1'; //kann hier geprüft werden https://smart.omlet.de/developers/api
                  let omlet = undefined;
                   
                  async function connect() {
                      console.log("start main2");
                      omlet = await omletAPIClient(token);
                      await getDevices(true);
                   
                      //console.log(JSON.stringify(await getDevices(), null, 2));
                  }
                  async function getDevices(info = false) {
                      try {
                          const devices = await getRequest('/device');
                          if (devices && info) {
                              devices.forEach(device => {
                                  console.log(`Device name: ${device.name}, deviceId: ${device.deviceId}`);
                              });
                          }
                          return devices;
                      } catch (error) {
                          console.log(error);
                          return [];
                      }
                  }
                  async function getDevice(deviceId) {
                      try {
                          const device = await getRequest(`/device/${deviceId}`);
                          return device;
                      } catch (error) {
                          console.log(error);
                          return {};
                      }
                  }
                  async function getConfiguration(deviceId) {
                      try {
                          const configuration = await getRequest(`/device/${deviceId}/configuration`);
                          return configuration;
                      } catch (error) {
                          console.log(error);
                          return {};
                      }
                  }
                  async function setConfiguration(deviceId, configuration) {
                      try {
                          const result = await patchRequest(`/device/${deviceId}/configuration`, configuration);
                   
                          return result;
                      } catch (error) {
                          console.log(error);
                          return {};
                      }
                  }
                  async function getDeviceInfo(deviceId) {
                      const device = await getDevice(deviceId);
                   
                      setState(dp_devicedata, JSON.stringify(device, null, 2), true);
                      setState(dp_doortimeopen, device.configuration.door.openTime, true);
                      setState(dp_doortimeclose, device.configuration.door.closeTime, true);
                      setState(dp_doormodeopen, device.configuration.door.openMode, true);
                      setState(dp_doormodeclose, device.configuration.door.closeMode, true);
                      setState(dp_doorcloseLightLevel, device.configuration.door.closeLightLevel, true);
                      setState(dp_dooropenLightLevel, device.configuration.door.openLightLevel, true);
                      setState(dp_dooropenDelay, device.configuration.door.openDelay, true);
                      setState(dp_doorcloseDelay, device.configuration.door.closeDelay, true);
                  }
                  async function doAction(deviceId, actionName) {
                      const devices = await getDevices();
                      const device = devices.find(device => device.deviceId === deviceId);
                      if (device) {
                          const action = device.actions.find(action => action.actionName === actionName);
                          if (action) {
                              await postRequest(action.url);
                              await getDeviceInfo(outdoorDeviceId);
                          }
                      }
                  }
                  async function lightOn(deviceId) {
                      await doAction(deviceId, 'on');
                  }
                  async function lightOff(deviceId) {
                      await doAction(deviceId, 'off');
                  }
                  async function doorOpen(deviceId) {
                      await doAction(deviceId, 'open');
                  }
                  async function doorClose(deviceId) {
                      await doAction(deviceId, 'close');
                  }
                  async function doorStop(deviceId) {
                      await doAction(deviceId, 'stop');
                  }
                  async function setDoorTimeClose(deviceId, time) {
                      const configuration = getConfiguration(deviceId);
                      const newConfiguration = {
                          ...configuration,
                          door: {
                              ...configuration.door,
                              closeTime: time
                          }
                      };
                      await setConfiguration(deviceId, newConfiguration);
                      await getDeviceInfo(outdoorDeviceId);
                  }
                  async function setDoorTimeOpen(deviceId, time) {
                      const configuration = getConfiguration(deviceId);
                      const newConfiguration = {
                          ...configuration,
                          door: {
                              ...configuration.door,
                              openTime: time
                          }
                      };
                      await setConfiguration(deviceId, newConfiguration);
                      await getDeviceInfo(outdoorDeviceId);
                  }
                  function omletAPIClient(token) {
                      return axios.create({
                          baseURL: BASE_URL,
                          headers: {
                              'Authorization': `Bearer ${token}`,
                              'Content-Type': 'application/json',
                          },
                      });
                  }
                  function isValidTime24h(value) {
                      return /^(?:[01]\d|2[0-3]):[0-5]\d$/.test(value);
                  }
                  async function getRequest(url) {
                      try {
                          const data = await omlet.get(url);
                          console.debug(`get request: ${url},status: ${data.status}`);
                          return data.data;
                      } catch (e) {
                          console.log(e);
                      }
                  }
                  async function postRequest(url) {
                      try {
                          const data = await omlet.post(url);
                          console.debug(`post request: ${url},status: ${data.status}`);
                          return data.data;
                      } catch (e) {
                          console.log(e);
                      }
                  }
                  async function patchRequest(url, data) {
                      try {
                          const result = await omlet.patch(url, data);
                          console.debug(`patch request: ${url},status: ${result.status}`);
                          return result.data;
                      } catch (e) {
                          console.log(e);
                      }
                  }
                  function watchTimeState(id, label) {
                      on({ id: id, change: 'ne', ack: false }, async (obj) => {
                          const newVal = String(obj.state.val || '').trim();
                          const oldVal = obj.oldState && obj.oldState.val != null
                              ? String(obj.oldState.val)
                              : '';
                          if (isValidTime24h(newVal)) {
                              log(`${label}: gültige Zeit gesetzt -> ${newVal}`, 'debug');
                              if (id === dp_doortimeopen) {
                                  await setDoorTimeOpen(outdoorDeviceId, newVal);
                              }
                              if (id === dp_doortimeclose) {
                                  await setDoorTimeClose(outdoorDeviceId, newVal);
                              }
                              return;
                          }
                          log(`${label}: ungültige Zeit "${newVal}" - setze auf "${oldVal}" zurück`, 'warn');
                   
                          // ungültige Eingabe zurücksetzen
                          setState(id, oldVal, true);
                      });
                  }
                  async function main() {
                      await connect();
                      await getDeviceInfo(outdoorDeviceId);
                      on({ id: dp_door, change: 'ne', ack: false }, async (evt) => {
                          //log(evt);
                          if (evt.state.val) {
                              await doorOpen(outdoorDeviceId);
                          } else {
                              await doorClose(outdoorDeviceId);
                          }
                      });
                      on({ id: dp_light, change: 'ne', ack: false }, async (evt) => {
                          //log(evt);
                          if (evt.state.val) {
                              await lightOn(outdoorDeviceId);
                          } else {
                              await lightOff(outdoorDeviceId);
                          }
                      });
                      on({ id: dp_doorstop, change: 'any', ack: false }, async (/* evt */) => {
                          //log(evt);
                          doorStop(outdoorDeviceId);
                          setState(dp_doorstop, false, true);
                      });
                      watchTimeState(dp_doortimeopen, 'Öffnungszeit');
                      watchTimeState(dp_doortimeclose, 'Schließzeit');
                      // await lightOn(outdoorDeviceId);
                      // await lightOff(outdoorDeviceId);
                   
                  }
                  main();
                  

                  Hi, habe jetzt noch einmal ein paar Datenpunkte hinzugefügt. Ab wann werden denn die Datenpunkte in beide Richtungen geschrieben? Als ich die Tür über das Skript aufgemacht habe, ging es sofort. Als ich in der APP beispielsweise den Mode von "time" auf "manual" gestellt habe, ging es auch nach 5 Minuten nicht, erst nachdem ch das skript neu gestaret habe.

                  OliverIOO 1 Antwort Letzte Antwort
                  0
                  • J jwerlsdf
                    // -- configuration area
                    const token = 'XXXXXXXXXX';
                    const outdoorDeviceId = 'laTnMsWSJxBMGgJD';
                    const dp_light = '0_userdata.0.omlet.light'; // datenpunkt mit typ bool anlegen und namen hier eintragen
                    const dp_door = '0_userdata.0.omlet.door'; // datenpunkt mit typ bool anlegen und namen hier eintragen
                    const dp_doorstop = '0_userdata.0.omlet.doorstop'; // datenpunkt mit typ bool und rolle button anlegen und namen hier eintragen
                    const dp_doortimeopen = '0_userdata.0.omlet.doortimeopen'; // datenpunkt mit typ string, zeit im 24h format im format 06:00 oder 20:30
                    const dp_doortimeclose = '0_userdata.0.omlet.doortimeclose'; // datenpunkt mit typ string, zeit im 24h format im format 06:00 oder 20:30
                    const dp_devicedata = '0_userdata.0.omlet.devicedata'; //// datenpunkt mit typ string, hier wird nach jeder aktualisierung der aktuelle device status abgerufen und eingetragen
                    const dp_doormodeopen = "0_userdata.0.omlet.doormodeopen"; //// datenpunkt mit typ string, hier wird nach jeder aktualisierung der aktuelle device status abgerufen und eingetragen
                    const dp_doormodeclose = "0_userdata.0.omlet.doormodeclose"; //// datenpunkt mit typ string, hier wird nach jeder aktualisierung der aktuelle device status abgerufen und eingetragen
                    const dp_doorcloseLightLevel = "0_userdata.0.omlet.LightLevelclose"; //// datenpunkt mit typ number
                    const dp_dooropenLightLevel = "0_userdata.0.omlet.LightLevelopen";  //// datenpunkt mit typ number
                    const dp_dooropenDelay = "0_userdata.0.omlet.Delayopen";  //// datenpunkt mit typ number
                    const dp_doorcloseDelay = "0_userdata.0.omlet.Delayclose" //// datenpunkt mit typ number
                    
                    
                    // --
                     
                    const axios = require("axios/dist/browser/axios.cjs");
                    const BASE_URL = 'https://x107.omlet.co.uk/api/v1'; //kann hier geprüft werden https://smart.omlet.de/developers/api
                    let omlet = undefined;
                     
                    async function connect() {
                        console.log("start main2");
                        omlet = await omletAPIClient(token);
                        await getDevices(true);
                     
                        //console.log(JSON.stringify(await getDevices(), null, 2));
                    }
                    async function getDevices(info = false) {
                        try {
                            const devices = await getRequest('/device');
                            if (devices && info) {
                                devices.forEach(device => {
                                    console.log(`Device name: ${device.name}, deviceId: ${device.deviceId}`);
                                });
                            }
                            return devices;
                        } catch (error) {
                            console.log(error);
                            return [];
                        }
                    }
                    async function getDevice(deviceId) {
                        try {
                            const device = await getRequest(`/device/${deviceId}`);
                            return device;
                        } catch (error) {
                            console.log(error);
                            return {};
                        }
                    }
                    async function getConfiguration(deviceId) {
                        try {
                            const configuration = await getRequest(`/device/${deviceId}/configuration`);
                            return configuration;
                        } catch (error) {
                            console.log(error);
                            return {};
                        }
                    }
                    async function setConfiguration(deviceId, configuration) {
                        try {
                            const result = await patchRequest(`/device/${deviceId}/configuration`, configuration);
                     
                            return result;
                        } catch (error) {
                            console.log(error);
                            return {};
                        }
                    }
                    async function getDeviceInfo(deviceId) {
                        const device = await getDevice(deviceId);
                     
                        setState(dp_devicedata, JSON.stringify(device, null, 2), true);
                        setState(dp_doortimeopen, device.configuration.door.openTime, true);
                        setState(dp_doortimeclose, device.configuration.door.closeTime, true);
                        setState(dp_doormodeopen, device.configuration.door.openMode, true);
                        setState(dp_doormodeclose, device.configuration.door.closeMode, true);
                        setState(dp_doorcloseLightLevel, device.configuration.door.closeLightLevel, true);
                        setState(dp_dooropenLightLevel, device.configuration.door.openLightLevel, true);
                        setState(dp_dooropenDelay, device.configuration.door.openDelay, true);
                        setState(dp_doorcloseDelay, device.configuration.door.closeDelay, true);
                    }
                    async function doAction(deviceId, actionName) {
                        const devices = await getDevices();
                        const device = devices.find(device => device.deviceId === deviceId);
                        if (device) {
                            const action = device.actions.find(action => action.actionName === actionName);
                            if (action) {
                                await postRequest(action.url);
                                await getDeviceInfo(outdoorDeviceId);
                            }
                        }
                    }
                    async function lightOn(deviceId) {
                        await doAction(deviceId, 'on');
                    }
                    async function lightOff(deviceId) {
                        await doAction(deviceId, 'off');
                    }
                    async function doorOpen(deviceId) {
                        await doAction(deviceId, 'open');
                    }
                    async function doorClose(deviceId) {
                        await doAction(deviceId, 'close');
                    }
                    async function doorStop(deviceId) {
                        await doAction(deviceId, 'stop');
                    }
                    async function setDoorTimeClose(deviceId, time) {
                        const configuration = getConfiguration(deviceId);
                        const newConfiguration = {
                            ...configuration,
                            door: {
                                ...configuration.door,
                                closeTime: time
                            }
                        };
                        await setConfiguration(deviceId, newConfiguration);
                        await getDeviceInfo(outdoorDeviceId);
                    }
                    async function setDoorTimeOpen(deviceId, time) {
                        const configuration = getConfiguration(deviceId);
                        const newConfiguration = {
                            ...configuration,
                            door: {
                                ...configuration.door,
                                openTime: time
                            }
                        };
                        await setConfiguration(deviceId, newConfiguration);
                        await getDeviceInfo(outdoorDeviceId);
                    }
                    function omletAPIClient(token) {
                        return axios.create({
                            baseURL: BASE_URL,
                            headers: {
                                'Authorization': `Bearer ${token}`,
                                'Content-Type': 'application/json',
                            },
                        });
                    }
                    function isValidTime24h(value) {
                        return /^(?:[01]\d|2[0-3]):[0-5]\d$/.test(value);
                    }
                    async function getRequest(url) {
                        try {
                            const data = await omlet.get(url);
                            console.debug(`get request: ${url},status: ${data.status}`);
                            return data.data;
                        } catch (e) {
                            console.log(e);
                        }
                    }
                    async function postRequest(url) {
                        try {
                            const data = await omlet.post(url);
                            console.debug(`post request: ${url},status: ${data.status}`);
                            return data.data;
                        } catch (e) {
                            console.log(e);
                        }
                    }
                    async function patchRequest(url, data) {
                        try {
                            const result = await omlet.patch(url, data);
                            console.debug(`patch request: ${url},status: ${result.status}`);
                            return result.data;
                        } catch (e) {
                            console.log(e);
                        }
                    }
                    function watchTimeState(id, label) {
                        on({ id: id, change: 'ne', ack: false }, async (obj) => {
                            const newVal = String(obj.state.val || '').trim();
                            const oldVal = obj.oldState && obj.oldState.val != null
                                ? String(obj.oldState.val)
                                : '';
                            if (isValidTime24h(newVal)) {
                                log(`${label}: gültige Zeit gesetzt -> ${newVal}`, 'debug');
                                if (id === dp_doortimeopen) {
                                    await setDoorTimeOpen(outdoorDeviceId, newVal);
                                }
                                if (id === dp_doortimeclose) {
                                    await setDoorTimeClose(outdoorDeviceId, newVal);
                                }
                                return;
                            }
                            log(`${label}: ungültige Zeit "${newVal}" - setze auf "${oldVal}" zurück`, 'warn');
                     
                            // ungültige Eingabe zurücksetzen
                            setState(id, oldVal, true);
                        });
                    }
                    async function main() {
                        await connect();
                        await getDeviceInfo(outdoorDeviceId);
                        on({ id: dp_door, change: 'ne', ack: false }, async (evt) => {
                            //log(evt);
                            if (evt.state.val) {
                                await doorOpen(outdoorDeviceId);
                            } else {
                                await doorClose(outdoorDeviceId);
                            }
                        });
                        on({ id: dp_light, change: 'ne', ack: false }, async (evt) => {
                            //log(evt);
                            if (evt.state.val) {
                                await lightOn(outdoorDeviceId);
                            } else {
                                await lightOff(outdoorDeviceId);
                            }
                        });
                        on({ id: dp_doorstop, change: 'any', ack: false }, async (/* evt */) => {
                            //log(evt);
                            doorStop(outdoorDeviceId);
                            setState(dp_doorstop, false, true);
                        });
                        watchTimeState(dp_doortimeopen, 'Öffnungszeit');
                        watchTimeState(dp_doortimeclose, 'Schließzeit');
                        // await lightOn(outdoorDeviceId);
                        // await lightOff(outdoorDeviceId);
                     
                    }
                    main();
                    

                    Hi, habe jetzt noch einmal ein paar Datenpunkte hinzugefügt. Ab wann werden denn die Datenpunkte in beide Richtungen geschrieben? Als ich die Tür über das Skript aufgemacht habe, ging es sofort. Als ich in der APP beispielsweise den Mode von "time" auf "manual" gestellt habe, ging es auch nach 5 Minuten nicht, erst nachdem ch das skript neu gestaret habe.

                    OliverIOO Offline
                    OliverIOO Offline
                    OliverIO
                    schrieb am zuletzt editiert von
                    #33

                    @jwerlsdf

                    hah, glatt vergessen einzubauen.
                    ich hab dir auch noch einen knopf für manuellen refresh eingebaut. bitte datenpunkt gemäß anweisung anlegen

                    const token = 'XXXXXXXXXX';
                    const outdoorDeviceId = 'laTnMsWSJxBMGgJD';
                    const dp_light = '0_userdata.0.omlet.light'; // datenpunkt mit typ bool anlegen und namen hier eintragen
                    const dp_door = '0_userdata.0.omlet.door'; // datenpunkt mit typ bool anlegen und namen hier eintragen
                    const dp_doorstop = '0_userdata.0.omlet.doorstop'; // datenpunkt mit typ bool und rolle button anlegen und namen hier eintragen
                    const dp_doortimeopen = '0_userdata.0.omlet.doortimeopen'; // datenpunkt mit typ string, zeit im 24h format im format 06:00 oder 20:30
                    const dp_doortimeclose = '0_userdata.0.omlet.doortimeclose'; // datenpunkt mit typ string, zeit im 24h format im format 06:00 oder 20:30
                    const dp_devicedata = '0_userdata.0.omlet.devicedata'; //// datenpunkt mit typ string, hier wird nach jeder aktualisierung der aktuelle device status abgerufen und eingetragen
                    const dp_doormodeopen = "0_userdata.0.omlet.doormodeopen"; //// datenpunkt mit typ string, hier wird nach jeder aktualisierung der aktuelle device status abgerufen und eingetragen
                    const dp_doormodeclose = "0_userdata.0.omlet.doormodeclose"; //// datenpunkt mit typ string, hier wird nach jeder aktualisierung der aktuelle device status abgerufen und eingetragen
                    const dp_doorcloseLightLevel = "0_userdata.0.omlet.LightLevelclose"; //// datenpunkt mit typ number
                    const dp_dooropenLightLevel = "0_userdata.0.omlet.LightLevelopen";  //// datenpunkt mit typ number
                    const dp_dooropenDelay = "0_userdata.0.omlet.Delayopen";  //// datenpunkt mit typ number
                    const dp_doorcloseDelay = "0_userdata.0.omlet.Delayclose" //// datenpunkt mit typ number
                    
                    const dp_refresh = '0_userdata.0.omlet.refresh'; // datenpunkt mit typ bool und rolle button anlegen und namen hier eintragen
                    const refreshtime = 5; //time in seconds to refresh data
                    
                    // --
                    
                    const axios = require("axios/dist/browser/axios.cjs");
                    const BASE_URL = 'https://x107.omlet.co.uk/api/v1'; //kann hier geprüft werden https://smart.omlet.de/developers/api
                    let omlet = undefined;
                    
                    async function connect() {
                        console.log("start main2");
                        omlet = await omletAPIClient(token);
                        await getDevices(true);
                    
                        //console.log(JSON.stringify(await getDevices(), null, 2));
                    }
                    async function getDevices(info = false) {
                        try {
                            const devices = await getRequest('/device');
                            if (devices && info) {
                                devices.forEach(device => {
                                    console.log(`Device name: ${device.name}, deviceId: ${device.deviceId}`);
                                });
                            }
                            return devices;
                        } catch (error) {
                            console.log(error);
                            return [];
                        }
                    }
                    async function getDevice(deviceId) {
                        try {
                            const device = await getRequest(`/device/${deviceId}`);
                            return device;
                        } catch (error) {
                            console.log(error);
                            return {};
                        }
                    }
                    async function getConfiguration(deviceId) {
                        try {
                            const configuration = await getRequest(`/device/${deviceId}/configuration`);
                            return configuration;
                        } catch (error) {
                            console.log(error);
                            return {};
                        }
                    }
                    async function setConfiguration(deviceId, configuration) {
                        try {
                            const result = await patchRequest(`/device/${deviceId}/configuration`, configuration);
                    
                            return result;
                        } catch (error) {
                            console.log(error);
                            return {};
                        }
                    }
                    async function getDeviceInfo(deviceId) {
                        const device = await getDevice(deviceId);
                    
                        setState(dp_devicedata, JSON.stringify(device, null, 2), true);
                        setState(dp_doortimeopen, device.configuration.door.openTime, true);
                        setState(dp_doortimeclose, device.configuration.door.closeTime, true);
                        setState(dp_doormodeopen, device.configuration.door.openMode, true);
                        setState(dp_doormodeclose, device.configuration.door.closeMode, true);
                        setState(dp_doorcloseLightLevel, device.configuration.door.closeLightLevel, true);
                        setState(dp_dooropenLightLevel, device.configuration.door.openLightLevel, true);
                        setState(dp_dooropenDelay, device.configuration.door.openDelay, true);
                        setState(dp_doorcloseDelay, device.configuration.door.closeDelay, true);
                    }
                    async function doAction(deviceId, actionName) {
                        const devices = await getDevices();
                        const device = devices.find(device => device.deviceId === deviceId);
                        if (device) {
                            const action = device.actions.find(action => action.actionName === actionName);
                            if (action) {
                                await postRequest(action.url);
                                await getDeviceInfo(outdoorDeviceId);
                            }
                        }
                    }
                    async function lightOn(deviceId) {
                        await doAction(deviceId, 'on');
                    }
                    async function lightOff(deviceId) {
                        await doAction(deviceId, 'off');
                    }
                    async function doorOpen(deviceId) {
                        await doAction(deviceId, 'open');
                    }
                    async function doorClose(deviceId) {
                        await doAction(deviceId, 'close');
                    }
                    async function doorStop(deviceId) {
                        await doAction(deviceId, 'stop');
                    }
                    async function setDoorTimeClose(deviceId, time) {
                        const configuration = getConfiguration(deviceId);
                        const newConfiguration = {
                            ...configuration,
                            door: {
                                ...configuration.door,
                                closeTime: time
                            }
                        };
                        await setConfiguration(deviceId, newConfiguration);
                        await getDeviceInfo(outdoorDeviceId);
                    }
                    async function setDoorTimeOpen(deviceId, time) {
                        const configuration = getConfiguration(deviceId);
                        const newConfiguration = {
                            ...configuration,
                            door: {
                                ...configuration.door,
                                openTime: time
                            }
                        };
                        await setConfiguration(deviceId, newConfiguration);
                        await getDeviceInfo(outdoorDeviceId);
                    }
                    function omletAPIClient(token) {
                        return axios.create({
                            baseURL: BASE_URL,
                            headers: {
                                'Authorization': `Bearer ${token}`,
                                'Content-Type': 'application/json',
                            },
                        });
                    }
                    function isValidTime24h(value) {
                        return /^(?:[01]\d|2[0-3]):[0-5]\d$/.test(value);
                    }
                    async function getRequest(url) {
                        try {
                            const data = await omlet.get(url);
                            console.debug(`get request: ${url},status: ${data.status}`);
                            return data.data;
                        } catch (e) {
                            console.log(e);
                        }
                    }
                    async function postRequest(url) {
                        try {
                            const data = await omlet.post(url);
                            console.debug(`post request: ${url},status: ${data.status}`);
                            return data.data;
                        } catch (e) {
                            console.log(e);
                        }
                    }
                    async function patchRequest(url, data) {
                        try {
                            const result = await omlet.patch(url, data);
                            console.debug(`patch request: ${url},status: ${result.status}`);
                            return result.data;
                        } catch (e) {
                            console.log(e);
                        }
                    }
                    function watchTimeState(id, label) {
                        on({ id: id, change: 'ne', ack: false }, async (obj) => {
                            const newVal = String(obj.state.val || '').trim();
                            const oldVal = obj.oldState && obj.oldState.val != null
                                ? String(obj.oldState.val)
                                : '';
                            if (isValidTime24h(newVal)) {
                                log(`${label}: gültige Zeit gesetzt -> ${newVal}`, 'debug');
                                if (id === dp_doortimeopen) {
                                    await setDoorTimeOpen(outdoorDeviceId, newVal);
                                }
                                if (id === dp_doortimeclose) {
                                    await setDoorTimeClose(outdoorDeviceId, newVal);
                                }
                                return;
                            }
                            log(`${label}: ungültige Zeit "${newVal}" - setze auf "${oldVal}" zurück`, 'warn');
                    
                            // ungültige Eingabe zurücksetzen
                            setState(id, oldVal, true);
                        });
                    }
                    async function main() {
                        await connect();
                        await getDeviceInfo(outdoorDeviceId);
                        on({ id: dp_door, change: 'ne', ack: false }, async (evt) => {
                            //log(evt);
                            if (evt.state.val) {
                                await doorOpen(outdoorDeviceId);
                            } else {
                                await doorClose(outdoorDeviceId);
                            }
                        });
                        on({ id: dp_light, change: 'ne', ack: false }, async (evt) => {
                            //log(evt);
                            if (evt.state.val) {
                                await lightOn(outdoorDeviceId);
                            } else {
                                await lightOff(outdoorDeviceId);
                            }
                        });
                        on({ id: dp_doorstop, change: 'any', ack: false }, async (/* evt */) => {
                            //log(evt);
                            doorStop(outdoorDeviceId);
                            setState(dp_doorstop, false, true);
                        });
                        on({ id: dp_refresh, change: 'any', ack: false }, async (/* evt */) => {
                            //log(evt);
                            await getDeviceInfo(outdoorDeviceId);
                            setState(dp_refresh, false, true);
                        });
                        watchTimeState(dp_doortimeopen, 'Öffnungszeit');
                        watchTimeState(dp_doortimeclose, 'Schließzeit');
                        schedule(`*/${refreshtime} * * * *`, () => {
                            log('Will be triggered every 2 minutes!');
                        });
                        // await lightOn(outdoorDeviceId);
                        // await lightOff(outdoorDeviceId);
                    
                    }
                    main();
                    

                    Meine Adapter und Widgets
                    TVProgram, SqueezeboxRPC, OpenLiga, RSSFeed, MyTime,, pi-hole2, vis-json-template, skiinfo, vis-mapwidgets, vis-2-widgets-rssfeed
                    Links im Profil

                    1 Antwort Letzte Antwort
                    0
                    • J Offline
                      J Offline
                      jwerlsdf
                      schrieb am zuletzt editiert von
                      #34

                      Dank dir. Werde ich in den nächsten Tagen testen.

                      1 Antwort Letzte Antwort
                      0
                      Antworten
                      • In einem neuen Thema antworten
                      Anmelden zum Antworten
                      • Älteste zuerst
                      • Neuste zuerst
                      • Meiste Stimmen


                      Support us

                      ioBroker
                      Community Adapters
                      Donate

                      461

                      Online

                      32.7k

                      Benutzer

                      82.5k

                      Themen

                      1.3m

                      Beiträge
                      Community
                      Impressum | Datenschutz-Bestimmungen | Nutzungsbedingungen | Einwilligungseinstellungen
                      ioBroker Community 2014-2025
                      logo
                      • Anmelden

                      • Du hast noch kein Konto? Registrieren

                      • Anmelden oder registrieren, um zu suchen
                      • Erster Beitrag
                        Letzter Beitrag
                      0
                      • Home
                      • Aktuell
                      • Tags
                      • Ungelesen 0
                      • Kategorien
                      • Unreplied
                      • Beliebt
                      • GitHub
                      • Docu
                      • Hilfe