MagicMirror Forum
    • Recent
    • Tags
    • Unsolved
    • Solved
    • MagicMirror² Repository
    • Documentation
    • 3rd-Party-Modules
    • Donate
    • Discord
    • Register
    • Login
    A New Chapter for MagicMirror: The Community Takes the Lead
    Read the statement by Michael Teeuw here.

    How to Change MMM-DWD-WarnWeather Module for Switzerland

    Scheduled Pinned Locked Moved Development
    13 Posts 3 Posters 7.8k Views 3 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • strawberry 3.141S Offline
      strawberry 3.141 Project Sponsor Module Developer @Squirrel
      last edited by

      @Squirrel the simplified structure of his json looks like this

      result = {
        "warnings": {
          "103353000" : [ //regionID
            {"regionName": "Kreis Harburg", ...},
            ...
          ],
          ...
        },
        ...
      }
      

      result is an object, in this object we have the property warnings which is also an object, nested in this we have the regionIds as properties which are of the type array. To acces the first entry in this array we do the following result['warnings'][regionId][0]. Every entry of this array is again an object which has a proerty regionName, which he’s comparing to the region defined in the config of the module.

      Please create a github issue if you need help, so I can keep track

      1 Reply Last reply Reply Quote 0
      • S Offline
        Squirrel
        last edited by Squirrel

        Ok i understand. But i have problems to transfer this to my module.

        the simplified structure of my json looks like this:

        result = {
            "alarms": [ {
                 "id":49664,
                 ...
                 "regions": [
                       {
                       "id":40,
                       "name_de":"Agglo BE",
                       "name_fr":"Agglo BE",
                       "name_it":"Agglo BE"
                        }
                  ],
                 ...
              },
              
         }
        

        Now i tried as a first test to display some stuff of my JSON.

        var regionId = 0; // Later i want to create a for-loop with this
        
        console.log (result['alarms'][regionId]['regions']);
        

        With this i got something like:

        "id":40,
         "name_de":"Agglo BE",
         "name_fr":"Agglo BE",
         "name_it":"Agglo BE"
        

        But then i tried

        console.log (result['alarms'][regionId]['regions']['name_de']);
        
        AND
        
        console.log (result['alarms'][regionId]['regions'][1]);
        

        But this gives me undefined as answer… I dont understand why…?
        Very strange is also that i no more see the regions when i delete the last bracket again… then i get [Object][Object]as Answer!? And when i Start again with only the [‘alarms’] Bracket i can see all the Warnings and then again also the regions with [‘alarms’][regionId][‘regions’]

        strawberry 3.141S 1 Reply Last reply Reply Quote 0
        • strawberry 3.141S Offline
          strawberry 3.141 Project Sponsor Module Developer @Squirrel
          last edited by

          @Squirrel result['alarms'][0]['regions'][0]['name_de'] returns Agglo BE you have to access arrays [] with the index (starting with 0) and access objects {} with the property names

          Please create a github issue if you need help, so I can keep track

          1 Reply Last reply Reply Quote 0
          • S Offline
            Squirrel
            last edited by

            @strawberry-3-141 thank you for your fast and competent answer.

            This is what i did now:

            
            ......
            
            request ( {
                      url: url,
                      method: 'GET'
            		  }, 
            
                       function (error, response, body) {
            
            			var result = JSON.parse(body);
            			var warningData = [];
            			
            			for (var regionId in result['alarms']) {
            
                            			if (result['alarms'][regionId]['regions'][0]['name_de'] == region) {
            					var warnings = new Object();
            					warnings.start = result['alarms'][regionId]['valid_from'];
            					warningData.push(warnings.start);
            					warnings.end = result['alarms'][regionId]['valid_to'];
            					warningData.push(warnings.end);
            					warnings.regionName = result['alarms'][regionId]['regions'][0]['name_de'];
            					warningData.push(warnings.regionName);
            					warnings.level = result['alarms'][regionId]['priority']
            					warningData.push(warnings.level);
            					warnings.type = result['alarms'][regionId]['code']
            					warningData.push(warnings.type);
            					warnings.altitudeStart = undefined;
            					warningData.push(warnings.altitudeStart);
            					
            					if (warnings.type == 1){warnings.event = "Frost";}
            					else if (warnings.type == 2){warnings.event = "Gewitter";}
            					else if (warnings.type == 3){warnings.event = "Grossflächige Glätte";}
            					else if (warnings.type == 4){warnings.event = "Kräftiger Regen";}
            					else if (warnings.type == 5){warnings.event = "Schnee";}
            					else if (warnings.type == 6){warnings.event = "Sturm";}
            					else if (warnings.type == 7){warnings.event = "Hochwasser";}
            
            					warningData.push(warnings.event);
            
            					warnings.headline = result['alarms'][regionId]['de']['title'];
            					warningData.push(warnings.headline);
            					warnings.description = result['alarms'][regionId]['de']['paragraph'];
            					warningData.push(warnings.description);
            				}
            			}
            			self.sendSocketNotification('WARNINGS_DATA', {warnings: warningData, region: region});
            
            			console.log(warnings);
            			console.log(warningData)
            
            		});
            
                      .......
            
            

            Now i got the following outputs:

            // With console.log(warnings);
            
            { start: '2017-01-16T21:00:00.000Z',
              end: '2017-01-19T00:00:00.000Z',
              regionName: 'Seeland/Bielersee',
              level: 1,
              type: 6,
              altitudeStart: undefined,
              event: 'Sturm',
              headline: 'Starke Windböen',
              description: 'eisige Bise. 21 Uhr bis Do 00 Uhr' }
            
            // With console.log (warningData)
            
            [ '2017-01-17T00:00:00.000Z',
              '2017-01-18T23:00:00.000Z',
              'Seeland/Bielersee',
              1,
              3,
              undefined,
              'Grossflächige Glätte',
              'Gefahr von grossflächiger Glätte',
              'durch Schneeglätte. Di 00 Uhr bis Mi 23 Uhr',
              '2017-01-16T21:00:00.000Z',
              '2017-01-19T00:00:00.000Z',
              'Seeland/Bielersee',
              1,
              6,
              undefined,
              'Sturm',
              'Starke Windböen',
              'eisige Bise. 21 Uhr bis Do 00 Uhr' ]
            

            My Problem now is that my Output is still looking different than his output. And when i get 2 warnings for the same Region the Output goes into the same Array… By the Way… Why i get only one Warning for the region with console.log(warnings);

            Here is the Output example of his module:

            [ { start: 1484586000000,
                end: 1484650800000,
                regionName: 'Kreis Harburg',
                .....
                state: 'Niedersachsen' 
              },
              { start: 1484586000000,
                end: 1484650800000,
                regionName: 'Kreis Harburg',
                .......
                state: 'Niedersachsen' 
                }
             ]
            
            strawberry 3.141S 1 Reply Last reply Reply Quote 0
            • strawberry 3.141S Offline
              strawberry 3.141 Project Sponsor Module Developer @Squirrel
              last edited by

              @Squirrel you are pushing every single propertie to the array instead of the whole object

              if (result['alarms'][regionId]['regions'][0]['name_de'] == region) {
                var warnings = {};
                warnings.start = result['alarms'][regionId]['valid_from'];
                warnings.end = result['alarms'][regionId]['valid_to'];
                warnings.regionName = result['alarms'][regionId]['regions'][0]['name_de'];
                warnings.level = result['alarms'][regionId]['priority']
                warnings.type = result['alarms'][regionId]['code']
                warnings.altitudeStart = undefined;
              					
                if (warnings.type == 1){warnings.event = "Frost";}
                else if (warnings.type == 2){warnings.event = "Gewitter";}
                else if (warnings.type == 3){warnings.event = "Grossflächige Glätte";}
                else if (warnings.type == 4){warnings.event = "Kräftiger Regen";}
                else if (warnings.type == 5){warnings.event = "Schnee";}
                else if (warnings.type == 6){warnings.event = "Sturm";}
                else if (warnings.type == 7){warnings.event = "Hochwasser";}
              
                warnings.headline = result['alarms'][regionId]['de']['title'];
                warnings.description = result['alarms'][regionId]['de']['paragraph'];
                warningData.push(warnings);
              }
              

              try this instead

              Please create a github issue if you need help, so I can keep track

              1 Reply Last reply Reply Quote 0
              • S Offline
                Squirrel
                last edited by

                Thanks again for fast Answer! Now everything is working. Im so happy! :)

                I have just one last Question. I tried to run both modules at the same time. This is giving me problems. The Icons are too much big and i cant see something else…

                I changed all the names of the module files and folders and i have no idea where i can find this fault. I think nobody gonna us this two modules at the same time but i want to upload my module for other users and if somebody is using both of them i dont want to destroy @LukeCodewalker s module… ;)

                1 Reply Last reply Reply Quote 0
                • S Offline
                  Squirrel
                  last edited by

                  I uploaded my Project MMM-WetteralarmCH now! If somebody can help me with the problem of using both modules (MMM-WetteralarmCH and. MMM-DWD-WarnWeather) at the same time i gonna update my Project again.

                  Thanks to everybody for helping me!

                  1 Reply Last reply Reply Quote 0
                  • LukeCodewalkerL Offline
                    LukeCodewalker Module Developer
                    last edited by

                    Sorry for being absent so long. Busy times in the moment :) but I’m glad to see that you’ve got it working. :thumbsup:

                    1 Reply Last reply Reply Quote 0
                    • S Offline
                      Squirrel
                      last edited by

                      @LukeCodewalker thanks! But I still need a Solution to don’t Crash your Module while both modules are running at the same time… Let me know when you have more time to talk about that topic 😉

                      1 Reply Last reply Reply Quote 0
                      • 1
                      • 2
                      • 1 / 2
                      • First post
                        Last post
                      Enjoying MagicMirror? Please consider a donation!
                      MagicMirror created by Michael Teeuw.
                      Forum managed by Sam, technical setup by Karsten.
                      This forum is using NodeBB as its core | Contributors
                      Contact | Privacy Policy