Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. Blockly
    5. Aus JSON 136 Datenpunkte erzeugen

    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

    Aus JSON 136 Datenpunkte erzeugen

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

      @videonisse

      hier ein script

      • du musst die id des dp eingeben - 2. zeile

      • das musst du überall angleichen: 0_userdata.0.TELEGRAF2 -- anstatt telegraf nimmst du einen anderen ordner - zeile 7 und 8

      • das ganze braucht noch einen trigger, damit das immer upgedatet wird - zum testen aber einfach mal aufrufen - dann sollten die dp erstellt werden

      
      let myvar = JSON.parse(getState(id).val)
      
      var keys = Object.keys(myvar);
      for(let u=0;u<keys.length;u++){
          let theVal=myvar[keys[u]]
       if(existsState('0_userdata.0.TELEGRAF2.'+keys[u])) setState('0_userdata.0.TELEGRAF2.'+keys[u],theVal);
               else createState('0_userdata.0.TELEGRAF2.'+keys[u], theVal, { name: keys[u],unit:'' }); 
      }
      
      1 Reply Last reply Reply Quote 0
      • paul53
        paul53 @Videonisse last edited by paul53

        @videonisse sagte: mit eine Schleife neue Datenpunkte erzeugen und updaten... Hat jemand ein beispiel wie man so was macht?

        Hier wird so etwas ähnliches gemacht.

        const idJSON = 'mqtt.1.ThermIQ.ThermIQ-room.data';
        const path = '0_userdata.0.ThermIQ.'; // Pfad anpassen
         
        on(idJSON, function (dp) {
            let obj = JSON.parse(dp.state.val);
            Object.keys(obj).forEach(function(key) {
                if(existsState(path + key)) setState(path + key, obj[key], true);
                else createState(path + key, obj[key], {read: true, write: false, type: typeof obj[key], name: key});
            });
        });
        
        V 1 Reply Last reply Reply Quote 0
        • V
          Videonisse @paul53 last edited by Videonisse

          @paul53 Danke! Ist fuer mich so einfach, kann ich dein skript benutzen wie es ist und nur the "const" ändern?

          Soll idJSON = mein DP mit JSON sein? (es ist mqtt.1.ThermIQ.ThermIQ-room.data)

          const JSPath = '0_userdata.0'; // JS- Pfad
          const parsedStatesPath = JSPath + ".ThermIQ."; // Pfad fuer geparste States
          const idJSON = 'mqtt.1.ThermIQ.ThermIQ-room.data';   // Full path to the DP with the JSON?
           
          on(idJSON, function (dp) {
              let JsonObj = JSON.parse(dp.state.val);
              Object.keys(JsonObj).forEach(function(key) {
                  if(existsState(parsedStatesPath + key)) setState(parsedStatesPath + key, JsonObj[key], true);
                  else createState(parsedStatesPath + key, JsonObj[key], {read: true, write: false, type: typeof JsonObj[key], name: key});
              });
          });
          
          
          paul53 1 Reply Last reply Reply Quote 0
          • paul53
            paul53 @Videonisse last edited by paul53

            @videonisse
            Habe gerade meinen Beitrag oben um ein Skript ergänzt.
            Deine Anpassungen sollten ebenso funktionieren.

            V 1 Reply Last reply Reply Quote 0
            • V
              Videonisse @paul53 last edited by

              @paul53 Funktioniert einwandfrei und sogar erzeugst die Objekte mit richtigen Type! Vielen Dank für ein sehr elegant geschriebenes Skript! 🙂

              Kleine Frage; Der Wert "timestamp" ist ein Linux-Datum, aber es fehlen die Millisekunden. Ist es möglich, dies anzupassen und DP mit einem neuen Wert zu überschreiben, der am Ende "000" hinzufügt? Oder muss ich das separat machen?

              paul53 1 Reply Last reply Reply Quote 0
              • paul53
                paul53 @Videonisse last edited by

                @videonisse sagte: Linux-Datum, aber es fehlen die Millisekunden.

                Linux (Unix) verwendet Sekunden. Wozu benötigst Du Millisekunden?

                @videonisse sagte in Aus JSON 136 Datenpunkte erzeugen:

                Oder muss ich das separat machen?

                Ja, als erstes in der Schleife:

                        if(key == 'timestamp') obj[key] = 1000 * obj[key];
                
                V 1 Reply Last reply Reply Quote 0
                • V
                  Videonisse @paul53 last edited by

                  @paul53 Danke, funktioniert natürlich gut!

                  Du hast recht, es ist schon in Linux Format (seconds), aber ich brauche es in Javascript Format (milliseconds). Ich weiß aber nicht, ob das noch richtigist, wollte Object/State Role = Datum haben. Dann muss es angeblich in milliseconds sein.

                  "date (common.type = number - epoch seconds * 1000" Source

                  Ich möchte auch gern die Variable "idJSON" in Blockly setzen aber verstehe nicht wie ich dass richtig mache.

                  Kann ich irgenwie die Variabel "object ID" von den Trigger benutzen? Ich habe mit unten probiert aber geht nicht.

                  0c407deb-e349-4b06-9bdd-279f40ffd1ef-image.png

                  56a239e8-dd24-4c42-a8f7-be354bfc465c-image.png

                  paul53 1 Reply Last reply Reply Quote 0
                  • paul53
                    paul53 @Videonisse last edited by paul53

                    @videonisse sagte: Kann ich irgenwie die Variabel "object ID" von den Trigger benutzen?

                    Der Block ist schon richtig, aber "value" (oder so ähnlich) selektieren.
                    Wenn schon mit Blockly, dann

                    Bild_2021-11-13_222902.png

                    Inhalt der Funktion ThermiqUpdate(source):

                    let obj = JSON.parse(source);
                    Object.keys(obj).forEach(function(key) {
                        if(key == 'timestamp') obj[key] = 1000 * obj[key];
                        if(existsState(path + key)) setState(path + key, obj[key], true);
                        else createState(path + key, obj[key], {read: true, write: false, type: typeof obj[key], name: key});
                    });
                    
                    V 1 Reply Last reply Reply Quote 0
                    • V
                      Videonisse @paul53 last edited by Videonisse

                      @paul53 Works like a charm! Nochmals, viele viele Danke! 🙂

                      Ich habe letzen Tage Probleme javascript.js und hatte zb error: "JavaScript heap out of memory"

                      Kann die Ursache den Code "on(idJSON, function (dp) {" zusammen mit Blockly gewesen sein?

                      Homoran paul53 2 Replies Last reply Reply Quote 0
                      • Homoran
                        Homoran Global Moderator Administrators @Videonisse last edited by

                        @videonisse sagte in Aus JSON 136 Datenpunkte erzeugen:

                        JavaScript heap out of memory

                        gilt deine Signatur noch?

                        ioBroker v3.3.18, Debian 10 Buster 64-bit (Vmware 6.5 VM)

                        da solltest du eigentlich genug RAM haben (wenn das auf einem PC läuft und in der VM genug RAM angemeldet ist)

                        V 1 Reply Last reply Reply Quote 0
                        • paul53
                          paul53 @Videonisse last edited by

                          @videonisse sagte: Kann die Ursache den Code "on(idJSON, function (dp) {" zusammen mit Blockly gewesen sein?

                          Ja, da der Trigger "on(idJSON, function (dp) {" innerhalb des Blockly-Triggers im Laufe der Zeit zu sehr vielen Trigger-Ereignissen führt.

                          1 Reply Last reply Reply Quote 0
                          • V
                            Videonisse @Homoran last edited by

                            @homoran Ja, ich habe normale weise 60% frei von 2GB ram. Aber mit die letzte Code Änderung von @paul53 bleibt javascript.js auf ~245MB

                            Alles ist jetzt stabil und keine Fehler in log.

                            Homoran 1 Reply Last reply Reply Quote 0
                            • Homoran
                              Homoran Global Moderator Administrators @Videonisse last edited by

                              @videonisse sagte in Aus JSON 136 Datenpunkte erzeugen:

                              ich habe normale weise 60% frei von 2GB ram.

                              das ist unter Linux nicht normal!
                              www.linuxatemyram.com

                              @videonisse sagte in Aus JSON 136 Datenpunkte erzeugen:

                              bleibt javascript.js auf ~245MB

                              meine beiden Instanzen haben 160 und 180 MB und ich habe nicht viel/aufwändiges

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

                              Support us

                              ioBroker
                              Community Adapters
                              Donate

                              720
                              Online

                              31.9k
                              Users

                              80.1k
                              Topics

                              1.3m
                              Posts

                              4
                              15
                              727
                              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