NEWS
Omlet Hühnerstall webhook API
-
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).
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 bessermeine aktuellen experimente für das licht hat den status 204 zurückgegeben.
der doku nach bedeutet dasAction 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(); -
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. -
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.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 beachtenconst 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 eingetragenwenn 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) aufrufenSind die Funktionen soweit verständlich? Brauchst du mehr Detaildokumentation für spätere Anpassungen?
-
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. -
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.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(); -
// -- 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.
-
// -- 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.
hah, glatt vergessen einzubauen.
ich hab dir auch noch einen knopf für manuellen refresh eingebaut. bitte datenpunkt gemäß anweisung anlegenconst 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(); -
Hi, aktuell was ich beobachtet habe, ja. Die Rückmeldung wollte ich erst geben, wenn ich den Futterautomat erhalten habe, der aber bisher noch nicht geliefert wurde. Auch diesen kann man mittels API einbinden. Sollte es bei der Einbindung Probleme geben, melde ih mich noch mal, ansonsten aber auch so.
-
wahrscheinlich sind wir noch nicht fertig, aber die erste stufe ist schon mal da.
das skript wurde (mithilfe von KI) soweit umgestellt, das du keine device id mehr heraussuchen musst. das skript ruft einfach die informationen ab und legt die datenpunkte je device an.
die informationen werden dann alle 5 sekunden aktualisiert.
prüfe bitte mal, ob die informationen alle so sinnvoll sind.
auch ob die angelegten datenpunkte für den einzelnen device typ passen.
prüfe auch ob die steuerung noch so funktioniert. das kann ich hier schlecht ausprobieren.da das skript den befehl setObjekt zum anlegen von Datenpunkte verwendet musst du in den adapter einstellungen des javascript adapters die folgende option setzen

const token = "xxx"; const BASE_DP = "0_userdata.0.omlet"; const refreshtime = 5; // Sekunden const axios = require("axios"); const BASE_URL = "https://x107.omlet.co.uk/api/v1"; let omlet; const initializedDevices = new Set(); function cleanId(input) { return String(input || "unknown") .normalize("NFD") .replace(/[\u0300-\u036f]/g, "") .replace(/[^a-zA-Z0-9_]+/g, "_") .replace(/_+/g, "_") .replace(/^_+|_+$/g, ""); } function getDeviceBase(device) { return `${BASE_DP}.${cleanId(device.name)}`; } function getType(value) { if (typeof value === "boolean") return "boolean"; if (typeof value === "number") return "number"; return "string"; } async function ensureFolder(id, name) { if (!existsObject(id)) { setObject(id, { type: "folder", common: { name }, native: {}, }); } } async function ensureState(id, value, common) { if (!existsObject(id)) { await createStateAsync(id, value, common); } } async function ensureDeviceDatapoints(device) { const base = getDeviceBase(device); await ensureFolder(BASE_DP, "Omlet"); await ensureFolder(base, device.name); await ensureState(`${base}.devicedata`, "", { name: "Device JSON Daten", type: "string", role: "json", read: true, write: false, }); await ensureState(`${base}.refresh`, false, { name: "Refresh", type: "boolean", role: "button", read: true, write: true, }); await ensureState(`${base}.door`, false, { name: "Tür / Klappe öffnen oder schließen", type: "boolean", role: "switch", read: true, write: true, }); await ensureState(`${base}.doorstop`, false, { name: "Tür / Klappe stoppen", type: "boolean", role: "button", read: true, write: true, }); const hasLight = Array.isArray(device.actions) && device.actions.some(a => a.actionName === "on" || a.actionName === "off"); if (hasLight) { await ensureState(`${base}.light`, false, { name: "Licht", type: "boolean", role: "switch", read: true, write: true, }); } if (device.configuration && device.configuration.door) { await ensureDoorConfigurationDatapoints(base); } } async function ensureDoorConfigurationDatapoints(base) { await ensureState(`${base}.doortimeopen`, "", { name: "Öffnungszeit", type: "string", role: "text", read: true, write: true, }); await ensureState(`${base}.doortimeclose`, "", { name: "Schließzeit", type: "string", role: "text", read: true, write: true, }); await ensureState(`${base}.doormodeopen`, "", { name: "Öffnungsmodus", type: "string", role: "text", read: true, write: true, }); await ensureState(`${base}.doormodeclose`, "", { name: "Schließmodus", type: "string", role: "text", read: true, write: true, }); await ensureState(`${base}.LightLevelclose`, 0, { name: "Schließen bei Lichtwert", type: "number", role: "value", read: true, write: true, }); await ensureState(`${base}.LightLevelopen`, 0, { name: "Öffnen bei Lichtwert", type: "number", role: "value", read: true, write: true, }); await ensureState(`${base}.Delayopen`, 0, { name: "Öffnungsverzögerung", type: "number", role: "value", read: true, write: true, }); await ensureState(`${base}.Delayclose`, 0, { name: "Schließverzögerung", type: "number", role: "value", read: true, write: true, }); } async function updateDeviceDatapoints(device) { const base = getDeviceBase(device); await setStateAsync(`${base}.devicedata`, JSON.stringify(device, null, 2), true); if (device.state && device.state.door && device.state.door.state) { await setStateAsync(`${base}.door`, device.state.door.state === "open", true); } if (device.state && device.state.feeder && device.state.feeder.state) { await setStateAsync(`${base}.door`, device.state.feeder.state === "open", true); } if (existsObject(`${base}.light`) && device.state && device.state.light && device.state.light.state) { await setStateAsync(`${base}.light`, device.state.light.state === "on", true); } if (device.configuration && device.configuration.door) { const door = device.configuration.door; await setStateAsync(`${base}.doortimeopen`, door.openTime || "", true); await setStateAsync(`${base}.doortimeclose`, door.closeTime || "", true); await setStateAsync(`${base}.doormodeopen`, door.openMode || "", true); await setStateAsync(`${base}.doormodeclose`, door.closeMode || "", true); await setStateAsync(`${base}.LightLevelclose`, door.closeLightLevel ?? 0, true); await setStateAsync(`${base}.LightLevelopen`, door.openLightLevel ?? 0, true); await setStateAsync(`${base}.Delayopen`, door.openDelay ?? 0, true); await setStateAsync(`${base}.Delayclose`, door.closeDelay ?? 0, true); } } async function refreshDevice(deviceId) { const device = await getDevice(deviceId); if (!device || !device.deviceId) return; await ensureDeviceDatapoints(device); await updateDeviceDatapoints(device); } async function refreshAllDevices() { const devices = await getDevices(); for (const device of devices) { await refreshDevice(device.deviceId); } } function isValidTime24h(value) { return /^(?:[01]\d|2[0-3]):[0-5]\d$/.test(String(value)); } async function updateDoorConfiguration(deviceId, key, value) { const configuration = await getConfiguration(deviceId); if (!configuration || !configuration.door) return; configuration.door[key] = value; await setConfiguration(deviceId, configuration); await refreshDevice(deviceId); } function setupDeviceWatcher(device) { const base = getDeviceBase(device); if (initializedDevices.has(device.deviceId)) return; initializedDevices.add(device.deviceId); on({ id: `${base}.door`, change: "ne", ack: false }, async evt => { if (evt.state.val) { await doAction(device.deviceId, "open"); } else { await doAction(device.deviceId, "close"); } await refreshDevice(device.deviceId); }); if (existsObject(`${base}.light`)) { on({ id: `${base}.light`, change: "ne", ack: false }, async evt => { if (evt.state.val) { await doAction(device.deviceId, "on"); } else { await doAction(device.deviceId, "off"); } await refreshDevice(device.deviceId); }); } on({ id: `${base}.doorstop`, change: "any", ack: false }, async () => { await doAction(device.deviceId, "stop"); await setStateAsync(`${base}.doorstop`, false, true); await refreshDevice(device.deviceId); }); on({ id: `${base}.refresh`, change: "any", ack: false }, async () => { await refreshDevice(device.deviceId); await setStateAsync(`${base}.refresh`, false, true); }); if (device.configuration && device.configuration.door) { watchDoorConfiguration(device, base); } log(`Omlet Watcher angelegt: ${device.name} (${device.deviceId})`); } function watchDoorConfiguration(device, base) { on({ id: `${base}.doortimeopen`, change: "ne", ack: false }, async evt => { const value = String(evt.state.val || "").trim(); if (!isValidTime24h(value)) { log(`Ungültige Öffnungszeit: ${value}`, "warn"); await refreshDevice(device.deviceId); return; } await updateDoorConfiguration(device.deviceId, "openTime", value); }); on({ id: `${base}.doortimeclose`, change: "ne", ack: false }, async evt => { const value = String(evt.state.val || "").trim(); if (!isValidTime24h(value)) { log(`Ungültige Schließzeit: ${value}`, "warn"); await refreshDevice(device.deviceId); return; } await updateDoorConfiguration(device.deviceId, "closeTime", value); }); on({ id: `${base}.doormodeopen`, change: "ne", ack: false }, async evt => { await updateDoorConfiguration(device.deviceId, "openMode", String(evt.state.val)); }); on({ id: `${base}.doormodeclose`, change: "ne", ack: false }, async evt => { await updateDoorConfiguration(device.deviceId, "closeMode", String(evt.state.val)); }); on({ id: `${base}.LightLevelclose`, change: "ne", ack: false }, async evt => { await updateDoorConfiguration(device.deviceId, "closeLightLevel", Number(evt.state.val)); }); on({ id: `${base}.LightLevelopen`, change: "ne", ack: false }, async evt => { await updateDoorConfiguration(device.deviceId, "openLightLevel", Number(evt.state.val)); }); on({ id: `${base}.Delayopen`, change: "ne", ack: false }, async evt => { await updateDoorConfiguration(device.deviceId, "openDelay", Number(evt.state.val)); }); on({ id: `${base}.Delayclose`, change: "ne", ack: false }, async evt => { await updateDoorConfiguration(device.deviceId, "closeDelay", Number(evt.state.val)); }); } 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 { return await getRequest(`/device/${deviceId}`); } catch (error) { console.log(error); return {}; } } async function getConfiguration(deviceId) { try { return await getRequest(`/device/${deviceId}/configuration`); } catch (error) { console.log(error); return {}; } } async function setConfiguration(deviceId, configuration) { try { return await patchRequest(`/device/${deviceId}/configuration`, configuration); } catch (error) { console.log(error); return {}; } } async function doAction(deviceId, actionName) { const device = await getDevice(deviceId); if (!device || !Array.isArray(device.actions)) return; const action = device.actions.find(a => a.actionName === actionName); if (!action) { log(`Aktion "${actionName}" für Gerät "${device.name}" nicht verfügbar`, "warn"); return; } log(`Omlet Aktion: ${device.name} -> ${actionName}`); await postRequest(action.url); } function omletAPIClient(token) { return axios.create({ baseURL: BASE_URL, headers: { Authorization: `Bearer ${token}`, "Content-Type": "application/json", }, }); } async function getRequest(url) { const data = await omlet.get(url); console.debug(`get request: ${url}, status: ${data.status}`); return data.data; } async function postRequest(url) { const data = await omlet.post(url); console.debug(`post request: ${url}, status: ${data.status}`); return data.data; } async function patchRequest(url, data) { const result = await omlet.patch(url, data); console.debug(`patch request: ${url}, status: ${result.status}`); return result.data; } async function main() { omlet = omletAPIClient(token); await refreshAllDevices(); const devices = await getDevices(true); for (const device of devices) { const fullDevice = await getDevice(device.deviceId); await ensureDeviceDatapoints(fullDevice); setupDeviceWatcher(fullDevice); } setInterval(async () => { await refreshAllDevices(); }, refreshtime * 1000); } main(); -
ok, skript importiert und es kommen daten bereits rein. Er baut auch die Struktur 1 Baum (omlet) mit zwei Zweigen (Devices) auf. Die Struktur von der Tür scheint auf den ersten Blick ok zu sein.
Die Struktur des Feeders ist falsch:
Es gibt 4 Datenpunkte:- devicedata ist korrekt und es kommen wichtige Daten rein.
2-4 ist falsch: Door, Doorstop, refresh (wobei refesh könnte man als manuellen anstoß drinnen lassen)
- devicedata ist korrekt und es kommen wichtige Daten rein.
-
ok, skript importiert und es kommen daten bereits rein. Er baut auch die Struktur 1 Baum (omlet) mit zwei Zweigen (Devices) auf. Die Struktur von der Tür scheint auf den ersten Blick ok zu sein.
Die Struktur des Feeders ist falsch:
Es gibt 4 Datenpunkte:- devicedata ist korrekt und es kommen wichtige Daten rein.
2-4 ist falsch: Door, Doorstop, refresh (wobei refesh könnte man als manuellen anstoß drinnen lassen)
- devicedata ist korrekt und es kommen wichtige Daten rein.
Hey! Du scheinst an dieser Unterhaltung interessiert zu sein, hast aber noch kein Konto.
Hast du es satt, bei jedem Besuch durch die gleichen Beiträge zu scrollen? Wenn du dich für ein Konto anmeldest, kommst du immer genau dorthin zurück, wo du zuvor warst, und kannst dich über neue Antworten benachrichtigen lassen (entweder per E-Mail oder Push-Benachrichtigung). Du kannst auch Lesezeichen speichern und Beiträge positiv bewerten, um anderen Community-Mitgliedern deine Wertschätzung zu zeigen.
Mit deinem Input könnte dieser Beitrag noch besser werden 💗
Registrieren Anmelden