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

  • Default (No Skin)
  • No Skin
Collapse
ioBroker Logo

Community Forum

donate donate
  1. ioBroker Community Home
  2. Deutsch
  3. Skripten / Logik
  4. JavaScript
  5. Datenpunkt überschreiben

NEWS

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

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

  • Weihnachtsangebot 2025! 🎄
    BluefoxB
    Bluefox
    25
    1
    2.3k

Datenpunkt überschreiben

Scheduled Pinned Locked Moved JavaScript
2 Posts 2 Posters 209 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    woostel
    wrote on last edited by
    #1

    hallo, also ich glaube, das ich dieses async und await oder CreateState nicht verstanden habe, oder anderswo ist der Wurm drin:-)

    Für ein ECharts - Balkendiagramm möchte ich Daten einer InfluxBD abfragen und als Json String bereitstellen.

    Ich hatte mit vorgestellt, das der Datenpunkt (zeile 16) bei jedem Durchlauf Init() neu angelegt wird, also leer ist.
    Das ist leider nicht so denn Daten werden an die bestehende angehangen.

    let flux =  
        'import "date"' +
        ' import "timezone"' +
        ' option location = timezone.location(name: "Europe/Berlin")'+
        ' from(bucket: "iobroker")' +
        ' |> range(start: -7d)' +
        ' |> filter(fn: (r) => r["_measurement"] == "zaehlerstand.strom")' +
        ' |> filter(fn: (r) => r["_field"] == "value")'+
        ' |> difference(nonNegative: true, columns: ["_value"])'+
        ' |> aggregateWindow(every: 1d, fn: sum, createEmpty: true,timeSrc: "_start")'+
        ' |> map( fn: (r) => ({r with fluxts: (uint(v: r._time) / uint(v: 1000 * 1000 * 1000 ))}))'+
        ' |> fill(column: "_value", value: 0.0)';
        //' |> cumulativeSum(columns: ["_value"])';
        //' |> keep(columns: ["_time", "_value","fluxts"])';
    
    let idJson = '0_userdata.0.test.json';
    let obj = JSON.parse(getState(idJson).val);
    
    // Datenpunkte anlegen ... 
    async function init() {
        createState(idJson, '[]',true, { type: 'string', name: 'Verbrauchswerte' });
    };
    
    // Daten holen ... 
    async function getData () {
         sendTo('influxdb.1', 'query', flux, function (result) {
            if (result.error) {
                console.error(result.error);
            } else  {
            // show result
                obj.splice(0,0);
                for (var i = 0; i < result.result[0].length; i++) {
                    var resMonat = result.result[0][i].fluxts; 
                    var resWert = result.result[0][i]._value;
                    obj.push({ ts: resMonat, val: resWert });
                }
                console.log(obj);
                setState(idJson, JSON.stringify(obj),true);
            }
        });
    };
    
    await init(); // einmal ausführen
    
    schedule('{"time":{"start":"00:00","end":"24:00","mode":"minutes","interval":1},"period":{"days":1}}', async function () {
        await init();
        await getData();
    });
    
    O 1 Reply Last reply
    0
    • W woostel

      hallo, also ich glaube, das ich dieses async und await oder CreateState nicht verstanden habe, oder anderswo ist der Wurm drin:-)

      Für ein ECharts - Balkendiagramm möchte ich Daten einer InfluxBD abfragen und als Json String bereitstellen.

      Ich hatte mit vorgestellt, das der Datenpunkt (zeile 16) bei jedem Durchlauf Init() neu angelegt wird, also leer ist.
      Das ist leider nicht so denn Daten werden an die bestehende angehangen.

      let flux =  
          'import "date"' +
          ' import "timezone"' +
          ' option location = timezone.location(name: "Europe/Berlin")'+
          ' from(bucket: "iobroker")' +
          ' |> range(start: -7d)' +
          ' |> filter(fn: (r) => r["_measurement"] == "zaehlerstand.strom")' +
          ' |> filter(fn: (r) => r["_field"] == "value")'+
          ' |> difference(nonNegative: true, columns: ["_value"])'+
          ' |> aggregateWindow(every: 1d, fn: sum, createEmpty: true,timeSrc: "_start")'+
          ' |> map( fn: (r) => ({r with fluxts: (uint(v: r._time) / uint(v: 1000 * 1000 * 1000 ))}))'+
          ' |> fill(column: "_value", value: 0.0)';
          //' |> cumulativeSum(columns: ["_value"])';
          //' |> keep(columns: ["_time", "_value","fluxts"])';
      
      let idJson = '0_userdata.0.test.json';
      let obj = JSON.parse(getState(idJson).val);
      
      // Datenpunkte anlegen ... 
      async function init() {
          createState(idJson, '[]',true, { type: 'string', name: 'Verbrauchswerte' });
      };
      
      // Daten holen ... 
      async function getData () {
           sendTo('influxdb.1', 'query', flux, function (result) {
              if (result.error) {
                  console.error(result.error);
              } else  {
              // show result
                  obj.splice(0,0);
                  for (var i = 0; i < result.result[0].length; i++) {
                      var resMonat = result.result[0][i].fluxts; 
                      var resWert = result.result[0][i]._value;
                      obj.push({ ts: resMonat, val: resWert });
                  }
                  console.log(obj);
                  setState(idJson, JSON.stringify(obj),true);
              }
          });
      };
      
      await init(); // einmal ausführen
      
      schedule('{"time":{"start":"00:00","end":"24:00","mode":"minutes","interval":1},"period":{"days":1}}', async function () {
          await init();
          await getData();
      });
      
      O Offline
      O Offline
      Oli
      wrote on last edited by
      #2

      @woostel
      ich bin jetzt nicht der große Experte was programmieren angeht, aber wie ich das sehe, holst du dir in der Zeile 17 das mit Daten befüllte JSON in eine Variable und pusht danach in deiner "getData Funktion" drauf.

      Gruß
      Oliver

      1 Reply Last reply
      1
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      Support us

      ioBroker
      Community Adapters
      Donate

      764

      Online

      32.6k

      Users

      82.2k

      Topics

      1.3m

      Posts
      Community
      Impressum | Datenschutz-Bestimmungen | Nutzungsbedingungen | Einwilligungseinstellungen
      ioBroker Community 2014-2025
      logo
      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Home
      • Recent
      • Tags
      • Unread 0
      • Categories
      • Unreplied
      • Popular
      • GitHub
      • Docu
      • Hilfe