NEWS
Alexa Shopping List mit Bring synchronisieren
-
@heimweh
ich meine Diese Protokolleinträge :

ja, es sind nur infos und ich kann sie abstellen. aber es passiert ja doch ein permanentes schreiben Rtg. Alexa. ich bin nicht sicher ob das nicht zu viel traffic dort führt und die uns das irgendwann übel nehmen ;-)
@aruttkamp bei mir ist das nicht so. Hast Du den Bring Teil in meinen Script reingebastelt? Was ich mir gerade noch überlege - der Listen Teil soll ja wieder funktionieren (s.o.) - hat es damit evtl zu tun?
-
@heimweh
ich meine Diese Protokolleinträge :

ja, es sind nur infos und ich kann sie abstellen. aber es passiert ja doch ein permanentes schreiben Rtg. Alexa. ich bin nicht sicher ob das nicht zu viel traffic dort führt und die uns das irgendwann übel nehmen ;-)
@aruttkamp
Bei mir funktioniert es seit heute nicht mehr. Allerdings werden auch keine Alexa IOT Befehle erkannt oder die Alexa Summary aktualisiert. Auch Ankündigungen sind nicht mehr möglich. Hat das noch jemand? Hat Amazon da ein Problem? -
@aruttkamp
Bei mir funktioniert es seit heute nicht mehr. Allerdings werden auch keine Alexa IOT Befehle erkannt oder die Alexa Summary aktualisiert. Auch Ankündigungen sind nicht mehr möglich. Hat das noch jemand? Hat Amazon da ein Problem? -
@aruttkamp
Bei mir funktioniert es seit heute nicht mehr. Allerdings werden auch keine Alexa IOT Befehle erkannt oder die Alexa Summary aktualisiert. Auch Ankündigungen sind nicht mehr möglich. Hat das noch jemand? Hat Amazon da ein Problem? -
@Heimweh
Nein. Dein Scrikt ist das von hier - 1:1 kopiert und nur oben im Kopf die entspr. ID´s geändert@aruttkamp ich habe gar kein Bring sondern Todoist. Hoffe Du hast jetzt nichts gemischt. Der Todoist Script mit viel weniger Logeinträgen wäre der hier (bitte selber testen - hab leider keine Zeit gefunden):
const axios = require('axios'); // Konfiguration const todoistShoppingListId = 'XXXXXXXXXXX'; // <- Deine Projekt-ID const todoistToken = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; // <- Dein API-Token on({ id: 'alexa2.0.History.summary', change: 'any' }, function (obj) { const inputRaw = obj.state.val; if (typeof inputRaw !== 'string') { console.warn('[ToDoist] ⚠️ Kein String erkannt in alexa2.0.History.summary:', inputRaw); return; } const input = inputRaw.trim(); const match = input.match(/^setze (.+) auf (?:meine|die) (einkaufsliste|todo[\s-]?liste)/i); if (match && match.length >= 3) { const rohAufgabe = match[1]; const ziel = match[2].replace(/\s|-/g, '').toLowerCase(); const mitZiffern = wordsToNumbersSmart(rohAufgabe); const aufgabe = capitalizeFirst(mitZiffern); let projektId = null; if (ziel === 'einkaufsliste') { projektId = todoistShoppingListId; } console.log(`[ToDoist] 🧠 Alexa erkannt → "${aufgabe}" für Projekt: ${ziel}`); addTaskToTodoist(aufgabe, projektId); } }); function addTaskToTodoist(text, projectId = null) { const todoistData = { content: text }; if (projectId) todoistData.project_id = projectId; axios.post('https://api.todoist.com/rest/v2/tasks', todoistData, { headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${todoistToken}` } }) .then(() => { console.log(`[ToDoist] ✅ Aufgabe "${text}" erfolgreich zu Todoist hinzugefügt.`); }) .catch(error => { console.error('[ToDoist] ❌ Fehler beim Hinzufügen zu Todoist:', error.message || error.response?.data || error); }); } function capitalizeFirst(text) { if (!text || typeof text !== 'string') return ''; return text.charAt(0).toUpperCase() + text.slice(1); } function wordsToNumbersSmart(text) { const ones = { 'null': 0, 'eins': 1, 'eine': 1, 'einen': 1, 'zwei': 2, 'drei': 3, 'vier': 4, 'fünf': 5, 'sechs': 6, 'sieben': 7, 'acht': 8, 'neun': 9, 'zehn': 10, 'elf': 11, 'zwölf': 12, 'dreizehn': 13, 'vierzehn': 14, 'fünfzehn': 15, 'sechzehn': 16, 'siebzehn': 17, 'achtzehn': 18, 'neunzehn': 19 }; const tens = { 'zwanzig': 20, 'dreißig': 30, 'vierzig': 40, 'fünfzig': 50, 'sechzig': 60, 'siebzig': 70, 'achtzig': 80, 'neunzig': 90 }; const multipliers = { 'hundert': 100, 'tausend': 1000 }; const skipWords = ['und', 'oder', 'mit', 'für', 'pro']; const words = text.toLowerCase().split(/\s+/); const finalText = []; let i = 0; let capitalizeNext = 0; while (i < words.length) { const word = words[i]; if (ones[word] !== undefined) { if (i + 2 < words.length && words[i + 1] === 'und' && tens[words[i + 2]]) { const value = ones[word] + tens[words[i + 2]]; finalText.push(value.toString()); capitalizeNext = 2; i += 3; continue; } if (i + 1 < words.length && multipliers[words[i + 1]]) { const value = ones[word] * multipliers[words[i + 1]]; finalText.push(value.toString()); capitalizeNext = 2; i += 2; continue; } finalText.push(ones[word].toString()); capitalizeNext = 2; i++; } else if (tens[word] !== undefined) { finalText.push(tens[word].toString()); capitalizeNext = 2; i++; } else if (!isNaN(word)) { finalText.push(word); capitalizeNext = 2; i++; } else { if (capitalizeNext > 0 && !skipWords.includes(word)) { finalText.push(word.charAt(0).toUpperCase() + word.slice(1)); capitalizeNext--; } else { finalText.push(word); } i++; } } return finalText.join(' '); } -
@Heimweh - kann mit der neuen Version vom Alexa Adapter 3.27.x über die Datenpunkte in alexa2.0.Lists.SHOP mit TODOIST gesycnt oder zuverlässig nach TODOIST geschrieben werden? Aktuell klappt das bei mir mit deinem Skript nur in 30 % der Fälle.
Danke!
VG Martin@martin_olw sorry das ich mir so lange Zeit gelassen habe. Wenn ich das jetzt richtig gesehen habe - funktioniert mit der neuen Adapter Version der Datenpunkt alexa2.0.Lists.SHOP.json wieder - dann wäre ja alles wieder beim alten und mein erster Script den ich zum Thema Todoist reingestellt habe, müsste wieder funktionieren wie "früher" - ich hab jetzt den Script von damals nochmal korrigiert (er hat auf eine Testliste verwiesen) aber nicht getestet (mir fehlt die Zeit)
const axios = require('axios'); // Todoist API-Konfiguration const todoistProjectId = '12345678'; // Todoist-Projekt-ID const todoistToken = 'xXxXxXxXxXxXxXxXxXxXxXxX'; // Todoist-API-Token let previousList = []; // 1. Überwachen der Änderungen in der Alexa-Liste on({ id: 'alexa2.0.Lists.SHOP.json', change: 'any' }, function (obj) { try { console.log('Änderung in der Alexa Liste erkannt.'); // Alexa-Liste als JSON parsen const currentList = JSON.parse(obj.state.val); console.log('Alexa-Liste erfolgreich geparst.'); if (currentList && currentList.length > 0) { console.log(`Alexa-Liste enthält ${currentList.length} Einträge.`); // Neues Item zur Todoist-Liste hinzufügen, wenn die Liste gewachsen ist if (previousList.length < currentList.length) { const newItem = currentList.find(item => !previousList.some(prevItem => prevItem.id === item.id)); if (newItem) { console.log(`Neues Item erkannt: "${newItem.value}" mit ID: ${newItem.id}`); addTaskToTodoist(newItem.value); } } // Aktualisiere die vorherige Liste previousList = currentList; } else { console.log('Alexa-Liste ist leer oder nicht verfügbar.'); } } catch (e) { console.error('Fehler beim Parsen der Alexa-Liste:', e.message || e); } }); // 2. Periodische Synchronisierung mit Todoist (alle 1 Minute) schedule('*/1 * * * *', function () { console.log("Todoist-Aufgaben werden abgefragt..."); // Abrufen der aktiven Todoist-Aufgaben axios.get(`https://api.todoist.com/rest/v2/tasks`, { headers: { 'Authorization': `Bearer ${todoistToken}` } }) .then(response => { const activeTasks = response.data.map(task => task.content); // Extrahiere die Inhalte der aktiven Aufgaben console.log('Aktive Todoist-Aufgaben erfolgreich abgerufen.'); // Vergleich der aktiven Todoist-Aufgaben mit der Alexa-Liste syncAlexaWithTodoist(activeTasks); }) .catch(error => { console.error('Fehler beim Abrufen der Todoist-Aufgaben:', error.message || error.response.data); }); }); // 3. Hilfsfunktion zum Hinzufügen von Aufgaben zu Todoist function addTaskToTodoist(itemValue) { const todoistData = { content: itemValue, project_id: todoistProjectId }; axios.post('https://api.todoist.com/rest/v2/tasks', todoistData, { headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${todoistToken}` } }) .then(response => { console.log(`Item "${itemValue}" erfolgreich zu Todoist hinzugefügt.`); }) .catch(error => { console.error('Fehler beim Hinzufügen zu Todoist:', error.message || error); }); } // 4. Hilfsfunktion zur Synchronisierung der Alexa-Liste mit Todoist function syncAlexaWithTodoist(activeTasks) { const alexaList = JSON.parse(getState('alexa2.0.Lists.SHOP.json').val); alexaList.forEach(item => { if (!activeTasks.includes(item.value)) { // Markiere das Item in Alexa als "completed", wenn es nicht mehr in Todoist aktiv ist const completeState = `alexa2.0.Lists.SHOP.items.${item.id}.completed`; setState(completeState, true, function(err) { if (err) { console.error(`Fehler beim Setzen von "completed" für "${item.value}" in Alexa:`, err.message || err); } else { console.log(`Item "${item.value}" in Alexa erfolgreich als "completed" markiert.`); } }); } }); } -
@Heimweh - kann mit der neuen Version vom Alexa Adapter 3.27.x über die Datenpunkte in alexa2.0.Lists.SHOP mit TODOIST gesycnt oder zuverlässig nach TODOIST geschrieben werden? Aktuell klappt das bei mir mit deinem Skript nur in 30 % der Fälle.
Danke!
VG Martin@martin_olw Ich hab es jetzt doch getestet und noch was geändert:
Hier die Beschreibung:
Der Skript überwacht den Datenpunkt alexa2.0.Lists.SHOP.json.
Sobald Alexa z. B. sagt:
„Alexa, setze vier Äpfel auf die Einkaufsliste“
wird das als neuer Eintrag in dieser Liste erkannt.Zahlwort-Umwandlung:
Aus „vier Äpfel“ wird „4 Äpfel“.
Auch zusammengesetzte Zahlen wie „fünfzehn“ oder „dreiundzwanzig“ werden korrekt erkannt.
Das erste Wort nach der Zahl wird großgeschrieben: z. B. „4 Äpfel“ statt „4 äpfel“.
Todoist-Eintrag:
Der bereinigte und umgewandelte Eintrag wird direkt über die REST-API an Todoist gesendet und dort im definierten Projekt als Aufgabe eingetragen.
Automatisches Entfernen in Alexa:
Nach 60 Sekunden wird der passende Alexa-Listeneintrag automatisch als „abgehakt“ markiert.
Es muss nur die ListID und der Token eingesetzt werden.
const axios = require('axios'); // Todoist API-Konfiguration const todoistProjectId = 'XXXXXXXX'; // Deine Projekt-ID const todoistToken = 'XXXXXXXXXXXXXXXXXXXXXXX'; // Dein Token let previousList = []; // 1. Alexa-Änderungen überwachen on({ id: 'alexa2.0.Lists.SHOP.json', change: 'any' }, function (obj) { try { const currentList = JSON.parse(obj.state.val); if (currentList && currentList.length > 0) { if (previousList.length < currentList.length) { const newItem = currentList.find(item => !previousList.some(prevItem => prevItem.id === item.id) ); if (newItem) { const umgewandelt = wordsToNumbersSmart(newItem.value); const aufgabe = capitalizeFirst(umgewandelt); addTaskToTodoist(aufgabe); // Nach 60 Sekunden Alexa-Eintrag auf "completed" setzen setTimeout(() => { const alexaList = JSON.parse(getState('alexa2.0.Lists.SHOP.json').val); const matchingItem = alexaList.find(item => { const itemText = wordsToNumbersSmart(item.value).trim().toLowerCase(); return itemText === aufgabe.trim().toLowerCase(); }); if (matchingItem) { const completeState = `alexa2.0.Lists.SHOP.items.${matchingItem.id}.completed`; setState(completeState, true); } else { console.warn(`⚠️ Kein passender Alexa-Eintrag zu "${aufgabe}" gefunden.`); } }, 60 * 1000); // 60 Sekunden } } previousList = currentList; } } catch (e) { console.error('Fehler beim Parsen der Alexa-Liste:', e.message || e); } }); // 2. Aufgaben an Todoist senden function addTaskToTodoist(itemValue) { const todoistData = { content: itemValue, project_id: todoistProjectId }; axios.post('https://api.todoist.com/rest/v2/tasks', todoistData, { headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${todoistToken}` } }) .then(() => { console.log(`✅ "${itemValue}" zu Todoist hinzugefügt.`); }) .catch(error => { console.error('Fehler beim Hinzufügen zu Todoist:', error.message || error); }); } // 3. Erstes Wort groß function capitalizeFirst(text) { if (!text || typeof text !== 'string') return ''; return text.charAt(0).toUpperCase() + text.slice(1); } // 4. Wörter → Zahlen (z. B. „vierzehn“ → 14) function wordsToNumbersSmart(text) { const ones = { 'null': 0, 'eins': 1, 'eine': 1, 'einen': 1, 'zwei': 2, 'drei': 3, 'vier': 4, 'fünf': 5, 'sechs': 6, 'sieben': 7, 'acht': 8, 'neun': 9, 'zehn': 10, 'elf': 11, 'zwölf': 12, 'dreizehn': 13, 'vierzehn': 14, 'fünfzehn': 15, 'sechzehn': 16, 'siebzehn': 17, 'achtzehn': 18, 'neunzehn': 19 }; const tens = { 'zwanzig': 20, 'dreißig': 30, 'vierzig': 40, 'fünfzig': 50, 'sechzig': 60, 'siebzig': 70, 'achtzig': 80, 'neunzig': 90 }; const multipliers = { 'hundert': 100, 'tausend': 1000 }; const skipWords = ['und', 'oder', 'mit', 'für', 'pro']; const words = text.toLowerCase().split(/\s+/); const finalText = []; let i = 0; let capitalizeNext = 0; while (i < words.length) { const word = words[i]; if (ones[word] !== undefined) { if (i + 2 < words.length && words[i + 1] === 'und' && tens[words[i + 2]]) { const value = ones[word] + tens[words[i + 2]]; finalText.push(value.toString()); capitalizeNext = 2; i += 3; continue; } if (i + 1 < words.length && multipliers[words[i + 1]]) { const value = ones[word] * multipliers[words[i + 1]]; finalText.push(value.toString()); capitalizeNext = 2; i += 2; continue; } finalText.push(ones[word].toString()); capitalizeNext = 2; i++; } else if (tens[word] !== undefined) { finalText.push(tens[word].toString()); capitalizeNext = 2; i++; } else if (!isNaN(word)) { finalText.push(word); capitalizeNext = 2; i++; } else { if (capitalizeNext > 0 && !skipWords.includes(word)) { finalText.push(word.charAt(0).toUpperCase() + word.slice(1)); capitalizeNext--; } else { finalText.push(word); } i++; } } return finalText.join(' '); } -
@martin_olw Ich hab es jetzt doch getestet und noch was geändert:
Hier die Beschreibung:
Der Skript überwacht den Datenpunkt alexa2.0.Lists.SHOP.json.
Sobald Alexa z. B. sagt:
„Alexa, setze vier Äpfel auf die Einkaufsliste“
wird das als neuer Eintrag in dieser Liste erkannt.Zahlwort-Umwandlung:
Aus „vier Äpfel“ wird „4 Äpfel“.
Auch zusammengesetzte Zahlen wie „fünfzehn“ oder „dreiundzwanzig“ werden korrekt erkannt.
Das erste Wort nach der Zahl wird großgeschrieben: z. B. „4 Äpfel“ statt „4 äpfel“.
Todoist-Eintrag:
Der bereinigte und umgewandelte Eintrag wird direkt über die REST-API an Todoist gesendet und dort im definierten Projekt als Aufgabe eingetragen.
Automatisches Entfernen in Alexa:
Nach 60 Sekunden wird der passende Alexa-Listeneintrag automatisch als „abgehakt“ markiert.
Es muss nur die ListID und der Token eingesetzt werden.
const axios = require('axios'); // Todoist API-Konfiguration const todoistProjectId = 'XXXXXXXX'; // Deine Projekt-ID const todoistToken = 'XXXXXXXXXXXXXXXXXXXXXXX'; // Dein Token let previousList = []; // 1. Alexa-Änderungen überwachen on({ id: 'alexa2.0.Lists.SHOP.json', change: 'any' }, function (obj) { try { const currentList = JSON.parse(obj.state.val); if (currentList && currentList.length > 0) { if (previousList.length < currentList.length) { const newItem = currentList.find(item => !previousList.some(prevItem => prevItem.id === item.id) ); if (newItem) { const umgewandelt = wordsToNumbersSmart(newItem.value); const aufgabe = capitalizeFirst(umgewandelt); addTaskToTodoist(aufgabe); // Nach 60 Sekunden Alexa-Eintrag auf "completed" setzen setTimeout(() => { const alexaList = JSON.parse(getState('alexa2.0.Lists.SHOP.json').val); const matchingItem = alexaList.find(item => { const itemText = wordsToNumbersSmart(item.value).trim().toLowerCase(); return itemText === aufgabe.trim().toLowerCase(); }); if (matchingItem) { const completeState = `alexa2.0.Lists.SHOP.items.${matchingItem.id}.completed`; setState(completeState, true); } else { console.warn(`⚠️ Kein passender Alexa-Eintrag zu "${aufgabe}" gefunden.`); } }, 60 * 1000); // 60 Sekunden } } previousList = currentList; } } catch (e) { console.error('Fehler beim Parsen der Alexa-Liste:', e.message || e); } }); // 2. Aufgaben an Todoist senden function addTaskToTodoist(itemValue) { const todoistData = { content: itemValue, project_id: todoistProjectId }; axios.post('https://api.todoist.com/rest/v2/tasks', todoistData, { headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${todoistToken}` } }) .then(() => { console.log(`✅ "${itemValue}" zu Todoist hinzugefügt.`); }) .catch(error => { console.error('Fehler beim Hinzufügen zu Todoist:', error.message || error); }); } // 3. Erstes Wort groß function capitalizeFirst(text) { if (!text || typeof text !== 'string') return ''; return text.charAt(0).toUpperCase() + text.slice(1); } // 4. Wörter → Zahlen (z. B. „vierzehn“ → 14) function wordsToNumbersSmart(text) { const ones = { 'null': 0, 'eins': 1, 'eine': 1, 'einen': 1, 'zwei': 2, 'drei': 3, 'vier': 4, 'fünf': 5, 'sechs': 6, 'sieben': 7, 'acht': 8, 'neun': 9, 'zehn': 10, 'elf': 11, 'zwölf': 12, 'dreizehn': 13, 'vierzehn': 14, 'fünfzehn': 15, 'sechzehn': 16, 'siebzehn': 17, 'achtzehn': 18, 'neunzehn': 19 }; const tens = { 'zwanzig': 20, 'dreißig': 30, 'vierzig': 40, 'fünfzig': 50, 'sechzig': 60, 'siebzig': 70, 'achtzig': 80, 'neunzig': 90 }; const multipliers = { 'hundert': 100, 'tausend': 1000 }; const skipWords = ['und', 'oder', 'mit', 'für', 'pro']; const words = text.toLowerCase().split(/\s+/); const finalText = []; let i = 0; let capitalizeNext = 0; while (i < words.length) { const word = words[i]; if (ones[word] !== undefined) { if (i + 2 < words.length && words[i + 1] === 'und' && tens[words[i + 2]]) { const value = ones[word] + tens[words[i + 2]]; finalText.push(value.toString()); capitalizeNext = 2; i += 3; continue; } if (i + 1 < words.length && multipliers[words[i + 1]]) { const value = ones[word] * multipliers[words[i + 1]]; finalText.push(value.toString()); capitalizeNext = 2; i += 2; continue; } finalText.push(ones[word].toString()); capitalizeNext = 2; i++; } else if (tens[word] !== undefined) { finalText.push(tens[word].toString()); capitalizeNext = 2; i++; } else if (!isNaN(word)) { finalText.push(word); capitalizeNext = 2; i++; } else { if (capitalizeNext > 0 && !skipWords.includes(word)) { finalText.push(word.charAt(0).toUpperCase() + word.slice(1)); capitalizeNext--; } else { finalText.push(word); } i++; } } return finalText.join(' '); }@heimweh, danke für das Script, endlich kann man die Einkaufsliste in Alexa wieder benutzen.
previousList = currentList;sollte allerdings außerhalb des if-Zweigs gesetzt werden, ansonsten gibt es ein Problem, wenn man die Liste in der Alexa-App leert. Dann wird beim nächsten Aufruf die previousList mehr Inhalte haben als die currentList und die folgenden Einträge bis zum Neustart des Scripts oder bis die aktuelle Liste wieder einen Eintrag mehr hat als die vorherige Liste nicht mehr hinzugefügt.
-
Hallo zusammen,
da mich die Änderung seitens Amazon auch nervt habe ich auch mal auf die schnelle ein kleines TypeScript zusammengebastelt.
Es ist mit Sicherheit noch nicht perfekt, aber ein Anfang. Da ich es gerade erst fertig gestellt habe, hatte ich auch noch nicht die Möglichkeit und Zeit es ausgiebig zu testen.
Gerne stelle ich es aber hier allen Interessierten zur Verfügung.Zusätzliche Info:
Der Code-Dokumentationsblock am Anfang des Scripts gibt eine genauere Beschreibung des Scripts.
Nachdem Ihr den Inhalt der Textdatei in ein neues TypeScript kopiert habt, sollte es denke ich reichen die 5 Werte im Code-Block "SCRIPT SETUP" anzupassen.
JavaScript und TypeScript sind recht neu für mich und mit Sicherheit habe ich auch ungewollt das eine oder andere potentielle Problem ins Script eingebaut.
Ihr dürft mir natürlich gerne feedback geben.P.S. Ich habe gerade erst gesehen das @icastillo15 ebenfalls etwas gebastelt hat. Hätte ich mal erst hier gelesen, dann hätte ich mir meine Arbeit vielleicht sparen können.
:laughing: Ich werde sein Script auf jeden Fall in den nächsten Tagen ebenfalls mal testen.
Vielleicht kann ich ja was lernen.Grüße
Daniel
Script-File:
Shopping-List Sync.txtDamit das hinterlegt Script Stand 11/2025 wieder funktioniert, muss hier die Alexa Object-ID geändert werden
von/*---------- OBJECT-ID CONSTANTS ------------------------------------------------------------------------------------------------*/ const objectID_alexa_shoppingList_folder = 'alexa2.0.Lists.SHOPPING_LIST'; const objectID_alexa_shoppingList_addItem = 'alexa2.0.Lists.SHOPPING_LIST.#New';auf
/*---------- OBJECT-ID CONSTANTS ------------------------------------------------------------------------------------------------*/ const objectID_alexa_shoppingList_folder = 'alexa2.0.Lists.SHOP'; const objectID_alexa_shoppingList_addItem = 'alexa2.0.Lists.SHOP.#New';Andernfalls erhält man die 2 Fehler
script.js.Bring-Alexa-Sync: Cannot read properties of undefined (reading 'val') und script.js.Bring-Alexa-Sync: TypeError: Cannot read properties of undefined (reading 'stopScriptAsync') -
Danke an alle für diese tolle Lösung. Ich hatte nur das Problem, dass das sync script alle 2 Minuten Änderungen erkannt hatte, obwohl sich nur das Update Date geändert hatte. Ich habe keine Sachen in Bring oder Alexa hinzugefügt/gelöscht. Daher wollte ich unnötige updates der Listen vermeiden und habe das Skript für mich wie folgt angepasst.
/****************************************************** * Bring ↔ Alexa List Sync (bidirectional) – MASTER VERSION * Author: SiL4S ******************************************************/ // List Object IDs const bringBaseId = 'bring.0.2c7bf13d-3896-4632-8a4e-c1d5a4993ce5'; const alexa2BaseId = 'alexa2.1.Lists.SHOP'; // Bring datapoints const bringListId = bringBaseId + '.content'; const bringListCompletedId = bringBaseId + '.recentContent'; const bringAddToList = bringBaseId + '.saveItem'; const bringCompleteItem = bringBaseId + '.moveToRecentContent'; // Alexa datapoints const alexaAddToList = alexa2BaseId + '.#New'; const alexaListId = alexa2BaseId + '.json'; // Loop-protection let lastAlexaJson = ""; let lastBringJson = ""; // Debug logging const printDebug = true; function debug(msg) { if (printDebug) log(msg); } // Bring Status const TodoItemStatus = { NeedsAction: 'needs_action', Completed: 'completed', }; // Normalize item names (trim, collapse spaces, fix casing) function normalize(name = '') { return name .trim() .replace(/\s+/g, ' ') .toLowerCase() .replace(/(^\w|\s\w)/g, m => m.toUpperCase()); } // Compare completed-state between Alexa and Bring items function compareCompleted(alexaItem, bringItem) { return ( (alexaItem.completed && bringItem.status === TodoItemStatus.Completed) || (!alexaItem.completed && bringItem.status === TodoItemStatus.NeedsAction) ); } // Remove duplicate Alexa entries function eliminateDuplicated() { const raw = getState(alexaListId).val; if (!raw) return; const list = JSON.parse(raw); const seen = {}; for (const i of list) { const normalized = normalize(i.value); if (seen[normalized]) { debug(`Deleting duplicate Alexa item: ${i.value}`); setState(`${alexa2BaseId}.items.${i.id}.#delete`, true); } else { seen[normalized] = true; } } } //----------------------------------------------- // Main Sync Function //----------------------------------------------- function syncLists(alexaList, bringList, bringRecent, timestampBring, source) { //---------------------------------------------------- // 1) Step: Process Alexa items (add / complete) //---------------------------------------------------- for (const alexaItem of alexaList) { const nameNorm = normalize(alexaItem.value); let foundInBring = false; let foundInRecent = false; // Bring "active" items for (const b of bringList) { if (normalize(b.name) === nameNorm) { foundInBring = true; // Completed syncing if (source === 'Alexa' && alexaItem.completed && !compareCompleted(alexaItem, b)) { debug(`→ Alexa COMPLETED → Mark completed in Bring: ${b.name}`); setState(bringCompleteItem, b.name); } if (source === 'Bring' && !compareCompleted(alexaItem, b)) { debug(`→ Bring COMPLETED → Mark completed in Alexa: ${alexaItem.value}`); setState(`${alexa2BaseId}.items.${alexaItem.id}.completed`, b.status === TodoItemStatus.Completed); } } } // Bring recentContent for (const r of bringRecent) { if (normalize(r.name) === nameNorm) { foundInRecent = true; if (source === 'Alexa' && !alexaItem.completed) { debug(`→ Alexa ACTIVE → Add item back to Bring: ${r.name}`); setState(bringAddToList, r.name); } if (source === 'Bring' && alexaItem.completed === false) { debug(`→ Bring has this item completed → Delete from Alexa: ${alexaItem.value}`); setState(`${alexa2BaseId}.items.${alexaItem.id}.#delete`, true); } } } //---------------------------------------------------------- // Item exists in Alexa but not in Bring → delete or push //---------------------------------------------------------- if (!foundInBring && !foundInRecent) { if (source === 'Alexa') { debug(`→ New Alexa item → Adding to Bring: ${alexaItem.value}`); setState(bringAddToList, nameNorm); } if (source === 'Bring') { debug(`→ Bring deleted item → Deleting from Alexa: ${alexaItem.value}`); setState(`${alexa2BaseId}.items.${alexaItem.id}.#delete`, true); } } } //---------------------------------------------------- // 2) Step: Push Bring items to Alexa //---------------------------------------------------- if (source === 'Bring') { const alexaNames = alexaList.map(a => normalize(a.value)); for (const b of bringList) { const bNorm = normalize(b.name); if (!alexaNames.includes(bNorm) && b.status !== TodoItemStatus.Completed) { debug(`→ New Bring item → Adding to Alexa: ${b.name}`); setState(alexaAddToList, b.name); } } } } //----------------------------------------------- // Full Sync Runner //----------------------------------------------- function doSync(source) { debug(`--- SYNC START (${source}) ---`); eliminateDuplicated(); const rawAlexa = getState(alexaListId).val; const rawBring = getState(bringListId).val; const rawRecent = getState(bringListCompletedId).val; if (!rawAlexa || !rawBring || !rawRecent) { debug('Missing datapoints!'); return; } const alexaList = JSON.parse(rawAlexa); const bringList = JSON.parse(rawBring); const bringRecent = JSON.parse(rawRecent); const bringTS = getState(bringListId).ts; debug('Processing…'); syncLists(alexaList, bringList, bringRecent, bringTS, source); debug(`--- SYNC FINISHED (${source}) ---`); } //----------------------------------------------- // Bring Trigger //----------------------------------------------- on({ id: bringListId, change: "any" }, () => { const now = getState(bringListId).val; if (now === lastBringJson) { debug('Bring: Update ignored (not real change)'); return; } lastBringJson = now; debug('Bring → Real change detected'); doSync('Bring'); }); //----------------------------------------------- // Alexa Trigger //----------------------------------------------- on({ id: alexaListId, change: "any" }, () => { const now = getState(alexaListId).val; if (now === lastAlexaJson) { debug('Alexa: Update ignored (polling only)'); return; } lastAlexaJson = now; debug('Alexa → Real change detected'); doSync('Alexa'); }); //----------------------------------------------- // Initial sync at startup //----------------------------------------------- doSync('Startup'); -
Danke an alle für diese tolle Lösung. Ich hatte nur das Problem, dass das sync script alle 2 Minuten Änderungen erkannt hatte, obwohl sich nur das Update Date geändert hatte. Ich habe keine Sachen in Bring oder Alexa hinzugefügt/gelöscht. Daher wollte ich unnötige updates der Listen vermeiden und habe das Skript für mich wie folgt angepasst.
/****************************************************** * Bring ↔ Alexa List Sync (bidirectional) – MASTER VERSION * Author: SiL4S ******************************************************/ // List Object IDs const bringBaseId = 'bring.0.2c7bf13d-3896-4632-8a4e-c1d5a4993ce5'; const alexa2BaseId = 'alexa2.1.Lists.SHOP'; // Bring datapoints const bringListId = bringBaseId + '.content'; const bringListCompletedId = bringBaseId + '.recentContent'; const bringAddToList = bringBaseId + '.saveItem'; const bringCompleteItem = bringBaseId + '.moveToRecentContent'; // Alexa datapoints const alexaAddToList = alexa2BaseId + '.#New'; const alexaListId = alexa2BaseId + '.json'; // Loop-protection let lastAlexaJson = ""; let lastBringJson = ""; // Debug logging const printDebug = true; function debug(msg) { if (printDebug) log(msg); } // Bring Status const TodoItemStatus = { NeedsAction: 'needs_action', Completed: 'completed', }; // Normalize item names (trim, collapse spaces, fix casing) function normalize(name = '') { return name .trim() .replace(/\s+/g, ' ') .toLowerCase() .replace(/(^\w|\s\w)/g, m => m.toUpperCase()); } // Compare completed-state between Alexa and Bring items function compareCompleted(alexaItem, bringItem) { return ( (alexaItem.completed && bringItem.status === TodoItemStatus.Completed) || (!alexaItem.completed && bringItem.status === TodoItemStatus.NeedsAction) ); } // Remove duplicate Alexa entries function eliminateDuplicated() { const raw = getState(alexaListId).val; if (!raw) return; const list = JSON.parse(raw); const seen = {}; for (const i of list) { const normalized = normalize(i.value); if (seen[normalized]) { debug(`Deleting duplicate Alexa item: ${i.value}`); setState(`${alexa2BaseId}.items.${i.id}.#delete`, true); } else { seen[normalized] = true; } } } //----------------------------------------------- // Main Sync Function //----------------------------------------------- function syncLists(alexaList, bringList, bringRecent, timestampBring, source) { //---------------------------------------------------- // 1) Step: Process Alexa items (add / complete) //---------------------------------------------------- for (const alexaItem of alexaList) { const nameNorm = normalize(alexaItem.value); let foundInBring = false; let foundInRecent = false; // Bring "active" items for (const b of bringList) { if (normalize(b.name) === nameNorm) { foundInBring = true; // Completed syncing if (source === 'Alexa' && alexaItem.completed && !compareCompleted(alexaItem, b)) { debug(`→ Alexa COMPLETED → Mark completed in Bring: ${b.name}`); setState(bringCompleteItem, b.name); } if (source === 'Bring' && !compareCompleted(alexaItem, b)) { debug(`→ Bring COMPLETED → Mark completed in Alexa: ${alexaItem.value}`); setState(`${alexa2BaseId}.items.${alexaItem.id}.completed`, b.status === TodoItemStatus.Completed); } } } // Bring recentContent for (const r of bringRecent) { if (normalize(r.name) === nameNorm) { foundInRecent = true; if (source === 'Alexa' && !alexaItem.completed) { debug(`→ Alexa ACTIVE → Add item back to Bring: ${r.name}`); setState(bringAddToList, r.name); } if (source === 'Bring' && alexaItem.completed === false) { debug(`→ Bring has this item completed → Delete from Alexa: ${alexaItem.value}`); setState(`${alexa2BaseId}.items.${alexaItem.id}.#delete`, true); } } } //---------------------------------------------------------- // Item exists in Alexa but not in Bring → delete or push //---------------------------------------------------------- if (!foundInBring && !foundInRecent) { if (source === 'Alexa') { debug(`→ New Alexa item → Adding to Bring: ${alexaItem.value}`); setState(bringAddToList, nameNorm); } if (source === 'Bring') { debug(`→ Bring deleted item → Deleting from Alexa: ${alexaItem.value}`); setState(`${alexa2BaseId}.items.${alexaItem.id}.#delete`, true); } } } //---------------------------------------------------- // 2) Step: Push Bring items to Alexa //---------------------------------------------------- if (source === 'Bring') { const alexaNames = alexaList.map(a => normalize(a.value)); for (const b of bringList) { const bNorm = normalize(b.name); if (!alexaNames.includes(bNorm) && b.status !== TodoItemStatus.Completed) { debug(`→ New Bring item → Adding to Alexa: ${b.name}`); setState(alexaAddToList, b.name); } } } } //----------------------------------------------- // Full Sync Runner //----------------------------------------------- function doSync(source) { debug(`--- SYNC START (${source}) ---`); eliminateDuplicated(); const rawAlexa = getState(alexaListId).val; const rawBring = getState(bringListId).val; const rawRecent = getState(bringListCompletedId).val; if (!rawAlexa || !rawBring || !rawRecent) { debug('Missing datapoints!'); return; } const alexaList = JSON.parse(rawAlexa); const bringList = JSON.parse(rawBring); const bringRecent = JSON.parse(rawRecent); const bringTS = getState(bringListId).ts; debug('Processing…'); syncLists(alexaList, bringList, bringRecent, bringTS, source); debug(`--- SYNC FINISHED (${source}) ---`); } //----------------------------------------------- // Bring Trigger //----------------------------------------------- on({ id: bringListId, change: "any" }, () => { const now = getState(bringListId).val; if (now === lastBringJson) { debug('Bring: Update ignored (not real change)'); return; } lastBringJson = now; debug('Bring → Real change detected'); doSync('Bring'); }); //----------------------------------------------- // Alexa Trigger //----------------------------------------------- on({ id: alexaListId, change: "any" }, () => { const now = getState(alexaListId).val; if (now === lastAlexaJson) { debug('Alexa: Update ignored (polling only)'); return; } lastAlexaJson = now; debug('Alexa → Real change detected'); doSync('Alexa'); }); //----------------------------------------------- // Initial sync at startup //----------------------------------------------- doSync('Startup');