Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. JavaScript
    5. Hilfe für Javascript Legastheniker und js v8.3.x

    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

    Hilfe für Javascript Legastheniker und js v8.3.x

    This topic has been deleted. Only users with topic management privileges can see it.
    • haus-automatisierung
      haus-automatisierung Developer Most Active @oFbEQnpoLKKl6mbY5e13 last edited by

      @ofbeqnpolkkl6mby5e13 MyMüll hab ich im Trashschedule Adapter doch schon integriert 🙃

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

        @ofbeqnpolkkl6mby5e13 sagte in Hilfe für Javascript Legastheniker und js v8.3.x:

        Hier mein Versuch

        du hast auf axios umgebaut?
        die Hinweise sind für httpGet

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

          @ofbeqnpolkkl6mby5e13 sagte in Hilfe für Javascript Legastheniker und js v8.3.x:

          var data = JSON.parse(body); // info ist ein Objekt

          und zumindest da ist noch ein body anstelle von response.data

          1 Reply Last reply Reply Quote 0
          • O
            oFbEQnpoLKKl6mbY5e13 @haus-automatisierung last edited by oFbEQnpoLKKl6mbY5e13

            @haus-automatisierung

            Könntest du bitte trotz Trashschedule-Adapter helfen, das Skript auf httpGet umzustellen?

            Mein aktueller Stand:

            const city_id = XXXXX;
            const area_id = XXX;
            const logging = false;
            const name = 'Müllabfuhr';
            const numberOfEntries = 69;
            
            const instanz = '0_userdata.' + instance + '.muell-test';
            
            if (logging) log('starting muell.' + instanz);
            
            extendObject(instanz, {
                   type: "device",
                   common: {
                       name: name
                   }
               }, function (err) {
                   if (err) {
                       log('could not create device: ', 'warn');
                       return;
                   }
               
               }
            );
            let baseData = {};
            
            // query names first
            httpGet('https://mymuell.jumomind.com/mmapp/api.php?r=trash&city_id=' + city_id + '&area_id=' + area_id, { timeout: 2000 }, (error, response) => {
               if (!error && response.data == 200) {
                   console.log(response.statusCode);
                   console.log(response.data);
               } else {
                   console.error(error);
               var data = JSON.parse(response.data);  // info ist ein Objekt
                       data.forEach((v, i) => {
                           baseData[v._name] = v;
                       })
               updateMuell();	
               }
            });
            
            schedule('{"time":{"exactTime":true,"start":"12:17"},"period":{"days":1}}', updateMuell);
            
            function updateMuell() {
               var options = 'https://mymuell.jumomind.com/webservice.php?idx=termins&city_id=' + city_id + '&area_id=' + area_id + '&ws=3'
            
               httpGet(options), (error, response)
                   if (!error && response.data == 200) 
                       var info = JSON.parse(response.data);  // info ist ein Objekt
                       if (info[0].Ack === 'Success') {
                           var data = info[0]._data;  // xy ist eine Eigenschaft des Objektes info
                           let counter = 0;
                           const date = (new Date())
                           const todayStr = date.getFullYear() + "-" + ('00' + (date.getMonth() + 1 )).slice(-2) + "-" + ('00' + date.getDate()).slice(-2)
                           log(todayStr);
                           data.forEach((v, i) => {
                               if (todayStr < v.cal_date && counter <= numberOfEntries) {
                                   
                                   const basePath = instanz + '.' + ('000' + counter).slice(-3);
                                   log(v.cal_date + ' -> ' + basePath, 'debug');
                                   // States erstellen
                                   extendObject(basePath, {
                                           type: "channel",
                                           common: {
                                               name: v.cal_date_normal
                                           }
                                       }, function (err) {
                                           if (err) {
                                               log('could not create device: ', 'warn');
                                               return;
                                           }
                                       
                                       }
                                   );
                                   createState(basePath + '.date', v.cal_date_normal, {
                                       name: 'Datum',
                                       desc: 'Datum der Abholung',
                                       type: 'string',
                                       read: true,
                                       write: false
                                   }, () => {
                                       setState(basePath + '.date', v.cal_date_normal, true);
                                   });
                                   
                                   createState(basePath + '.desc', baseData[v.cal_garbage_type].title, {
                                       name: 'Beschreibung',
                                       desc: 'Beschreibung der Abholung',
                                       type: 'string',
                                       read: true,
                                       write: false
                                   }, () => {
                                       setState(basePath + '.desc', baseData[v.cal_garbage_type].title, true);
                                   });
                                   createState(basePath + '.color', {
                                       name: 'Farbe',
                                       desc: 'Farbe',
                                       type: 'string',
                                       role: 'level.color',
                                       read: true,
                                       write: false
                                   }, () => {
                                       setState(basePath + '.color', baseData[v.cal_garbage_type].color, true);
                                   });
            
                                   counter++;
                               }
                           });
                       }
                   };
               
            

            2024-05-26 18:33:57.582  - info: javascript.0 (3416) Start JavaScript script.js.common._Tests.Test_17 (JavaScript/js)
            2024-05-26 18:33:57.614  - info: javascript.0 (3416) script.js.common._Tests.Test_17: registered 0 subscriptions, 1 schedule, 0 messages, 0 logs and 0 file subscriptions
            2024-05-26 18:33:57.705  - error: javascript.0 (3416) script.js.common._Tests.Test_17: null
            2024-05-26 18:33:57.707  - error: javascript.0 (3416) Error in callback: TypeError: Cannot read properties of undefined (reading 'headers')
            2024-05-26 18:33:57.708  - error: javascript.0 (3416)     at getHttpRequestConfig (/opt/iobroker/node_modules/iobroker.javascript/lib/tools.js:139:18)
            2024-05-26 18:33:57.708  - error: javascript.0 (3416)     at httpGet (/opt/iobroker/node_modules/iobroker.javascript/lib/sandbox.js:1179:20)
            2024-05-26 18:33:57.708  - error: javascript.0 (3416)     at updateMuell (script.js.common._Tests.Test_17:47:5)
            2024-05-26 18:33:57.709  - error: javascript.0 (3416)     at Object.<anonymous> (script.js.common._Tests.Test_17:38:5)
            2024-05-26 18:33:57.709  - error: javascript.0 (3416)     at /opt/iobroker/node_modules/iobroker.javascript/lib/sandbox.js:1189:38
            2024-05-26 18:33:57.709  - error: javascript.0 (3416)     at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
            2024-05-26 18:33:59.284  - info: javascript.0 (3416) Stopping script script.js.common._Tests.Test_17
            
            T Homoran 2 Replies Last reply Reply Quote 0
            • T
              TT-Tom @oFbEQnpoLKKl6mbY5e13 last edited by

              @ofbeqnpolkkl6mby5e13

              response.data sind deine Daten die zurückkommen, die wirst du nicht auf den Status prüfen wollen.
              Da müsste also response.statusCode stehen.

              if (!error && response.data == 200)
              
              O Homoran 2 Replies Last reply Reply Quote 0
              • O
                oFbEQnpoLKKl6mbY5e13 @TT-Tom last edited by oFbEQnpoLKKl6mbY5e13

                @tt-tom

                Okay, danke, das klingt plausibel. Habe ich angepasst.

                Edit:
                Damit ist der Fehler weg, aber die Objektstruktur wird nicht angelegt. Nur der oberste Ordner Muell-Test.
                Auf der Konsole werden Infos ausgegeben, die das alte Skript nicht ausgibt:

                2024-05-26 20:13:44.259  - info: javascript.0 (3416) Start JavaScript script.js.common._Tests.Test_17 (JavaScript/js)
                2024-05-26 20:13:44.302  - info: javascript.0 (3416) script.js.common._Tests.Test_17: registered 0 subscriptions, 1 schedule, 0 messages, 0 logs and 0 file subscriptions
                2024-05-26 20:13:44.397  - info: javascript.0 (3416) script.js.common._Tests.Test_17: 200
                2024-05-26 20:13:44.398  - info: javascript.0 (3416) script.js.common._Tests.Test_17: [{"title":"Bioabfall","name":"SZ_BIO","_name":"SZ_BIO","color":"a52a2a"},{"title":"Gelber Sack","name":"SZ_G","_name":"SZ_G","color":"FFFF00"},{"title":"Altpapier (120l, 240l)","name":"SZ_P","_name":"SZ_P","color":"2fc2e8"},{"title":"Restabfall (bis 240l)","name":"SZ_R","_name":"SZ_R","color":"2F4F4F"},{"title":"Weihnachtsbaum","name":"SZ_W","_name":"SZ_W","color":"36b515"}]
                2024-05-26 20:14:15.701  - info: javascript.0 (3416) Stopping script script.js.common._Tests.Test_17
                
                T 1 Reply Last reply Reply Quote 0
                • Homoran
                  Homoran Global Moderator Administrators @TT-Tom last edited by Homoran

                  @tt-tom sorry dass ich hier nochmal reingrätsche.
                  Ich hab ein altes wiffi-script auch noch umbauen wollten und natürlich nicht hinbekommen. Größenwahnsinnig wie ich durch die ersten Erfolge geworden bin hab ich natürlich duesmal keine Kopie erstellt.

                  Also den Rat von @haus-automatisierung beherzigt und mal eben versucht das ganze in Blockly nachzubauen.
                  nach gefühlten Stunden kopieren und konfigurieren von Blöcken hatte ich die Hauptfunktion laufen.

                  Screenshot_20240526-191624_Firefox.jpg

                  Jetzt kommt immer wieder timeout überschritten, cannot get vars of null

                  wie fange ich error im Blockly ab?
                  if !error....

                  1 Reply Last reply Reply Quote 0
                  • T
                    TT-Tom last edited by

                    @homoran
                    Bin leider nur am Handy unterwegs, kann aber nachher nochmal nachschauen. Aber auf error prüfen ist schon mal die richtige Richtung. Wie sieht das Blockly denn in JS aus.

                    Homoran 1 Reply Last reply Reply Quote 0
                    • T
                      TT-Tom @oFbEQnpoLKKl6mbY5e13 last edited by TT-Tom

                      @ofbeqnpolkkl6mby5e13

                      Hast du auch beide Zeilen angepasst? Der Fehler kam zweimal vor. Zeile 47 glaube ich

                      O 1 Reply Last reply Reply Quote 0
                      • Homoran
                        Homoran Global Moderator Administrators @TT-Tom last edited by Homoran

                        @tt-tom sagte in Hilfe für Javascript Legastheniker und js v8.3.x:

                        kann aber nachher nochmal nachschauen

                        danke schonmal. hab jetzt als erste Hilfe den Timeout hochgesetzt.

                        @tt-tom sagte in Hilfe für Javascript Legastheniker und js v8.3.x:

                        Wie sieht das Blockly denn in JS aus.

                        ist nur ein gaaanz kleiner Teil des Originals

                        schedule("*/4 * * * *", async () => {
                          httpGet('http://192.168.138.62/?json:', { timeout: 4000, responseType: 'text' }, async (err, response) => {
                            if (err) {
                              console.error(err);
                            }
                            setState('javascript.1.WIFFI.Systeminfo.ip_Homematic_CCU' /* ip_Homematic_CCU */, getAttr(response.data, 'vars.0.value'), true);
                            setState('javascript.1.WIFFI.wz_co2' /* Luftqualitaet */, getAttr(response.data, 'vars.1.value'), true);
                            setState('javascript.1.WIFFI.wz_temp' /* Temperatur */, getAttr(response.data, 'vars.2.value'), true);
                            setState('javascript.1.WIFFI.wz_feuchte' /* Luftfeuchte */, getAttr(response.data, 'vars.3.value'), true);
                            setState('javascript.1.WIFFI.wz_taupunkt' /* Taupunkt */, getAttr(response.data, 'vars.4.value'), true);
                            setState('javascript.1.WIFFI.wz_feuchte_abs' /* Absolutfeuchte */, getAttr(response.data, 'vars.5.value'), true);
                            setState('javascript.1.WIFFI.wz_baro' /* Luftdruck */, getAttr(response.data, 'vars.6.value'), true);
                            setState('javascript.1.WIFFI.wz_luftdrucktrend' /* Luftdrucktrend */, getAttr(response.data, 'vars.7.value'), true);
                            setState('javascript.1.WIFFI.wz_lux' /* Helligkeit */, getAttr(response.data, 'vars.8.value'), true);
                            setState('javascript.1.WIFFI.wz_motion_left' /* Bewegung links */, getAttr(response.data, 'vars.9.value'), true);
                            setState('javascript.1.WIFFI.wz_motion_right' /* Bewegung rechts */, getAttr(response.data, 'vars.10.value'), true);
                            setState('javascript.1.WIFFI.wz_noise' /* Geraeusch */, getAttr(response.data, 'vars.12.value'), true);
                            setState('javascript.1.WIFFI.wz_elevation' /* Sonne-Elevation */, getAttr(response.data, 'vars.13.value'), true);
                            setState('javascript.1.WIFFI.wz_azimut' /* Sonne-Azimut */, getAttr(response.data, 'vars.14.value'), true);
                            setState('javascript.1.WIFFI.wz_buzzer' /* Buzzer */, getAttr(response.data, 'vars.15.value'), true);
                          });
                        });
                        

                        ooh.
                        sehe gerade das ist ja "hard-coded" if (err) {console.error(err);}
                        das kann man dann wohl nicht unterbinden

                        T 1 Reply Last reply Reply Quote 0
                        • T
                          TT-Tom @Homoran last edited by

                          @homoran

                          Das ist so richtig, damit du auch eine log-Ausgabe bekommst. Das if-Statement bedeutet soviel wie: Wenn ein Fehler vorliegt, dann schreibe ins Log die Fehlermeldung.

                          Mann könnte jetzt das ganze so um stellen, dass bei einem Fehler keine Auswertung gemacht wird.

                          Homoran 1 Reply Last reply Reply Quote 1
                          • Homoran
                            Homoran Global Moderator Administrators @TT-Tom last edited by

                            @tt-tom sagte in Hilfe für Javascript Legastheniker und js v8.3.x:

                            dass bei einem Fehler keine Auswertung gemacht wird.

                            aber das log wird trotzdem zugemüllt.
                            Allerdings dann (hoffentlich) nur noch mit timeout of x msec exceeded und nicht mehr die ganzen null Meldungen

                            T paul53 2 Replies Last reply Reply Quote 0
                            • O
                              oFbEQnpoLKKl6mbY5e13 @TT-Tom last edited by

                              @tt-tom

                              Inzwischen ja. Leider wird weiterhin keine Objektstruktur angelegt. Hier der aktuelle Stand:

                              const city_id = XXXXX;
                              const area_id = XXX;
                              const logging = false;
                              const name = 'Müllabfuhr';
                              const numberOfEntries = 69;
                              
                              const instanz = '0_userdata.' + instance + '.muell-test';
                              
                              if (logging) log('starting muell.' + instanz);
                              
                              extendObject(instanz, {
                                     type: "device",
                                     common: {
                                         name: name
                                     }
                                 }, function (err) {
                                     if (err) {
                                         log('could not create device: ', 'warn');
                                         return;
                                     }
                                 
                                 }
                              );
                              let baseData = {};
                              
                              // query names first
                              httpGet('https://mymuell.jumomind.com/mmapp/api.php?r=trash&city_id=' + city_id + '&area_id=' + area_id, { timeout: 2000 }, (error, response) => {
                                 if (!error && response.statusCode == 200) {
                                     console.log(response.statusCode);
                                     console.log(response.data);
                                 } else {
                                     console.error(error);
                                 var data = JSON.parse(response.data);  // info ist ein Objekt
                                         data.forEach((v, i) => {
                                             baseData[v._name] = v;
                                         })
                                 updateMuell();	
                                 }
                              });
                              
                              schedule('{"time":{"exactTime":true,"start":"12:17"},"period":{"days":1}}', updateMuell);
                              
                              function updateMuell() {
                                 var options = 'https://mymuell.jumomind.com/webservice.php?idx=termins&city_id=' + city_id + '&area_id=' + area_id + '&ws=3'
                              
                                 httpGet(options), (error, response)
                                     if (!error && response.statusCode == 200) 
                                         var info = JSON.parse(response.data);  // info ist ein Objekt
                                         if (info[0].Ack === 'Success') {
                                             var data = info[0]._data;  // xy ist eine Eigenschaft des Objektes info
                                             let counter = 0;
                                             const date = (new Date())
                                             const todayStr = date.getFullYear() + "-" + ('00' + (date.getMonth() + 1 )).slice(-2) + "-" + ('00' + date.getDate()).slice(-2)
                                             log(todayStr);
                                             data.forEach((v, i) => {
                                                 if (todayStr < v.cal_date && counter <= numberOfEntries) {
                                                     
                                                     const basePath = instanz + '.' + ('000' + counter).slice(-3);
                                                     log(v.cal_date + ' -> ' + basePath, 'debug');
                                                     // States erstellen
                                                     extendObject(basePath, {
                                                             type: "channel",
                                                             common: {
                                                                 name: v.cal_date_normal
                                                             }
                                                         }, function (err) {
                                                             if (err) {
                                                                 log('could not create device: ', 'warn');
                                                                 return;
                                                             }
                                                         
                                                         }
                                                     );
                                                     createState(basePath + '.date', v.cal_date_normal, {
                                                         name: 'Datum',
                                                         desc: 'Datum der Abholung',
                                                         type: 'string',
                                                         read: true,
                                                         write: false
                                                     }, () => {
                                                         setState(basePath + '.date', v.cal_date_normal, true);
                                                     });
                                                     
                                                     createState(basePath + '.desc', baseData[v.cal_garbage_type].title, {
                                                         name: 'Beschreibung',
                                                         desc: 'Beschreibung der Abholung',
                                                         type: 'string',
                                                         read: true,
                                                         write: false
                                                     }, () => {
                                                         setState(basePath + '.desc', baseData[v.cal_garbage_type].title, true);
                                                     });
                                                     createState(basePath + '.color', {
                                                         name: 'Farbe',
                                                         desc: 'Farbe',
                                                         type: 'string',
                                                         role: 'level.color',
                                                         read: true,
                                                         write: false
                                                     }, () => {
                                                         setState(basePath + '.color', baseData[v.cal_garbage_type].color, true);
                                                     });
                              
                                                     counter++;
                                                 }
                                             });
                                         }
                                     };
                                 
                              

                              T Homoran 2 Replies Last reply Reply Quote 0
                              • T
                                TT-Tom @Homoran last edited by

                                @homoran
                                Ja davon gehe aus. Muss mir das aber zu Hause erst anschauen.
                                Wobei der Timeout eventuell auch höher muss, wenn die Daten nicht so schnell da sind.

                                1 Reply Last reply Reply Quote 1
                                • T
                                  TT-Tom @oFbEQnpoLKKl6mbY5e13 last edited by

                                  @ofbeqnpolkkl6mby5e13
                                  Wie gesagt bin noch am Handy und schaue nachher mir das dann an. Das JSON kommt aber jetzt richtig rein?

                                  Setze das mal in Zeile 48 mit rein, damit du sehen kannst was da kommt.

                                  console.log(response.data);
                                  
                                  Homoran O 2 Replies Last reply Reply Quote 0
                                  • Homoran
                                    Homoran Global Moderator Administrators @TT-Tom last edited by

                                    @tt-tom sagte in Hilfe für Javascript Legastheniker und js v8.3.x:

                                    Setze das mal in Zeile 48 mit rein,

                                    steht schon in Zeile 30

                                    O 1 Reply Last reply Reply Quote 0
                                    • O
                                      oFbEQnpoLKKl6mbY5e13 @TT-Tom last edited by

                                      @tt-tom

                                      Ja, die werden bei jedem Skriptstart im Log ausgegeben, was aber vorher nie so war. Die Objektstruktur wird aber nicht angelegt (nur der Ordner 0_userdata.0.muell-test).

                                      1 Reply Last reply Reply Quote 0
                                      • O
                                        oFbEQnpoLKKl6mbY5e13 @Homoran last edited by oFbEQnpoLKKl6mbY5e13

                                        @homoran

                                        Richtig, aber eigentllich sollte das doch nur bei der Bedingung "if (!error && response.statusCode == 200)" ausgegeben werden.

                                        Edit:
                                        Ach, ich sehe gerade response.statusCode ist auch 200.

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

                                          @ofbeqnpolkkl6mby5e13 sagte in Hilfe für Javascript Legastheniker und js v8.3.x:

                                          if (info[0].Ack === 'Success') {

                                          ich könnte mit meinem begrenzten Wissen höchstens vorstellen dass dies nicht true ist und den Rest unterbindet

                                          O 1 Reply Last reply Reply Quote 0
                                          • O
                                            oFbEQnpoLKKl6mbY5e13 @Homoran last edited by oFbEQnpoLKKl6mbY5e13

                                            @homoran

                                            Schätze ich auch. Allerdings, wenn ich auf "if (!error && response.statusCode == 400)" ändere, dann kommt wieder:

                                            2024-05-26 21:18:41.065  - info: javascript.0 (3416) Start JavaScript script.js.common._Tests.Test_17 (JavaScript/js)
                                            2024-05-26 21:18:41.115  - info: javascript.0 (3416) script.js.common._Tests.Test_17: registered 0 subscriptions, 1 schedule, 0 messages, 0 logs and 0 file subscriptions
                                            2024-05-26 21:18:41.294  - error: javascript.0 (3416) script.js.common._Tests.Test_17: null
                                            2024-05-26 21:18:41.296  - error: javascript.0 (3416) Error in callback: TypeError: Cannot read properties of undefined (reading 'headers')
                                            2024-05-26 21:18:41.297  - error: javascript.0 (3416)     at getHttpRequestConfig (/opt/iobroker/node_modules/iobroker.javascript/lib/tools.js:139:18)
                                            2024-05-26 21:18:41.297  - error: javascript.0 (3416)     at httpGet (/opt/iobroker/node_modules/iobroker.javascript/lib/sandbox.js:1179:20)
                                            2024-05-26 21:18:41.298  - error: javascript.0 (3416)     at updateMuell (script.js.common._Tests.Test_17:47:5)
                                            2024-05-26 21:18:41.298  - error: javascript.0 (3416)     at Object.<anonymous> (script.js.common._Tests.Test_17:38:5)
                                            2024-05-26 21:18:41.298  - error: javascript.0 (3416)     at /opt/iobroker/node_modules/iobroker.javascript/lib/sandbox.js:1189:38
                                            2024-05-26 21:18:41.299  - error: javascript.0 (3416)     at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
                                            2024-05-26 21:18:48.201  - info: javascript.0 (3416) Stopping script script.js.common._Tests.Test_17
                                            

                                            Edit:
                                            Ich kann mir inzwischen nicht mehr vorstellen, dass man eine Anleitung erstellen kann, die einem User bei der Umstellung auf httpGet hilft.

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

                                            Support us

                                            ioBroker
                                            Community Adapters
                                            Donate

                                            549
                                            Online

                                            31.9k
                                            Users

                                            80.1k
                                            Topics

                                            1.3m
                                            Posts

                                            8
                                            104
                                            6463
                                            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