Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. gelöst: json tabelle erstellen

    NEWS

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

    • ioBroker goes Matter ... Matter Adapter in Stable

    • Monatsrückblick - April 2025

    gelöst: json tabelle erstellen

    This topic has been deleted. Only users with topic management privileges can see it.
    • M
      michihorn last edited by michihorn

      Hallo
      ich möchte gerne eine json-Tabelle erstellen. (habe ich noch nie gemacht)
      Inhalt soll einmal monatlich am letzten Tag des Monats der Zählerwert des Smartmeter Adapters sein. Das bedeutet, jeden Monat soll eine neue Zeile erstellt werden mit den Feldnamen Monat/Jahr und Zählerwert. Kann mich jemand mal in die richtige Richtung schupsen?
      Gruß
      Michael

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

        @michihorn sagte: in die richtige Richtung schupsen?

        const idZaehler = 'abc';
        const idJSON = '0_userdata.0.xyz';
        
        const arr = JSON.parse(getState(idJSON).val); // Mind. ein '[]' muss vorhanden sein
        
        schedule('50 59 23 * * *', function() {
            const tomorrow = Date.now() + 20000;
            if(new Date(tomorrow).getDate() == 1) {
                const obj = {
                    'Monat/Jahr': formatDate(new Date(), 'MM/YYYY'),
                    'Zählerstand': getState(idZaehler).val
                };
                arr.push(obj);
                // Anzahl Einträge verringern? 
                setState(idJSON, JSON.stringify(arr), true);
            }
        });
        
        1 Reply Last reply Reply Quote 1
        • M
          michihorn last edited by michihorn

          @paul53 sagte in json tabelle erstellen:

          const idJSON = '0_userdata.0.xyz';

          Hallo Paul,
          zum Testen habe ich für idZähler, den aktuellen Verbrauch genommen,
          den DP idJSON hast Du als Zustandstyp "json" in den Objecten angelegt?
          Denn ich komme folgende Warnmeldungen

          const idZaehler = 'smartmeter.0.1-0:16_7_0__255.value';
          const idJSON = '0_userdata.0.Haus.Energie.Strom_JSON';
           
          const arr = JSON.parse(getState(idJSON).val);
          
          
          schedule('50 59 23 * * *', function() {
              const tomorrow = Date.now() + 20000;
              if(new Date(tomorrow).getDate() == 1) {
                  const obj = {
                      'Monat/Jahr': formatDate(new Date(), 'MM/YYYY'),
                      'Zählerstand': getState(idZaehler).val
                  };
                  arr.push(obj);
                   //Anzahl Einträge verringern? 
                  setState(idJSON, JSON.stringify(arr), true);
              }
          });
          
          11:14:54.661	info	javascript.0 (1239) Start javascript script.js.Trash.Skript_1json
          11:14:54.669	warn	javascript.0 (1239) at script.js.Trash.Skript_1json:4:24
          11:14:54.669	warn	javascript.0 (1239) at script.js.Trash.Skript_1json:20:3
          11:14:54.683	info	javascript.0 (1239) script.js.Trash.Skript_1json: registered 0 subscriptions, 1 schedule, 0 messages, 0 logs and 0 file subscriptions
          
          paul53 1 Reply Last reply Reply Quote 0
          • paul53
            paul53 @michihorn last edited by paul53

            @michihorn sagte: ich komme folgende Warnmeldungen

            Trage manuell ein leeres Array [] ein.

            @michihorn sagte in json tabelle erstellen:

            für idZähler, den aktuellen Verbrauch genommen,

            Du meinst den Zählerstand in kWh?

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

              @paul53 sagte in json tabelle erstellen:

              Du meinst den Zählerstand in kWh?

              Nein in Watt

              Möglicherweise habe ich deine Anweisung falsch gedeutet, das leere Array habe ich in den Objekten eingetragen, die Warnmeldung ist weg. Wie kann ich das Script auf Funktion test, den Schedule auf "minütlich" setzen reicht nicht

              paul53 2 Replies Last reply Reply Quote 0
              • paul53
                paul53 @michihorn last edited by paul53

                @michihorn sagte: Nein in Watt

                Das ist die momentane Leistung!

                Du möchtest den Zählerstand in die Tabelle schreiben oder den Monatsverbrauch? Falls Monatsverbrauch, dann wird ein Hilfs-Datenpunkt benötigt, in dem sich der Zählerstand am Monatsende gemerkt wird.

                @michihorn sagte in json tabelle erstellen:

                die Warnmeldung ist allerdings beblieben

                Ich meinte auch: [] manuell im Tab "Objekte" eintragen. Sonst im Skript:

                var arr = [];
                if(getState(idJSON).val) arr = JSON.parse(getState(idJSON).val);
                
                1 Reply Last reply Reply Quote 0
                • paul53
                  paul53 @michihorn last edited by

                  @michihorn sagte: Wie kann ich das Script auf Funktion test

                  Den Monatswechsel abwarten.

                  M 1 Reply Last reply Reply Quote 2
                  • M
                    michihorn @paul53 last edited by

                    @paul53 sagte in json tabelle erstellen:

                    Den Monatswechsel abwarten.

                    ☺ So lange kann ich nicht mehr warten. Hab mal ganz brutal alles ausgeklammert:

                    const idZaehler = 'smartmeter.0.1-0:1_8_0__255.value';
                    const idJSON = '0_userdata.0.Haus.Energie.Strom_JSON';
                    
                    var arr = JSON.parse(getState(idJSON).val);
                    
                    //schedule('50 59 23 * * *', function() {
                    schedule("*/1 * * * *", function () {
                        var txt = '💬 Neuer Zählerstand gespeichert für ' + formatDate((Date.now()), "MM.JJ");
                        WhatsAppBot([0], txt);
                    
                        //const tomorrow = Date.now() + 20000;
                        //const tomorrow = Date.now();
                        //if (new Date(tomorrow).getDate() == 1) {
                            const obj = {
                                'Monat/Jahr': formatDate(new Date(), 'MM/YYYY'),
                                'Zählerstand': getState(idZaehler).val
                            };
                            arr.push(obj);
                            //Anzahl Einträge verringern? 
                            setState(idJSON, JSON.stringify(arr), true);
                      //  }
                    });
                    

                    Das Ergebnis in den Objekten sieht wie erwartet aus:

                    [{"Monat/Jahr":"01/2024","Zählerstand":3178.1119}]
                    

                    Das ganze dann in einer Tabelle:
                    zählerstand.png

                    Danke Paul

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

                    Support us

                    ioBroker
                    Community Adapters
                    Donate

                    1.0k
                    Online

                    31.7k
                    Users

                    79.7k
                    Topics

                    1.3m
                    Posts

                    javascript
                    2
                    8
                    312
                    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