Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. Timerfunktion

    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

    Timerfunktion

    This topic has been deleted. Only users with topic management privileges can see it.
    • liv-in-sky
      liv-in-sky last edited by

      hab es noch etwas angepasst - jetzt werden die start- und stop-objects auch noch zurückgesetzt. das ganze läuft mit sekunden. die beiden switch sorgen dafür, das nur bei true etwas weitergegeben wird.

      code für funktion:

      var value  = context.get('value') || 10000;
      
      if (msg.topic == "value")
      {
        value = msg.payload * 1000;
        context.set('value',value);  
      }
      
      else if (msg.topic == "start")
      {
      
      var timer = setTimeout(function(){msg.payload = "now";
      node.send([msg,{ payload: false }]);
      msg.payload = false;
      node.send([null,null,msg,{ payload: false }]);
      var countdownstop  = context.get('countdown');
      clearInterval(countdownstop)},value);
      context.set('timer',timer); 
      
      var count = value/1000;
      //count = count -1 ;
      msg.payload=count;
      node.send([null,msg]);
      var countdown = setInterval(function() {count=count-1;
      msg.payload=count;
      node.send([null,msg]) }, 1000);
      context.set('countdown',countdown);
      
      }
      
      else if (msg.topic == "stop")
      {
      var timerstop  = context.get('timer')
      clearTimeout(timerstop);
      msg.payload = false;
      node.send([null,null,null,msg,{ payload: false }]);
      node.send([null,null,msg,{ payload: false }]);
      
      var countdownstop  = context.get('countdown')
      clearInterval(countdownstop);
      
      }
      
      

      4447_timernodered.png

      1 Reply Last reply Reply Quote 0
      • G
        Garf last edited by

        @liv-in-sky:

        hab es noch etwas angepasst - jetzt werden die start- und stop-objects auch noch zurückgesetzt. das ganze läuft mit sekunden. die beiden switch sorgen dafür, das nur bei true etwas weitergegeben wird. `
        In der Zeit wo ich noch versucht habe das Javascript zu verstehen, hast Du es schon verändert und deutlich verbessert. 😮

        Mein Respekt dafür. :mrgreen:

        @liv-in-sky:

        jetzt werden die start- und stop-objects auch noch zurückgesetzt. das ganze läuft mit sekunden. die beiden switch sorgen dafür, das nur bei true etwas weitergegeben wird. `
        Im Dashboard läuft es noch nicht so ganz rund, aber grundsätzlich sind die Funktionen schon gegeben. Ein Javascript in dieser Form habe ich so auch noch nicht gesehen.

        1 Reply Last reply Reply Quote 0
        • Sefina
          Sefina last edited by

          Klasse es läuft,

          Die nächste Frage währe was müsste man ändern das der Payload von Start weitergegeben wird und nicht ein Fixer wert.

          Theoretisch Müsste ich ja den Payload in einer Variable Speichern und dann ausgeben ?

          Kann mir da nochmal wer helfen ?

          Warum benötigt man aber nun noch ein Counter, die Funktion ist ja jetzt wesentlich größer ?

          MfG

          1 Reply Last reply Reply Quote 0
          • liv-in-sky
            liv-in-sky last edited by

            @garf

            habe mich mehr auf vis für die ausgabe konzentriert - mehr benötige ich nicht
            4447_erstes.gif

            1 Reply Last reply Reply Quote 0
            • liv-in-sky
              liv-in-sky last edited by

              @selfina

              vielleicht zu früh am morgen aber kannst du das anders erklären! was meinst du mit fixer wert von start. start ist entweder true oder false - gestartet oder eben nicht.

              anbei die ansicht von iobroker-objekte - alle infos sind vorhanden (bild2). falls du die start info aber auch noch für etwas anderes in nodered brauchst musst du eine nodered variable anlegen. die functionnode "Start als variable" erstellt eine variable, die im ganzen flow gültig ist

              var test = msg.payload;
              if (test == "true") {
                   flow.set('startvar',true);
                   msg.payload = flow.get('startvar')
              }
              else {
                  flow.set('startvar',false);
                  msg.payload = flow.get('startvar')
              }
              return msg;
              
              

              4447_2tes.gif
              4447_startvar.png

              1 Reply Last reply Reply Quote 0
              • Sefina
                Sefina last edited by

                Du schreibst ja msg.payload = "now" das kommt als String ja am Ende Raus. Ich benötige aber den String der beim Topic mitgeschickt wird.

                Da ich true oder false nicht benötige.

                Also muss ich am Anfang sagen

                var Test = msg.payload

                und dann im Timer

                msg.payload= Test

                Ist das Richtig ?

                MfG

                1 Reply Last reply Reply Quote 0
                • liv-in-sky
                  liv-in-sky last edited by

                  wenn du das topic benötigst

                  var test = msg.topic

                  du kannst hinter jede node eine debug (grün)setzen - in der definition dieser debug node kannst du auswählen: complete msg object - darin siehst du dann, was du für möglichkeiten hast.

                  msg = ein object - hat verschiedene "unterpunkte" - mit punkten getrennt.

                  msg.topic, msg.title, msg.payload sind teile davon

                  1 Reply Last reply Reply Quote 0
                  • Sefina
                    Sefina last edited by

                    Hi erstmal und vielen dank für deine/eure Hilfe,

                    da ich momentan anfange zu verstehen wie es geht bitte ich dich etwas einfacher mir mein Fehler zu erklären.

                    Zur Info :

                    Deine Funktion habe ich etwas angepasst, Variablen geändert und die Funktion etwas angepasst.

                    3 Eingänge :

                    A = start/"Nachricht"

                    B = stop

                    C= TimerZeit/"Zeit in Sekunden"

                    3 Ausgänge

                    A = sendet die Nachricht ohne Topic nach abgeschlossenen Timer

                    B = zählt die Sekunden herunter und beim stop wird die Nachricht "Abbruch" geschrieben

                    C = gibt am anfang ein true und beim beenden (egal ob Manuel oder Automatisch) ein false

                    An sich funktioniert es schon recht gut, leider bekomme ich viel zu viele Meldungen und ich weiß nicht woher.

                    Ich hoffe du kannst mir da etwas erklären ….

                    Zum Importieren :

                    [{"id":"344d0103.95f4be","type":"function","z":"45099a56.32a174","name":"Timer","func":"var TimerZeit  = context.get('TimerZeit') || 10000;\n\nif (msg.topic == \"TimerZeit\")\n{\n  TimerZeit = msg.payload * 1000;\n  context.set('TimerZeit',TimerZeit);  \n}\n\nif (msg.topic == \"start\")\n{\n    node.send([null,msg,{ payload: true }]);\n    var msgStart = msg.payload\n    var timer = setTimeout(function()\n    {\n        var countdownstop  = context.get('countdown');\n        msg.payload = msgStart;\n        msg.topic = \"\";\n        node.send([null,msg,{ payload: false }]);\n        node.send([msg,{ payload: 0 }]);\n        clearInterval(countdownstop)},TimerZeit);\n        context.set('timer',timer); \n\n        //Abwärtzzähler !!!! (Mitte) ;\n        var count = TimerZeit/1000;\n        msg.payload=count;\n        node.send([null,msg]);\n        var countdown = setInterval(function() \n        {\n            count=count-1;\n            msg.payload=count;\n            node.send([null,msg]) }, 1000);\n            context.set('countdown',countdown);\n}\n\nif (msg.topic == \"stop\")\n{\n    var timerstop  = context.get('timer')\n    clearTimeout(timerstop);\n    node.send([msg,{ payload: \"Abbruch\" }]);\n    node.send([null,msg,{ payload: false }]);\n\n    var countdownstop  = context.get('countdown')\n    clearInterval(countdownstop);\n}\n","outputs":3,"noerr":0,"x":250,"y":280,"wires":[["3dd89880.551ff8"],["af2b75eb.13f0c8"],["d6058e9c.fb98f"]]},{"id":"da490012.aee8e","type":"inject","z":"45099a56.32a174","name":"","topic":"start","payload":"ON","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":240,"wires":[["344d0103.95f4be"]]},{"id":"17ace082.518b3f","type":"inject","z":"45099a56.32a174","name":"","topic":"stop","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":280,"wires":[["344d0103.95f4be"]]},{"id":"9327c7b8.15d208","type":"inject","z":"45099a56.32a174","name":"","topic":"TimerZeit","payload":"2","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":320,"wires":[["344d0103.95f4be"]]},{"id":"3dd89880.551ff8","type":"debug","z":"45099a56.32a174","name":"Oben","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":370,"y":240,"wires":[]},{"id":"d6058e9c.fb98f","type":"debug","z":"45099a56.32a174","name":"Unten","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":370,"y":320,"wires":[]},{"id":"af2b75eb.13f0c8","type":"debug","z":"45099a56.32a174","name":"Mitte","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":370,"y":280,"wires":[]}]
                    

                    Zum lesen :

                    var TimerZeit  = context.get('TimerZeit') || 10000;
                    
                    if (msg.topic == "TimerZeit")
                    {
                      TimerZeit = msg.payload * 1000;
                      context.set('TimerZeit',TimerZeit);  
                    }
                    
                    if (msg.topic == "start")
                    {
                        node.send([null,msg,{ payload: true }]);
                        var msgStart = msg.payload
                        var timer = setTimeout(function()
                        {
                            var countdownstop  = context.get('countdown');
                            msg.payload = msgStart;
                            msg.topic = "";
                            node.send([null,msg,{ payload: false }]);
                            node.send([msg,{ payload: 0 }]);
                            clearInterval(countdownstop)},TimerZeit);
                            context.set('timer',timer); 
                    
                            //Abwärtzzähler !!!! (Mitte) ;
                            var count = TimerZeit/1000;
                            msg.payload=count;
                            node.send([null,msg]);
                            var countdown = setInterval(function() 
                            {
                                count=count-1;
                                msg.payload=count;
                                node.send([null,msg]) }, 1000);
                                context.set('countdown',countdown);
                    }
                    
                    if (msg.topic == "stop")
                    {
                        var timerstop  = context.get('timer')
                        clearTimeout(timerstop);
                        node.send([msg,{ payload: "Abbruch" }]);
                        node.send([null,msg,{ payload: false }]);
                    
                        var countdownstop  = context.get('countdown')
                        clearInterval(countdownstop);
                    }
                    
                    

                    MfG

                    1 Reply Last reply Reply Quote 0
                    • liv-in-sky
                      liv-in-sky last edited by

                      soll es so aussehen? (zum importieren)

                      [
                          {
                              "id": "339cf0c4.15d68",
                              "type": "function",
                              "z": "39af8664.6b097a",
                              "name": "Timer",
                              "func": "var TimerZeit  = context.get('TimerZeit') || 10000;\n\nif (msg.topic == \"TimerZeit\")\n{\n  TimerZeit = msg.payload * 1000;\n  context.set('TimerZeit',TimerZeit);  \n  msg.payload=TimerZeit / 1000;\n  node.send([null,msg]);\n}\n\nif (msg.topic == \"start\")\n{\n    msg.payload = true;\n    node.send([null,null,msg]);\n    var msgStart = msg.payload\n    var timer = setTimeout(function()\n    {\n        var countdownstop  = context.get('countdown');\n        msg.payload = \"FERTIG\";\n        msg.topic = null;\n        node.send([msg]);\n        //node.send([msg,{ payload: 0 }]);\n        clearInterval(countdownstop)},TimerZeit);\n        context.set('timer',timer); \n\n        //Abwärtzzähler !!!! (Mitte) ;\n        var count = TimerZeit/1000;\n        msg.payload=count;\n        node.send([null,msg]);\n        var countdown = setInterval(function() \n        {\n            count=count-1;\n            msg.payload=count;\n            node.send([null,msg]) }, 1000);\n            context.set('countdown',countdown);\n}\n\nif (msg.topic == \"stop\")\n{\n    var timerstop  = context.get('timer')\n    clearTimeout(timerstop);\n    msg.payload = \"ABBRUCH\";\n    node.send([null,msg]);\n    //node.send([null,null,msg,{ payload: false }]);\n\n    var countdownstop  = context.get('countdown')\n    clearInterval(countdownstop);\n}\n",
                              "outputs": 3,
                              "noerr": 0,
                              "x": 690,
                              "y": 360,
                              "wires": [
                                  [
                                      "bf530166.7c0da"
                                  ],
                                  [
                                      "8bb4f1d4.80d58"
                                  ],
                                  [
                                      "cda4bbdb.7efd18"
                                  ]
                              ]
                          },
                          {
                              "id": "d760f75f.a6c378",
                              "type": "inject",
                              "z": "39af8664.6b097a",
                              "name": "",
                              "topic": "start",
                              "payload": "ON",
                              "payloadType": "str",
                              "repeat": "",
                              "crontab": "",
                              "once": false,
                              "onceDelay": 0.1,
                              "x": 560,
                              "y": 320,
                              "wires": [
                                  [
                                      "339cf0c4.15d68"
                                  ]
                              ]
                          },
                          {
                              "id": "191100f2.c033cf",
                              "type": "inject",
                              "z": "39af8664.6b097a",
                              "name": "",
                              "topic": "stop",
                              "payload": "",
                              "payloadType": "str",
                              "repeat": "",
                              "crontab": "",
                              "once": false,
                              "onceDelay": 0.1,
                              "x": 550,
                              "y": 360,
                              "wires": [
                                  [
                                      "339cf0c4.15d68"
                                  ]
                              ]
                          },
                          {
                              "id": "8952b490.9967f8",
                              "type": "inject",
                              "z": "39af8664.6b097a",
                              "name": "",
                              "topic": "TimerZeit",
                              "payload": "4",
                              "payloadType": "num",
                              "repeat": "",
                              "crontab": "",
                              "once": false,
                              "onceDelay": 0.1,
                              "x": 560,
                              "y": 400,
                              "wires": [
                                  [
                                      "339cf0c4.15d68"
                                  ]
                              ]
                          },
                          {
                              "id": "bf530166.7c0da",
                              "type": "debug",
                              "z": "39af8664.6b097a",
                              "name": "Oben",
                              "active": true,
                              "tosidebar": true,
                              "console": false,
                              "tostatus": false,
                              "complete": "true",
                              "x": 810,
                              "y": 320,
                              "wires": []
                          },
                          {
                              "id": "cda4bbdb.7efd18",
                              "type": "debug",
                              "z": "39af8664.6b097a",
                              "name": "Unten",
                              "active": true,
                              "tosidebar": true,
                              "console": false,
                              "tostatus": false,
                              "complete": "true",
                              "x": 810,
                              "y": 400,
                              "wires": []
                          },
                          {
                              "id": "8bb4f1d4.80d58",
                              "type": "debug",
                              "z": "39af8664.6b097a",
                              "name": "Mitte",
                              "active": true,
                              "tosidebar": true,
                              "console": false,
                              "tostatus": false,
                              "complete": "true",
                              "x": 810,
                              "y": 360,
                              "wires": []
                          }
                      ]
                      

                      was mir auffiel:

                      du hast die function mit 3 ausgängen definiert - du hast aber nur 2 benutzt - der driite wurde nie eingesetzt.

                      habe noch ein paar zeilen auskommentiert - brauchst du nicht.

                      habe timerzeit beim setzen auch ausgegeben.

                      PS: ich bin übrigens kein prograsmmierprofi - ich benutze in iobroker eigentlich nur blockly, da mir die javascripts zu unübersichtlich sind. in node-red spiele ich etwas mit den scripts - da kürzer und übersichtlicher - für mich. bin in node-red java-technisch irgendwie effektiver ? ich kann dir nicht sagen, ob das alles optimal oder resoucen-schonend programmiert ist - vielleicht gibt es bessere wege, so etwas zu machen 🙂

                      1 Reply Last reply Reply Quote 0
                      • Sefina
                        Sefina last edited by

                        Und ich nutze als Hauptsystem OpenHab und möchte von den Rules weg.

                        Da ich mit Javascripts und NodeJS kein Plan habe Versuche ich es halt zu lernen.

                        Leider lernt man bei Node Red nen neues Protokoll was halt nicht immer einfach ist.

                        Danke dir schaue es mir nachher mal an.

                        MfG

                        1 Reply Last reply Reply Quote 0
                        • liv-in-sky
                          liv-in-sky last edited by

                          kenne openhab nicht

                          habe mit iobroker begonnen, weil blockly darin war - damit kann ich erstmal alles wichtige schnell und einfach machen - javascript wir über die "jahre" besser werden - zumindest kann ich scripts kopieren und verstehe etwas.

                          letztlich macht man sich auch nur "regeln" im iobroker (wenn dann) - sind halt in js etwas flexibler und weiter ausbaubar - denke ich

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

                          Support us

                          ioBroker
                          Community Adapters
                          Donate

                          562
                          Online

                          31.7k
                          Users

                          79.9k
                          Topics

                          1.3m
                          Posts

                          3
                          14
                          2331
                          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