Weiter zum Inhalt
  • Home
  • Aktuell
  • Tags
  • 0 Ungelesen 0
  • Kategorien
  • Unreplied
  • Beliebt
  • GitHub
  • Docu
  • Hilfe
Skins
  • Hell
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dunkel
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

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

Community Forum

donate donate
  1. ioBroker Community Home
  2. Deutsch
  3. Skripten / Logik
  4. JavaScript
  5. Json für SourceAnalytics

NEWS

  • Neuer ioBroker-Blog online: Monatsrückblick März/April 2026
    BluefoxB
    Bluefox
    8
    1
    472

  • Verwendung von KI bitte immer deutlich kennzeichnen
    HomoranH
    Homoran
    10
    1
    392

  • Monatsrückblick Januar/Februar 2026 ist online!
    BluefoxB
    Bluefox
    18
    1
    978

Json für SourceAnalytics

Geplant Angeheftet Gesperrt Verschoben JavaScript
83 Beiträge 7 Kommentatoren 10.7k Aufrufe 9 Beobachtet
  • Älteste zuerst
  • Neuste zuerst
  • Meiste Stimmen
Antworten
  • In einem neuen Thema antworten
Anmelden zum Antworten
Dieses Thema wurde gelöscht. Nur Nutzer mit entsprechenden Rechten können es sehen.
  • crunchipC crunchip

    @hm_krause probier es mal mit diesem

    let myArrSourcA = [];
    let myBlacklist = [];
    let dataPunkt = '0_userdata.0.StromJson.StromJson';
    let theSchedule = ' * * * * *';
    
    function getValFixed(id, digits = 2) {
        if (!existsState(id)) return (0).toFixed(digits);
        const s = getState(id);
        if (s.val === null || s.val === undefined) return (0).toFixed(digits);
        const n = Number(s.val);
        return isNaN(n) ? (0).toFixed(digits) : n.toFixed(digits);
    }
    
    function getValRaw(id) {
        if (!existsState(id)) return 0;
        const s = getState(id);
        return (s.val === null || s.val === undefined) ? 0 : s.val;
    }
    
    async function addEntry(id4, direction, costPrefix, unitCheck = null) {
        const idy = id4.split('.');
        const rootId = `${idy[0]}.${idy[1]}.${idy[2]}`;
    
        if (myBlacklist.includes(idy[2])) return;
    
        // State-Objekt prüfen
        if (!existsObject(id4)) return;
        const stateObj = getObject(id4);
        if (!stateObj.common || (unitCheck && stateObj.common.unit !== unitCheck)) {
            return;
        }
    
        // Root-Objekt prüfen
        if (!await existsObjectAsync(rootId)) {
            log(`Root-Objekt fehlt: ${rootId}`, 'warn');
            return;
        }
        const rootObj = getObject(rootId);
        if (!rootObj.common) {
            log(`Root-Objekt ohne common: ${rootId}`, 'warn');
            return;
        }
    
        const theName = rootObj.common.name || 'no Name';
    
        const replaceBase = `${direction}.01_currentDay`;
        myArrSourcA.push({
            "Name": theName,
            "Gestern": getValFixed(id4.replace("01_currentDay", "01_previousDay")),
            "Heute": getValRaw(id4),
            "Monat": getValFixed(id4.replace("01_currentDay", "03_currentMonth")),
            "Jahr": getValFixed(id4.replace("01_currentDay", "05_currentYear")),
            "Gestern Kosten": getValFixed(id4.replace(replaceBase, `${costPrefix}.01_previousDay`)),
            "Heute Kosten": getValFixed(id4.replace(replaceBase, `${costPrefix}.01_currentDay`)),
            "Monats Kosten": getValFixed(id4.replace(replaceBase, `${costPrefix}.03_currentMonth`)),
            "Jahres Kosten": getValFixed(id4.replace(replaceBase, `${costPrefix}.05_currentYear`)),
            "Richtung": `<span style='color:${direction === "delivered" ? "green" : "red"}'>${direction}</span>`
        });
    }
    
    async function calcJson() {
        myArrSourcA = [];
    
        await $('sourceanalytix.0.*.currentYear.delivered.01_currentDay').eachAsync(async (id4) => {
            await addEntry(id4, 'delivered', 'earnings');
        });
    
        await $('sourceanalytix.0.*.currentYear.consumed.01_currentDay').eachAsync(async (id4) => {
            await addEntry(id4, 'consumed', 'costs', 'kWh');
        });
    
        return myArrSourcA;
    }
    
    async function writeDatapoint() {
        if (!(await existsStateAsync(dataPunkt))) {
            await createStateAsync(dataPunkt, "", {
                type: "string",
                name: "JSON SourcAnalytics",
                role: "value",
                read: true,
                write: true
            });
        }
    
        setState(dataPunkt, JSON.stringify(await calcJson()), true);
    }
    
    writeDatapoint();
    schedule(theSchedule, writeDatapoint);
    
    
    H Offline
    H Offline
    hm_krause
    schrieb am zuletzt editiert von
    #81

    @crunchip
    ...bringt noch:

    javascript.0	18:14:17.814	error	
    
    Error: TypeError: $(...).eachAsync is not a function
    
    javascript.0	18:14:17.814	error	
    
        at calcJson (script.js.Energie.Skript_1:65:71)
    
    javascript.0	18:14:17.814	error	
    
        at writeDatapoint (script.js.Energie.Skript_1:87:46)
    
    crunchipC 1 Antwort Letzte Antwort
    0
    • H hm_krause

      @crunchip
      ...bringt noch:

      javascript.0	18:14:17.814	error	
      
      Error: TypeError: $(...).eachAsync is not a function
      
      javascript.0	18:14:17.814	error	
      
          at calcJson (script.js.Energie.Skript_1:65:71)
      
      javascript.0	18:14:17.814	error	
      
          at writeDatapoint (script.js.Energie.Skript_1:87:46)
      
      crunchipC Abwesend
      crunchipC Abwesend
      crunchip
      Forum Testing Most Active Developer
      schrieb am zuletzt editiert von crunchip
      #82

      @hm_krause sagte in Json für SourceAnalytics:

      bringt noch:

      let myArrSourcA = [];
      let myBlacklist = [];
      let dataPunkt = '0_userdata.0.StromJson.StromJson';
      let theSchedule = ' * * * * *';
      
      function getValFixed(id, digits = 2) {
          if (!existsState(id)) return '0.00';
          const s = getState(id);
          if (s.val === null || s.val === undefined) return '0.00';
          const n = Number(s.val);
          return isNaN(n) ? '0.00' : n.toFixed(digits);
      }
      
      function getValRaw(id) {
          if (!existsState(id)) return 0;
          const s = getState(id);
          return (s.val === null || s.val === undefined) ? 0 : Number(s.val);
      }
      
      function addEntry(id4, direction, costPrefix, unitCheck = null) {
          const idy = id4.split('.');
          const rootId = `${idy[0]}.${idy[1]}.${idy[2]}`;
      
          if (myBlacklist.includes(idy[2])) return;
      
          // Alles sync, keine async mehr nötig
          if (!existsObject(id4)) return;
          const stateObj = getObject(id4);
          if (!stateObj?.common || (unitCheck && stateObj.common.unit !== unitCheck)) return;
      
          if (!existsObject(rootId)) {
              log(`Root fehlt: ${rootId}`, 'warn');
              return;
          }
          const rootObj = getObject(rootId);
          if (!rootObj?.common) {
              log(`Root ohne common: ${rootId}`, 'warn');
              return;
          }
      
          const theName = rootObj.common.name || 'no Name';
          const replaceBase = `${direction}.01_currentDay`;
      
          myArrSourcA.push({
              "Name": theName,
              "Gestern": getValFixed(id4.replace("01_currentDay", "01_previousDay")),
              "Heute": getValRaw(id4),
              "Monat": getValFixed(id4.replace("01_currentDay", "03_currentMonth")),
              "Jahr": getValFixed(id4.replace("01_currentDay", "05_currentYear")),
              "Gestern Kosten": getValFixed(id4.replace(replaceBase, `${costPrefix}.01_previousDay`)),
              "Heute Kosten": getValFixed(id4.replace(replaceBase, `${costPrefix}.01_currentDay`)),
              "Monats Kosten": getValFixed(id4.replace(replaceBase, `${costPrefix}.03_currentMonth`)),
              "Jahres Kosten": getValFixed(id4.replace(replaceBase, `${costPrefix}.05_currentYear`)),
              "Richtung": `<span style='color:${direction === "delivered" ? "green" : "red"}'>${direction}</span>`
          });
      }
      
      async function calcJson() {
          myArrSourcA = [];
      
          // Normale .each() - sync und blitzschnell!
          $('sourceanalytix.0.*.currentYear.delivered.01_currentDay').each(function (id4) {
              addEntry(id4, 'delivered', 'earnings');
          });
      
          $('sourceanalytix.0.*.currentYear.consumed.01_currentDay').each(function (id4) {
              addEntry(id4, 'consumed', 'costs', 'kWh');
          });
      
          return myArrSourcA;
      }
      
      async function writeDatapoint() {
          if (!(await existsStateAsync(dataPunkt))) {
              await createStateAsync(dataPunkt, "", {
                  type: "string", name: "JSON SourcAnalytics", role: "value", read: true, write: true
              });
          }
          setState(dataPunkt, JSON.stringify(await calcJson()), true);
      }
      
      writeDatapoint();
      schedule(theSchedule, writeDatapoint);
      
      

      umgestiegen von Proxmox auf Unraid

      H 1 Antwort Letzte Antwort
      1
      • crunchipC crunchip

        @hm_krause sagte in Json für SourceAnalytics:

        bringt noch:

        let myArrSourcA = [];
        let myBlacklist = [];
        let dataPunkt = '0_userdata.0.StromJson.StromJson';
        let theSchedule = ' * * * * *';
        
        function getValFixed(id, digits = 2) {
            if (!existsState(id)) return '0.00';
            const s = getState(id);
            if (s.val === null || s.val === undefined) return '0.00';
            const n = Number(s.val);
            return isNaN(n) ? '0.00' : n.toFixed(digits);
        }
        
        function getValRaw(id) {
            if (!existsState(id)) return 0;
            const s = getState(id);
            return (s.val === null || s.val === undefined) ? 0 : Number(s.val);
        }
        
        function addEntry(id4, direction, costPrefix, unitCheck = null) {
            const idy = id4.split('.');
            const rootId = `${idy[0]}.${idy[1]}.${idy[2]}`;
        
            if (myBlacklist.includes(idy[2])) return;
        
            // Alles sync, keine async mehr nötig
            if (!existsObject(id4)) return;
            const stateObj = getObject(id4);
            if (!stateObj?.common || (unitCheck && stateObj.common.unit !== unitCheck)) return;
        
            if (!existsObject(rootId)) {
                log(`Root fehlt: ${rootId}`, 'warn');
                return;
            }
            const rootObj = getObject(rootId);
            if (!rootObj?.common) {
                log(`Root ohne common: ${rootId}`, 'warn');
                return;
            }
        
            const theName = rootObj.common.name || 'no Name';
            const replaceBase = `${direction}.01_currentDay`;
        
            myArrSourcA.push({
                "Name": theName,
                "Gestern": getValFixed(id4.replace("01_currentDay", "01_previousDay")),
                "Heute": getValRaw(id4),
                "Monat": getValFixed(id4.replace("01_currentDay", "03_currentMonth")),
                "Jahr": getValFixed(id4.replace("01_currentDay", "05_currentYear")),
                "Gestern Kosten": getValFixed(id4.replace(replaceBase, `${costPrefix}.01_previousDay`)),
                "Heute Kosten": getValFixed(id4.replace(replaceBase, `${costPrefix}.01_currentDay`)),
                "Monats Kosten": getValFixed(id4.replace(replaceBase, `${costPrefix}.03_currentMonth`)),
                "Jahres Kosten": getValFixed(id4.replace(replaceBase, `${costPrefix}.05_currentYear`)),
                "Richtung": `<span style='color:${direction === "delivered" ? "green" : "red"}'>${direction}</span>`
            });
        }
        
        async function calcJson() {
            myArrSourcA = [];
        
            // Normale .each() - sync und blitzschnell!
            $('sourceanalytix.0.*.currentYear.delivered.01_currentDay').each(function (id4) {
                addEntry(id4, 'delivered', 'earnings');
            });
        
            $('sourceanalytix.0.*.currentYear.consumed.01_currentDay').each(function (id4) {
                addEntry(id4, 'consumed', 'costs', 'kWh');
            });
        
            return myArrSourcA;
        }
        
        async function writeDatapoint() {
            if (!(await existsStateAsync(dataPunkt))) {
                await createStateAsync(dataPunkt, "", {
                    type: "string", name: "JSON SourcAnalytics", role: "value", read: true, write: true
                });
            }
            setState(dataPunkt, JSON.stringify(await calcJson()), true);
        }
        
        writeDatapoint();
        schedule(theSchedule, writeDatapoint);
        
        
        H Offline
        H Offline
        hm_krause
        schrieb am zuletzt editiert von
        #83

        @crunchip
        Passt,
        vielen Dank...

        1 Antwort Letzte Antwort
        0

        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
        Antworten
        • In einem neuen Thema antworten
        Anmelden zum Antworten
        • Älteste zuerst
        • Neuste zuerst
        • Meiste Stimmen


        Support us

        ioBroker
        Community Adapters
        Donate

        578

        Online

        32.8k

        Benutzer

        82.8k

        Themen

        1.3m

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

        • Du hast noch kein Konto? Registrieren

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