Skip to content
  • Home
  • Aktuell
  • Tags
  • 0 Ungelesen 0
  • Kategorien
  • Unreplied
  • Beliebt
  • 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

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

Community Forum

donate donate
  1. ioBroker Community Home
  2. English
  3. Scripting / Logic
  4. JavaScript
  5. [Question] restart causes empty values are written

NEWS

  • Weihnachtsangebot 2025! 🎄
    BluefoxB
    Bluefox
    22
    1
    1.1k

  • UPDATE 31.10.: Amazon Alexa - ioBroker Skill läuft aus ?
    apollon77A
    apollon77
    48
    3
    9.2k

  • Monatsrückblick – September 2025
    BluefoxB
    Bluefox
    14
    1
    2.4k

[Question] restart causes empty values are written

Geplant Angeheftet Gesperrt Verschoben JavaScript
javascript
14 Beiträge 4 Kommentatoren 1.2k Aufrufe 4 Watching
  • Ä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.
  • E Offline
    E Offline
    europe
    schrieb am zuletzt editiert von
    #1

    Following script produces empty entries when either the controller or system is rebooted, or when the script is restarted. How do I have to modify it to avoid that?

    // min (0 - 59)
    // hour (0 - 23)
    // day of month (1 - 31)
    // month (1 - 12)
    // day of week (0 - 6) (0 to 6 are Sunday to Saturday, or use names; 7 is Sunday, the same as 0)
    var cronH           = "59 * * * *";
    var cronD           = "59 23 * * *";
    var cronW           = "59 23 * * 0";
    var cronM           = "59 23 L * *";
    var cronY			= "59 23 31 12 *";
    var idH       = "0_userdata.0.Haus.Bezug.tmp.Total-h";
    var idD       = "0_userdata.0.Haus.Bezug.tmp.Total-d";
    var idW       = "0_userdata.0.Haus.Bezug.tmp.Total-w";
    var idM       = "0_userdata.0.Haus.Bezug.tmp.Total-m";
    var idY       = "0_userdata.0.Haus.Bezug.tmp.Total-y";
    var idValue   = "smartmeter.0.1-0:1_8_0__255.value"; // Bezug kWh
    var id_H      = "0_userdata.0.Haus.Bezug.Hour";
    var id_D      = "0_userdata.0.Haus.Bezug.Day";
    var id_W      = "0_userdata.0.Haus.Bezug.Week";
    var id_M      = "0_userdata.0.Haus.Bezug.Month";
    var id_Y      = "0_userdata.0.Haus.Bezug.Year";
    //var debug           = false;
    var DPArray         = [idH, idD , idW, idM, idY, id_H, id_D, id_W, id_M, id_Y];
    var DPUnit          = "kWh";
    DPArray.forEach(function(wert, index, array) {
        var DPType = wert.split(".");
        var DPDescr = "Power consumption of " + (DPType[DPType.length - 1]);
     
        if(index > 3) DPUnit = "kWh";
        createState(wert, 0, {
            name: DPDescr,
            desc: DPDescr,
            type: 'number',
            unit: DPUnit,
            role: 'value'
        });
    });
    function haupt (pre, post) {
        var nPre = getState(pre).val;
        var nCur = getState(idValue).val;
        var nDiff = (Math.round((nCur * 100)) - Math.round((nPre * 100)))/100; // kWh
        setState(post, nDiff, true);
        //if(debug) log("Aus: " + nCur +" - "+ nPre + " = " + nDiff);
        var shandler = on ({id: post, change: 'any'}, function(data) {
            setState(pre, Math.round(nCur*100)/100, true);
            unsubscribe(shandler); 
        });
    }
    // schedule
    schedule(cronH, function () {
        haupt(idH, id_H);
    });
    schedule(cronD, function () {
        haupt(idD, id_D);
    });
    schedule(cronW, function () {
        haupt(idW, id_W);
    });
    schedule(cronM, function () {
        haupt(idM, id_M);
    });
    schedule(cronY, function () {
        haupt(idY, id_Y);
    });
    
    BananaJoeB 1 Antwort Letzte Antwort
    0
    • E europe

      Following script produces empty entries when either the controller or system is rebooted, or when the script is restarted. How do I have to modify it to avoid that?

      // min (0 - 59)
      // hour (0 - 23)
      // day of month (1 - 31)
      // month (1 - 12)
      // day of week (0 - 6) (0 to 6 are Sunday to Saturday, or use names; 7 is Sunday, the same as 0)
      var cronH           = "59 * * * *";
      var cronD           = "59 23 * * *";
      var cronW           = "59 23 * * 0";
      var cronM           = "59 23 L * *";
      var cronY			= "59 23 31 12 *";
      var idH       = "0_userdata.0.Haus.Bezug.tmp.Total-h";
      var idD       = "0_userdata.0.Haus.Bezug.tmp.Total-d";
      var idW       = "0_userdata.0.Haus.Bezug.tmp.Total-w";
      var idM       = "0_userdata.0.Haus.Bezug.tmp.Total-m";
      var idY       = "0_userdata.0.Haus.Bezug.tmp.Total-y";
      var idValue   = "smartmeter.0.1-0:1_8_0__255.value"; // Bezug kWh
      var id_H      = "0_userdata.0.Haus.Bezug.Hour";
      var id_D      = "0_userdata.0.Haus.Bezug.Day";
      var id_W      = "0_userdata.0.Haus.Bezug.Week";
      var id_M      = "0_userdata.0.Haus.Bezug.Month";
      var id_Y      = "0_userdata.0.Haus.Bezug.Year";
      //var debug           = false;
      var DPArray         = [idH, idD , idW, idM, idY, id_H, id_D, id_W, id_M, id_Y];
      var DPUnit          = "kWh";
      DPArray.forEach(function(wert, index, array) {
          var DPType = wert.split(".");
          var DPDescr = "Power consumption of " + (DPType[DPType.length - 1]);
       
          if(index > 3) DPUnit = "kWh";
          createState(wert, 0, {
              name: DPDescr,
              desc: DPDescr,
              type: 'number',
              unit: DPUnit,
              role: 'value'
          });
      });
      function haupt (pre, post) {
          var nPre = getState(pre).val;
          var nCur = getState(idValue).val;
          var nDiff = (Math.round((nCur * 100)) - Math.round((nPre * 100)))/100; // kWh
          setState(post, nDiff, true);
          //if(debug) log("Aus: " + nCur +" - "+ nPre + " = " + nDiff);
          var shandler = on ({id: post, change: 'any'}, function(data) {
              setState(pre, Math.round(nCur*100)/100, true);
              unsubscribe(shandler); 
          });
      }
      // schedule
      schedule(cronH, function () {
          haupt(idH, id_H);
      });
      schedule(cronD, function () {
          haupt(idD, id_D);
      });
      schedule(cronW, function () {
          haupt(idW, id_W);
      });
      schedule(cronM, function () {
          haupt(idM, id_M);
      });
      schedule(cronY, function () {
          haupt(idY, id_Y);
      });
      
      BananaJoeB Offline
      BananaJoeB Offline
      BananaJoe
      Most Active
      schrieb am zuletzt editiert von BananaJoe
      #2

      @europe da du sonst auch auf deutsch geschrieben hast:
      Definiere mal "empty entries". Am Anfang legt dein Skript die Datenpunkte an, da sollte aber eigentlich nichts passieren wenn es diese schon gibt.
      Sonst kommentiere den Block mal aus, Zeile 25 bis 36 und teste ob das Problem dann weg ist

      Define “empty entries”. At the beginning your script creates the data points, but nothing should actually happen if they already exist.
      Otherwise comment out the block, lines 25 to 36 and test whether the problem goes away

      ioBroker@Ubuntu 24.04 LTS (VMware) für: >260 Geräte, 5 Switche, 7 AP, 9 IP-Cam, 1 NAS 42TB, 1 ESXi 15TB, 4 Proxmox 1TB, 1 Hyper-V 48TB, 14 x Echo, 5x FireTV, 5 x Tablett/Handy VIS || >=160 Tasmota/Shelly || >=95 ZigBee || PV 8.1kW / Akku 14kWh || 2x USV 750W kaskadiert || Creality CR-10 SE 3D-Drucker

      mcm1957M E 2 Antworten Letzte Antwort
      0
      • BananaJoeB BananaJoe

        @europe da du sonst auch auf deutsch geschrieben hast:
        Definiere mal "empty entries". Am Anfang legt dein Skript die Datenpunkte an, da sollte aber eigentlich nichts passieren wenn es diese schon gibt.
        Sonst kommentiere den Block mal aus, Zeile 25 bis 36 und teste ob das Problem dann weg ist

        Define “empty entries”. At the beginning your script creates the data points, but nothing should actually happen if they already exist.
        Otherwise comment out the block, lines 25 to 36 and test whether the problem goes away

        mcm1957M Online
        mcm1957M Online
        mcm1957
        schrieb am zuletzt editiert von mcm1957
        #3

        @bananajoe said in [Question] restart causes empty values are written:

        @europe da du sonst auch auf deutsch geschrieben hast:

        This is the english forum section.

        So PLEASE use english languge here, especially if TE writes in english - so this topic is put intentionally into the english section. For german langugage postiungs theres the german area.

        Thanks

        P.S. Even if @europe can read/write in german, others might not be capable to do so, Thats why we have an english section

        Entwicklung u Betreuung: envertech-pv, hoymiles-ms, ns-client, pid, snmp Adapter;
        Support Repositoryverwaltung.

        Wer Danke sagen will, kann nen Kaffee spendieren: https://paypal.me/mcm1957atiobroker

        LESEN - gute Forenbeitrage

        1 Antwort Letzte Antwort
        0
        • BananaJoeB Offline
          BananaJoeB Offline
          BananaJoe
          Most Active
          schrieb am zuletzt editiert von
          #4

          i edit my post

          ioBroker@Ubuntu 24.04 LTS (VMware) für: >260 Geräte, 5 Switche, 7 AP, 9 IP-Cam, 1 NAS 42TB, 1 ESXi 15TB, 4 Proxmox 1TB, 1 Hyper-V 48TB, 14 x Echo, 5x FireTV, 5 x Tablett/Handy VIS || >=160 Tasmota/Shelly || >=95 ZigBee || PV 8.1kW / Akku 14kWh || 2x USV 750W kaskadiert || Creality CR-10 SE 3D-Drucker

          1 Antwort Letzte Antwort
          1
          • BananaJoeB BananaJoe

            @europe da du sonst auch auf deutsch geschrieben hast:
            Definiere mal "empty entries". Am Anfang legt dein Skript die Datenpunkte an, da sollte aber eigentlich nichts passieren wenn es diese schon gibt.
            Sonst kommentiere den Block mal aus, Zeile 25 bis 36 und teste ob das Problem dann weg ist

            Define “empty entries”. At the beginning your script creates the data points, but nothing should actually happen if they already exist.
            Otherwise comment out the block, lines 25 to 36 and test whether the problem goes away

            E Offline
            E Offline
            europe
            schrieb am zuletzt editiert von europe
            #5

            @bananajoe Thanks... with empty entries I meant null values
            273aa300-d54a-454a-bfd1-ab0c28300d3c-image.png

            Once without Ack once with.

            I just want to understand it before commenting that part out...

            1 Antwort Letzte Antwort
            0
            • BananaJoeB Offline
              BananaJoeB Offline
              BananaJoe
              Most Active
              schrieb am zuletzt editiert von
              #6

              You have a function haupt.
              Your function will started via scheduled timers and different parameter - ok.
              But in your function Haupt you also have an on Trigger-reaction in line 44
              So everytime (!) your function is called, a new(!) trigger instance is added. for cronH 24 on a day. If the value change, all 24 instances of the trigger will start. so the script will cause a slow death of the system ...

              so move the on part to a separete section an be sure it will be called only once.

              i did not now if this is the problem, but it is a problem

              ioBroker@Ubuntu 24.04 LTS (VMware) für: >260 Geräte, 5 Switche, 7 AP, 9 IP-Cam, 1 NAS 42TB, 1 ESXi 15TB, 4 Proxmox 1TB, 1 Hyper-V 48TB, 14 x Echo, 5x FireTV, 5 x Tablett/Handy VIS || >=160 Tasmota/Shelly || >=95 ZigBee || PV 8.1kW / Akku 14kWh || 2x USV 750W kaskadiert || Creality CR-10 SE 3D-Drucker

              1 Antwort Letzte Antwort
              0
              • E Offline
                E Offline
                europe
                schrieb am zuletzt editiert von
                #7

                @bananajoe ok, I'm completely stuck here now. I have like 10 of these crons running and the system is up and running sine more than 100 days on a simple TV box. So it might be a problem, but it didn't kill me yet...

                I just start the script once. If I modify it, I stop it and then start it again. You say while its running is it basically forking all the time?!

                Do you have a link to some basics that I can educate myself on this matter?

                "move the on part to a separate section", I have not the faintest clue how to implement that. Is there maybe a general function which implements what I want? A cron.tab like behaviour?

                BananaJoeB AsgothianA 2 Antworten Letzte Antwort
                0
                • E europe

                  @bananajoe ok, I'm completely stuck here now. I have like 10 of these crons running and the system is up and running sine more than 100 days on a simple TV box. So it might be a problem, but it didn't kill me yet...

                  I just start the script once. If I modify it, I stop it and then start it again. You say while its running is it basically forking all the time?!

                  Do you have a link to some basics that I can educate myself on this matter?

                  "move the on part to a separate section", I have not the faintest clue how to implement that. Is there maybe a general function which implements what I want? A cron.tab like behaviour?

                  BananaJoeB Offline
                  BananaJoeB Offline
                  BananaJoe
                  Most Active
                  schrieb am zuletzt editiert von
                  #8

                  @europe sagte in [Question] restart causes empty values are written:

                  I just start the script once. If I modify it, I stop it and then start it again. You say while its running is it basically forking all the time?!

                  if you stop the script, all "childs / sub-instances" are also stopped.

                  separte section: remove the on expression from the function and create this for every datapoint separate.

                  i am sorry, but i know no learning sites for this in english.

                  ioBroker@Ubuntu 24.04 LTS (VMware) für: >260 Geräte, 5 Switche, 7 AP, 9 IP-Cam, 1 NAS 42TB, 1 ESXi 15TB, 4 Proxmox 1TB, 1 Hyper-V 48TB, 14 x Echo, 5x FireTV, 5 x Tablett/Handy VIS || >=160 Tasmota/Shelly || >=95 ZigBee || PV 8.1kW / Akku 14kWh || 2x USV 750W kaskadiert || Creality CR-10 SE 3D-Drucker

                  1 Antwort Letzte Antwort
                  0
                  • E Offline
                    E Offline
                    europe
                    schrieb am zuletzt editiert von
                    #9

                    @bananajoe okay, thanks, a link to a german one is fine as well :-)
                    Do you know a better script which implements this kind of cron tab like behaviour?

                    BananaJoeB 1 Antwort Letzte Antwort
                    0
                    • E europe

                      @bananajoe okay, thanks, a link to a german one is fine as well :-)
                      Do you know a better script which implements this kind of cron tab like behaviour?

                      BananaJoeB Offline
                      BananaJoeB Offline
                      BananaJoe
                      Most Active
                      schrieb am zuletzt editiert von
                      #10

                      @europe you could just use the "SourceAnalytix" Adapter. This Adapter will create all data you want with your script i think

                      I learned JavaScript / JavaScript with ioBroker/nodeJS and Blocky by 1.001 Websites and Examples from this forum.
                      I am sorry, but do not have "this one" site for learning this.
                      There are many videos on youtube for different themes (trigger, crontabs)

                      You don`t do it the wrong way. you wrote a script, you have problems and ask here. same as i do at the beginning.

                      ioBroker@Ubuntu 24.04 LTS (VMware) für: >260 Geräte, 5 Switche, 7 AP, 9 IP-Cam, 1 NAS 42TB, 1 ESXi 15TB, 4 Proxmox 1TB, 1 Hyper-V 48TB, 14 x Echo, 5x FireTV, 5 x Tablett/Handy VIS || >=160 Tasmota/Shelly || >=95 ZigBee || PV 8.1kW / Akku 14kWh || 2x USV 750W kaskadiert || Creality CR-10 SE 3D-Drucker

                      1 Antwort Letzte Antwort
                      0
                      • E europe

                        @bananajoe ok, I'm completely stuck here now. I have like 10 of these crons running and the system is up and running sine more than 100 days on a simple TV box. So it might be a problem, but it didn't kill me yet...

                        I just start the script once. If I modify it, I stop it and then start it again. You say while its running is it basically forking all the time?!

                        Do you have a link to some basics that I can educate myself on this matter?

                        "move the on part to a separate section", I have not the faintest clue how to implement that. Is there maybe a general function which implements what I want? A cron.tab like behaviour?

                        AsgothianA Offline
                        AsgothianA Offline
                        Asgothian
                        Developer
                        schrieb am zuletzt editiert von
                        #11

                        @europe sagte in [Question] restart causes empty values are written:

                        @bananajoe ok, I'm completely stuck here now. I have like 10 of these crons running and the system is up and running sine more than 100 days on a simple TV box. So it might be a problem, but it didn't kill me yet...

                        I just start the script once. If I modify it, I stop it and then start it again. You say while its running is it basically forking all the time?!

                        Do you have a link to some basics that I can educate myself on this matter?

                        "move the on part to a separate section", I have not the faintest clue how to implement that. Is there maybe a general function which implements what I want? A cron.tab like behaviour?

                        Actually, in your case the ’on’ construct is ok - due to the fast that once triggar is activated you unsubscribe from the triggar (line 46 in your code)
                        This does not get us closer to the null values, but explains why all is running fine.

                        ioBroker auf RPi4 - Hardware soweit wie möglich via Zigbee.
                        "Shit don't work" ist keine Fehlermeldung, sondern ein Fluch.

                        1 Antwort Letzte Antwort
                        0
                        • BananaJoeB Offline
                          BananaJoeB Offline
                          BananaJoe
                          Most Active
                          schrieb am zuletzt editiert von BananaJoe
                          #12

                          @Asgothian

                          unsubscribe

                          damned, i missed the unsubscribe, so after the on is created it will immediately destroyed?

                          ioBroker@Ubuntu 24.04 LTS (VMware) für: >260 Geräte, 5 Switche, 7 AP, 9 IP-Cam, 1 NAS 42TB, 1 ESXi 15TB, 4 Proxmox 1TB, 1 Hyper-V 48TB, 14 x Echo, 5x FireTV, 5 x Tablett/Handy VIS || >=160 Tasmota/Shelly || >=95 ZigBee || PV 8.1kW / Akku 14kWh || 2x USV 750W kaskadiert || Creality CR-10 SE 3D-Drucker

                          AsgothianA 1 Antwort Letzte Antwort
                          0
                          • BananaJoeB BananaJoe

                            @Asgothian

                            unsubscribe

                            damned, i missed the unsubscribe, so after the on is created it will immediately destroyed?

                            AsgothianA Offline
                            AsgothianA Offline
                            Asgothian
                            Developer
                            schrieb am zuletzt editiert von
                            #13

                            @bananajoe sagte in [Question] restart causes empty values are written:

                            @Asgothian

                            unsubscribe

                            damned, i missed the unsubscribe, so after the on is created it will immediately destroyed?

                            The first time the trigger fires, yes.

                            ioBroker auf RPi4 - Hardware soweit wie möglich via Zigbee.
                            "Shit don't work" ist keine Fehlermeldung, sondern ein Fluch.

                            E 1 Antwort Letzte Antwort
                            0
                            • AsgothianA Asgothian

                              @bananajoe sagte in [Question] restart causes empty values are written:

                              @Asgothian

                              unsubscribe

                              damned, i missed the unsubscribe, so after the on is created it will immediately destroyed?

                              The first time the trigger fires, yes.

                              E Offline
                              E Offline
                              europe
                              schrieb am zuletzt editiert von
                              #14

                              @asgothian

                              Okay, I just found the history adapter writes null at start and stop. I deactivated that.
                              I still had all those null entries though, which messed up with certain plotting routines.

                              I also wrote a script which goes through all objects (incl. 3 level of nesting according to the doc) and removes items in a certain timeframe of 1 minute.

                              Here's the code:

                              // 21-10-2024. Works perfectly!!
                              // To see log entries, set log level of javascript instance to silly
                              var startTimestamp = new Date('2023-12-14T09:40:00.000');
                              var endTimestamp = new Date(startTimestamp.getTime() + 1*60000);
                              
                              //log(startTimestamp.toISOString())
                              //log(endTimestamp.toISOString())
                              
                              $('0_userdata.0.Haus.*').each(function(id) { 
                                  //log(id.toString())
                              //var currentElement = $(this);
                                 //console.log(currentElement);
                                sendTo('history.0', 'deleteRange', [{ id: id.toString(), start: startTimestamp, end: endTimestamp }]);
                              });
                              
                              
                              1 Antwort Letzte Antwort
                              0
                              Antworten
                              • In einem neuen Thema antworten
                              Anmelden zum Antworten
                              • Älteste zuerst
                              • Neuste zuerst
                              • Meiste Stimmen


                              Support us

                              ioBroker
                              Community Adapters
                              Donate

                              325

                              Online

                              32.5k

                              Benutzer

                              81.7k

                              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