Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. Node-Red Javascript Node - Berechnung ungewünscht doppelt

    NEWS

    • Neuer Blog: Fotos und Eindrücke aus Solingen

    • ioBroker@Smart Living Forum Solingen, 14.06. - Agenda added

    • ioBroker goes Matter ... Matter Adapter in Stable

    Node-Red Javascript Node - Berechnung ungewünscht doppelt

    This topic has been deleted. Only users with topic management privileges can see it.
    • mickym
      mickym Most Active last edited by mickym

      Ja Dein Problem wurde letztlich von @rewenode richtig beschrieben. Du triggerst Deine Funktion immer mehrfach mit jedem Input. Du führst beide Werte über den Funktionsnode zusammen, indem Du es über den Context speicherst. Den Taupunkt speicherst Du in einer Flow-Variablen - was unnötig ist.

      aber Du musst eigentlich in Deiner Funktion gar nicht soviel ändern:

      Speichere den Taupunkt doch einfach auch in einer Kontextvariable (eine FlowVariable erschliesst sich mir hier nicht) und sende nur, wenn sich was ändert bzw. der Wert valide ist.

      Ich habe Deinen Code mal ergänzt, so müsste es einigermaßen funktionieren:

      var tempf   = context.get('tempf')|| 0;
      var humidity = context.get('humidity')|| 0;
      var oldTp = context.get('contexttp') || 0;
      var tp = 0;
      
      if (msg.topic === "tempf") {
          tempf= msg.payload;
          context.set('tempf',msg.payload);
      
      } else if (msg.topic === "humidity") {
          humidity= msg.payload;
        context.set('humidity',msg.payload);
      }
      
        
      
      // Quelle: https://www.chemie-schule.de/KnowHow/Taupunkt
      // Gültigkeitsbereich Taupunkt:    -30°C <= tmp <= 70°C
      if (tempf >0 && humidity > 0 ) {
      	tp = ((241.2 * Math.log(humidity/100)) + ((4222.03716 * tempf) / (241.2 + tempf))) / (17.5043 - Math.log(humidity/100) - ((17.5043*tempf)/(241.2+tempf)));
      	tp = Math.round( tp * 100 ) / 100;
      	context.set("contexttp",tp);
      }
      
      if (oldTp !== tp) { 
      msg.payload = tp;
      msg.topic = "Taupunkt";
      return msg }
      else {
          return null;
      }
      
      

      Somit wird nur gesendet, wenn sich Taupunkt verändert hat - bzw. gültig ist. Ggf. halt noch NaN abfangen und die Funktion mit "return null" verlassen!

      Wenn die Bedingung nicht zutrifft - sendet Dein Funktionsnode einfach nichts und die eingehenden Nachrichten befüllen nur die internen Variablen, bis ein Ergebnis da ist.

      L 1 Reply Last reply Reply Quote 1
      • mickym
        mickym Most Active last edited by

        Ich hab den Code nochmal leicht editiert!

        1 Reply Last reply Reply Quote 0
        • L
          lemuba @mickym last edited by

          @mickym

          Vielen Dank erstmal - das muss noch ein kleiner Fehler im Script sein...?

          13.5.2020, 15:48:05node: Taupunktfunction : (error)
          "ReferenceError: oldTP is not defined (line 22, col 1)"

          Line 22:

          if (oldTP !== tp) {

          mickym 1 Reply Last reply Reply Quote 0
          • mickym
            mickym Most Active @lemuba last edited by mickym

            @lemuba Ja ich habe nochmal geändert. War ein großes P anstelle eines kleinen und aussßerdem darfst die Variablen nur mit richtigem Topic setzen. Kopier nochmal alles aus der Box.

            L 1 Reply Last reply Reply Quote 0
            • L
              lemuba @mickym last edited by

              @mickym

              Top - funktioniert!:

              13.5.2020, 16:01:28node: 81594f23.375458
              Taupunkt : msg.payload : number
              6.09

              Das kann ich nun sicherlich als Grundlage nehmen um auch weitere Berechnungen durchzuführen/zu optimieren, wie z.B. Windchill:


              //Source: https://homematic-forum.de/forum/viewtopic.php?t=50821
              if (msg.topic === "windspeed") {
              context.set('wind',msg.payload);
              } else if (msg.topic === "tempf") {
              context.set('temp',msg.payload);
              } else if (msg.topic === "humidity") {
              context.set('feucht',msg.payload);
              }

              var wind = context.get('wind');
              var temp = context.get('temp');
              var feucht = context.get('feucht');

              if (typeof wind === 'undefined' || typeof temp === 'undefined' || typeof feucht === 'undefined') {
              node.status({text:"null"});
              return null;
              }

              msg.topic = "0/CurrentTemperature"

              if (temp !== null) {
              if (temp < 27) {
              if (wind >= 5) {
              msg.payload = 13.12 + (0.6215 * temp) + ((0.3965 * temp) - 11.37) * Math.pow(wind, 0.16);
              } else {
              msg.payload = temp;
              }
              } else {
              msg.payload = -8.785 + (1.611 * TEMP) + (2.339 * FEUCHT) + (-0.146 * TEMP * FEUCHT) + (-0.01231 * Math.pow(TEMP,2)) + (-0.01642 * Math.pow(FEUCHT,2)) + (0.002212 * Math.pow(TEMP,2) * FEUCHT) + (0.0007255 * TEMP * Math.pow(FEUCHT,2)) + (-0.000003582 * Math.pow(TEMP,2) * Math.pow(FEUCHT,2));
              }
              } else {
              node.status({text:"Ebenfalls null"});
              return null;
              }

              var aktuelleWerte = "Temp: " + temp + ", Wind: " + wind + ", Feucht: " + feucht + ", Ergebnis: " + msg.payload;

              node.status({text:aktuelleWerte});

              return msg;

              L 1 Reply Last reply Reply Quote 0
              • L
                lemuba @lemuba last edited by lemuba

                @mickym

                Noch eine Frage - bedeutet das jetzt aber, daß der Taupunkt nur bei Temperaturen über 0 Grad berechnet wird?

                if (tempf >0 && humidity > 0 ) {
                tp = ((241.2 * Math.log(humidity/100)) + ((4222.03716 * tempf) / (241.2 + tempf))) / (17.5043 - Math.log(humidity/100) - ((17.5043*tempf)/(241.2+tempf)));
                tp = Math.round( tp * 100 ) / 100;
                context.set("contexttp",tp);
                }

                mickym 1 Reply Last reply Reply Quote 0
                • mickym
                  mickym Most Active @lemuba last edited by

                  @lemuba Ja da hast Du Recht - letztlich diente die Bedingung nur dazu, zu überprüfen ob alles initialisiert ist. Entweder Du fängst das ab mit != NaN oder Du nimmst einfach einen anderen Wert.
                  Du kannst ja die Temperatur mit -100 initialisieren und dann in der Abfrage > -100 arbeiten. 😉

                  L 1 Reply Last reply Reply Quote 1
                  • L
                    lemuba @mickym last edited by lemuba

                    @mickym

                    Ok, bin aber gerade am schauen ob eine Taupunktberechnung unter Null Grad überhaupt relevant ist - ein anderes, dann metrolgisches Thema... 🙂

                    1 Reply Last reply Reply Quote 0
                    • mickym
                      mickym Most Active last edited by mickym

                      if (!(isNaN(tempf) || isNaN(humidity))) { ...

                      müsste eigentlich auch funktionieren als Bedingung

                      L 1 Reply Last reply Reply Quote 0
                      • L
                        lemuba @mickym last edited by

                        @mickym sagte in Node-Red Javascript Node - Berechnung ungewünscht doppelt:

                        !(isNaN(tempf) || isNaN(humidity))

                        Ich habe nun die -100 Variante eingebaut, weil die habe ich glaube ich verstanden... und funktioniert so weit:


                        var tempf = context.get('tempf')|| -100;
                        var humidity = context.get('humidity')|| -100;
                        var oldTp = context.get('contexttp') || -100;
                        var tp = -100;

                        if (msg.topic === "tempf") {
                        tempf= msg.payload;
                        context.set('tempf',msg.payload);

                        } else if (msg.topic === "humidity") {
                        humidity= msg.payload;
                        context.set('humidity',msg.payload);
                        }

                        // Quelle: https://www.chemie-schule.de/KnowHow/Taupunkt
                        // Gültigkeitsbereich Taupunkt: -30°C <= tmp <= 70°C
                        if (tempf > -100 && humidity > -100 ) {
                        tp = ((241.2 * Math.log(humidity/100)) + ((4222.03716 * tempf) / (241.2 + tempf))) / (17.5043 - Math.log(humidity/100) - ((17.5043*tempf)/(241.2+tempf)));
                        tp = Math.round( tp * 100 ) / 100;
                        context.set("contexttp",tp);
                        }

                        if (oldTp !== tp) {
                        msg.payload = tp;
                        msg.topic = "Taupunkt";
                        return msg }
                        else {
                        return null;
                        }

                        Mir war nicht ganz klar wo das reingehört hätte:

                        !(isNaN(tempf) || isNaN(humidity))

                        mickym 1 Reply Last reply Reply Quote 0
                        • mickym
                          mickym Most Active @lemuba last edited by

                          @lemuba

                          var tempf   = context.get('tempf')|| 0;
                          var humidity = context.get('humidity')|| 0;
                          var oldTp = context.get('contexttp') || 0;
                          var tp = 0;
                          
                          if (msg.topic === "tempf") {
                              tempf= msg.payload;
                              context.set('tempf',msg.payload);
                          
                          } else if (msg.topic === "humidity") {
                              humidity= msg.payload;
                            context.set('humidity',msg.payload);
                          }
                          
                            
                          
                          // Quelle: https://www.chemie-schule.de/KnowHow/Taupunkt
                          // Gültigkeitsbereich Taupunkt:    -30°C <= tmp <= 70°C
                          if (!(isNaN(tempf) || isNaN(humidity))) {
                          	tp = ((241.2 * Math.log(humidity/100)) + ((4222.03716 * tempf) / (241.2 + tempf))) / (17.5043 - Math.log(humidity/100) - ((17.5043*tempf)/(241.2+tempf)));
                          	tp = Math.round( tp * 100 ) / 100;
                          	context.set("contexttp",tp);
                          }
                          
                          if (oldTp !== tp) { 
                          msg.payload = tp;
                          msg.topic = "Taupunkt";
                          return msg }
                          else {
                              return null;
                          }
                          
                          
                          L 1 Reply Last reply Reply Quote 1
                          • L
                            lemuba @mickym last edited by lemuba

                            @mickym sagte in Node-Red Javascript Node - Berechnung ungewünscht doppelt:

                            var tempf = context.get('tempf')|| 0;
                            var humidity = context.get('humidity')|| 0;
                            var oldTp = context.get('contexttp') || 0;
                            var tp = 0;

                            if (msg.topic === "tempf") {
                            tempf= msg.payload;
                            context.set('tempf',msg.payload);

                            } else if (msg.topic === "humidity") {
                            humidity= msg.payload;
                            context.set('humidity',msg.payload);
                            }

                            // Quelle: https://www.chemie-schule.de/KnowHow/Taupunkt
                            // Gültigkeitsbereich Taupunkt: -30°C <= tmp <= 70°C
                            if (!(isNaN(tempf) || isNaN(humidity))) {
                            tp = ((241.2 * Math.log(humidity/100)) + ((4222.03716 * tempf) / (241.2 + tempf))) / (17.5043 - Math.log(humidity/100) - ((17.5043*tempf)/(241.2+tempf)));
                            tp = Math.round( tp * 100 ) / 100;
                            context.set("contexttp",tp);
                            }

                            if (oldTp !== tp) {
                            msg.payload = tp;
                            msg.topic = "Taupunkt";
                            return msg }
                            else {
                            return null;
                            }

                            Funktioniert auch... nun habe ich estmal was zum weiterspielen/testen... Vielen Dank erstmal!
                            Den Link/Quelle zu diesem Beitrag werde ich gleich mal im Script notieren....

                            1 Reply Last reply Reply Quote 0
                            • First post
                              Last post

                            Support us

                            ioBroker
                            Community Adapters
                            Donate

                            841
                            Online

                            31.8k
                            Users

                            80.0k
                            Topics

                            1.3m
                            Posts

                            javascript node-red
                            3
                            15
                            1463
                            Loading More Posts
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes
                            Reply
                            • Reply as topic
                            Log in to reply
                            Community
                            Impressum | Datenschutz-Bestimmungen | Nutzungsbedingungen
                            The ioBroker Community 2014-2023
                            logo