NEWS
[Frage] javascript-code zum Auslesen von Ertragswerten
-
So sollte es gehen.
Schlüssel ist das es 3 Trigger auf Selektoren gibt, die jeweils alle gleichartigen Datenpunkte erfassen, so das dann das Abspeichern entsprechend laufen kann.
Die Abfrage das der Wert nicht 0 sein soll ist wichtig, damit ein 0-Setzen des Wertes nicht in die Datenbank geschrieben wird, wenn am Anfang des Tages alle DP auf 0 gesetzt werden.testen konnte ich es nur teilweise - ich hab weder einen Fronius noch eine Influxdb zum rein schreiben.
Es sollte trotzdem gehen, da ich am Ende nur den einen Trigger durch 3 Trigger auf die 3 Selektoren ersetzt hab, deswegen muss ich den 'key' manuell übergeben, und das eigentliche Schreiben ist ausgelagert in eine externe funtktion.
A.
// Kopiert Daten für eine Zeitreihendarstellung in die influxdb2 // script von von Matthias Kleine // https://haus-automatisierung.com/software/2023/05/11/influxdb2-pv-dashboard.html // v.0.3 // const influxDbInstance = 'influxdb.0'; const token = '***mein token***'; const measurement = 'energy-stats'; // Modifikation des scripts von Matthias Kleine um aus den aggregierten fronius-Daten für den Tag den // entsprechenden Tag des Monats auslesen zu können. // Diese Umsetzung kommt von "ticaki" und "codierknecht" aus dem // iob-forum deutsch. Posts dazu siehe // https://forum.iobroker.net/topic/80183/frage-javascript-code-zum-auslesen-von-ertragswerten/8?_=1741455458605 const loggingObj = {}; async function Store(key, value) { loggingObj[key] = obj.state.val; // Save Data const data = `${measurement} ${Object.keys(loggingObj) .filter(key => !isNaN(loggingObj[key])) .map((key) => `${key}=${loggingObj[key]}`) .join(',')}`; if (data) { // console.log(`Saving "${data}" to InfluxDB @ ${protocol}://${host}:${port}/`); httpPostAsync(`${protocol}://${host}:${port}/api/v2/write?bucket=${bucket}&org=${org}`, data, { headers: { 'Content-Type': 'text/plain', 'Authorization': `Token ${token}` } }).catch(err => console.error(err)); } } async function start() { const influxDbInstanceConfig = await getObjectAsync(`system.adapter.${influxDbInstance}`); const protocol = influxDbInstanceConfig.native.protocol; const host = influxDbInstanceConfig.native.host; const port = influxDbInstanceConfig.native.port; const org = influxDbInstanceConfig.native.organization; const bucket = influxDbInstanceConfig.native.dbname; console.log(`Starting "${measurement}" logging to ${protocol}://${host}:${port} into bucket "${bucket}" by org ${org}`); // Init loggingObj with current values for (let [objId, key] of Object.entries(loggingTemplate)) { const state = await getStateAsync(objId); if (state && !isNaN(state.val)) { loggingObj[key] = state.val; } else { loggingObj[key] = 0; } } on({ id: $('channel[state.id=fronius-solarweb.0.96605a5f-da06-4373-b8d5-b985faa71359.day.EnergyFeedIn.values.*'), change: 'ne' }, async (obj) => { if (obj.state.val != 0) Store('exportedWh', obj.state.val) }); on({ id: $('channel[state.id=fronius-solarweb.0.96605a5f-da06-4373-b8d5-b985faa71359.day.EnergyPurchased.values.*'), change: 'ne' }, async (obj) => { if (obj.state.val != 0) Store('importedWh', obj.state.val) }); on({ id: $('channel[state.id=fronius-solarweb.0.96605a5f-da06-4373-b8d5-b985faa71359.day.EnergyProductionTotal.values.*'), change: 'ne' }, async (obj) => { if (obj.state.val != 0) Store('generatedWh', obj.state.val) }); } start();@asgothian
Habe es doch gerade ausprobiert. Gibt aber eine Fehlermeldung:javascript.0 19:32:52.547 info Stopping script script.js.smarthome_trost.neu_history_influxdb javascript.0 19:32:52.592 info Start JavaScript script.js.smarthome_trost.neu_history_influxdb (Javascript/js) javascript.0 19:32:52.595 info script.js.smarthome_trost.neu_history_influxdb: registered 0 subscriptions, 0 schedules, 0 messages, 0 logs and 0 file subscriptions javascript.0 19:32:52.597 info script.js.smarthome_trost.neu_history_influxdb: Starting "energy-stats" logging to http://localhost:8086 into bucket "iobrokerdata" by org Trost javascript.0 19:32:52.598 error script.js.smarthome_trost.neu_history_influxdb: ReferenceError: loggingTemplate is not defined javascript.0 19:32:52.598 error at start (script.js.smarthome_trost.neu_history_influxdb:53:45)Kann man das reparieren?