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.

    read csv-data and put it in an array

    Scheduled Pinned Locked Moved Utilities
    313 Posts 3 Posters 495.6k Views 4 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.
    • PerlchampP Offline
      Perlchamp
      last edited by

      all values “undefined”

      S 1 Reply Last reply Reply Quote 0
      • S Offline
        sdetweil @Perlchamp
        last edited by

        @Perlchamp this.active_brithdays undefined?

        did u fix the names in the notification handler?

        		if(notification === "have data"){
        
        			var self = this
        			Log.log(this.name + " received a socket notification: " + notification + " - Payload: " + payload);
        			
        			var now = moment();
        			
        
        			for(var birthday of payload) {
        				// get 1st 5 chars of birthdate, thru month
        				// we will use this as the key in the hash
        				var birth_date = birthday.birth.substring(0,5);
        				
        				// get the birthday as a moment in this year, for comparing
        				var birth_date_moment = moment(birth_date + now.year(),"DD.MMYYYY");
        
        				// u can add days to a moment object and then compare 
        				//     if the birthdate is before that date (and after now)
        				//     so within the next xx days
        
        				// if the date is the same or later, don't use time of day
        				if(birth_date_moment.startOf('day').isSame(now.startOf('day'))) {
        
        					// birthday is in this month
        					// check the hash if we've seen anything for today yet
        					// if we haven't see this date yet
        					if(this.active_birthdays[birth_date] == undefined){
                   
        						//  create the holder for its info (array of 
        						//		names) in the hash
        						this.active_birthdays[birth_date] = [];
        					}
        					// save the persons name on the list
        					this.active_birthdays[birth_date].push(birthday.name);
        				}
        			}
             
        			// tell MM to call and get our content
        			self.updateDom();
        		}     			
        

        Sam

        How to add modules

        learning how to use browser developers window for css changes

        1 Reply Last reply Reply Quote 0
        • PerlchampP Offline
          Perlchamp
          last edited by

          picture

          S 1 Reply Last reply Reply Quote 0
          • S Offline
            sdetweil @Perlchamp
            last edited by

            @Perlchamp click on line number 122 for a stop… birthday is not defined once you leave the for loop…

            Sam

            How to add modules

            learning how to use browser developers window for css changes

            1 Reply Last reply Reply Quote 0
            • PerlchampP Offline
              Perlchamp
              last edited by

              yes i did. all variables are correct

              S 1 Reply Last reply Reply Quote 0
              • S Offline
                sdetweil @Perlchamp
                last edited by

                @Perlchamp and getDom() will be called multiple times, once before your data arrives, maybe once more than when you data is ready

                and it does the innerText statement?

                and u look at the element, and look at the inner text and its correct?

                i am running out of energy for the day… been since 6am. now 11pm

                Sam

                How to add modules

                learning how to use browser developers window for css changes

                1 Reply Last reply Reply Quote 0
                • PerlchampP Offline
                  Perlchamp
                  last edited by

                  122 => next => marks “return”(131) => next => marks end of 131 => next => jumped to main.js

                  S 1 Reply Last reply Reply Quote 0
                  • S Offline
                    sdetweil @Perlchamp
                    last edited by

                    @Perlchamp mark the line for the second for(

                    for(var birthday of Object.keys(this.active_birthdays)){
                    			for(var name of this.active_birthdays[birthday]){              // stop on this line
                    

                    and remove the other stops

                    Sam

                    How to add modules

                    learning how to use browser developers window for css changes

                    1 Reply Last reply Reply Quote 0
                    • PerlchampP Offline
                      Perlchamp
                      last edited by

                      the same see picture i did send to you. it’s line 122 => stop => refresh => and than like posted before
                      no does nothing … ok never mind. coming from germany it’s now 06:00 in the morning did’t sleep …

                      S 1 Reply Last reply Reply Quote 0
                      • S Offline
                        sdetweil @Perlchamp
                        last edited by

                        @Perlchamp tomorrow, very close

                        Sam

                        How to add modules

                        learning how to use browser developers window for css changes

                        1 Reply Last reply Reply Quote 0
                        • PerlchampP Offline
                          Perlchamp
                          last edited by

                          don’t worry about me and sleep well.

                          S 1 Reply Last reply Reply Quote 0
                          • S Offline
                            sdetweil @Perlchamp
                            last edited by

                            @Perlchamp thx…

                            in debugging

                            put a stop on the self.updateDom() in the notiification code.
                            no place else

                            hit refresh

                            when it stops there,
                            scroll down to getdom and add a stop on the 1st for loop line

                            hit run arrow,
                            should stop in getdom now, with data

                            Sam

                            How to add modules

                            learning how to use browser developers window for css changes

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

                              @perl here is the updated perlchamp.js, shows date, persons name and age (no formatting)

                              Module.register("perlchamp", {
                              
                              	defaults: {
                              		language: "de",
                              		dimmEntries: true,   // true: dims entries and the associated 
                              							//       symbol when the date has expired.
                              							// false: delete entries and the associated 
                              							//        symbol when the date has expired.
                                               // used if look forward is desired, add to module config  in config.js
                                               days_forward:0,
                              	},
                              	// place to save birthdays to display
                                  active_birthdays : {},
                              
                              
                              	init: function(){
                              		Log.log(this.name + " is in init!");
                              	},
                              
                              	start: function(){
                              		Log.log("Starting module: " + this.name);
                              
                              		// Set locale
                              		moment.locale(config.language);
                              	},
                              
                              	loaded: function(callback) {
                              		Log.log(this.name + " is loaded!");
                              		callback();
                              	},
                              
                              	// return list of other functional scripts to use, if any (like require in node_helper)
                              	getScripts: function() {
                              		return	["moment.js"];
                              	}, 
                              
                              	// return list of stylesheet files to use if any
                              	getStyles: function() {
                              		return 	[this.data.path + "/css/perlchamp.css"];
                              	},
                              
                              /*
                              	// return list of translation files to use, if any
                              	getTranslations: function() {
                              		return {
                              			// sample of list of files to specify here, if no files, do not use this routine, , or return empty list
                              
                              			// en: "translations/en.json",  (folders and filenames in your module folder)
                              			// de: "translations/de.json"
                              		}
                              	},
                              */
                              
                              
                              	// only called if the module header was configured in module config in config.js
                              	getHeader: function() {
                              		return this.data.header;
                              	},
                              
                              	// messages received from other modules and the system (NOT from your node helper)
                              	// payload is a notification dependent data structure
                              	notificationReceived: function(notification, payload, sender) {
                              		// once everybody is loaded up
                              		if(notification==="ALL_MODULES_STARTED"){
                              			// send our config to our node_helper
                              			this.sendSocketNotification("CONFIG",this.config);
                              		}
                              		if (sender) {
                              			Log.log(this.name + " received a module notification: " + notification + " from sender: " + sender.name);
                              		} else {
                              			Log.log(this.name + " received a system notification: " + notification);
                              		}
                              	},
                              
                              	
                              	// messages received from from your node helper (NOT other modules or the system)
                              	// payload is a notification dependent data structure, up to you to design between module and node_helper
                              	socketNotificationReceived: function(notification, payload) {
                              /*
                              		Log.log(this.name + " received a socket notification: " + notification + " - Payload: " + payload);
                              		if(notification === "message_from_helper"){
                              			this.config.message = payload;
                              			// tell mirror runtime that our data has changed,
                              			// we will be called back at GetDom() to provide the updated content
                              			this.updateDom(1000);
                              		}
                              */
                              		if(notification === "have data"){
                              
                              			var self = this
                              			Log.log(this.name + " received a socket notification: " + notification + " - Payload: " + payload);
                              			
                              			var now = moment();
                              
                              			// clear the old list
                                                      this.active_birthdays={}
                              
                              			// only calculate in advance once
                              			var birthday_time_window=moment().add(self.config.days_forward,'days')
                              
                              			// loop thru the birthdays  from the file
                              			for( var birthday of payload) {
                              				// get 1st 5 chars of birthdate, thru month
                              				// we will use this as the key in the hash
                              				var birth_date=birthday.birth.substring(0,5)
                              
                              				// get the birthday as a moment in this year, for comparing
                              				var birth_date_moment = moment(birth_date+now.year(),"DD.MMYYYY")
                              				  
                              				// u can add days to a moment object and then compare if the birthdate is before that date (and after now)
                              				// so within the next xx days
                              
                              			  // if the date is the same or later, don't use time of day
                              			   if(birth_date_moment.startOf('day').isSameOrAfter(now.startOf('day')) &&
                              			   	  birth_date_moment.startOf('day').isSameOrBefore(birthday_time_window.startOf('day'))
                              			   		){
                              
                              			      // birthday is in this month
                              			      // check the hash if we've seen anything for today yet
                              			      // if we haven't see this date yet
                              			      if(self.active_birthdays[birth_date] == undefined){
                              			           //  create the holder for its info (array of info) in the hash
                              			           self.active_birthdays[birth_date]=[]
                              			      }
                              			      // save the persons name and age on the list
                              			      self.active_birthdays[birth_date].push({'name':birthday.name, 'age': now.diff(moment(birthday.birth,'DD.MM.YYYY'), 'years') })			     
                              			   }
                              			}
                                   
                              			// tell MM to call and get our content
                              			self.updateDom();
                              		}     			
                              			
                              /*			
                              			this.config.message = payload;
                              			// tell mirror runtime that our data has changed,
                              			// we will be called back at GetDom() to provide the updated content
                              			this.updateDom(1000)
                              			
                              			// filters the birthdaylist and output only the birthdays
                              			// of the current month, sorted (ascending) by days in 
                              			// the following format: 
                              			// [{ day: "10", name: ["firstname lastname",...] }]
                              
                              			// set variables
                              			var today_month = moment().format("MM");
                              			var today_day_month = moment().format("DD.MM");
                              
                              			var result = Object.entries(jsonObj
                              			.filter(({birth}) => birth.split('.')[1] === today_month)
                              			.reduce((a, {birth, name}) => {
                              				const day = +birth.split('.')[0];
                              				a[day] = [...(a[day] || []), name];
                              				return a
                              			}, {})
                              			).map(([day, name]) => ({day, name})).sort((a, b) => +a.day - b.day);
                              			birthdaysArray = result;
                              			
                              			// for debugging only
                              			//console.log("filtered and sorted: ", birthdaysArray);
                              */
                              			
                              	},
                              
                              	// system notification your module is being hidden
                              	// typically you would stop doing UI updates (getDom/updateDom) if the module is hidden
                              	suspend: function(){
                              
                              	},
                              
                              	// system notification your module is being unhidden/shown
                              	// typically you would resume doing UI updates (getDom/updateDom) if the module is shown
                              	resume: function(){
                              
                              	},
                              
                              	// this is the major worker of the module, it provides the displayable content for this module
                              	getDom: function() {
                              		var wrapper = document.createElement("div");
                              
                              // create your table and header here
                              
                              		for(var birthday of Object.keys(this.active_birthdays)){
                              			for(var person of this.active_birthdays[birthday]){
                              				var m=document.createElement('div')
                              // create tyour table row here				
                              				m.innerText=birthday+ ' '+ person.name+' age='+person.age
                              				wrapper.appendChild(m)					
                              			}
                              		}
                              
                              // create your table footer here	
                              
                              		// pass the created content back to MM to add to DOM.
                              		return wrapper;
                              	}
                              })
                              

                              this is the birthdays.csv I was using
                              the column names are the field names in the jsonobj

                              birth, name
                              12.07.1952, Sam
                              11.09.1944, Peg
                              02.05.1939, Frank 
                              09.04.1953, June
                              09.04.1953, Jane
                              30.12.1955, Joan
                              02.05.1939, Sue
                              05.05.1955, mary
                              

                              Sam

                              How to add modules

                              learning how to use browser developers window for css changes

                              1 Reply Last reply Reply Quote 0
                              • PerlchampP Offline
                                Perlchamp
                                last edited by

                                hi sam :-)
                                see my module-file. why i don’t get values for the Log.log(); statements ?

                                	// this is the major worker of the module, it provides the displayable content for this module
                                	getDom: function() {
                                		var self = this;
                                		var wrapper = document.createElement("div");
                                		Log.log("this.active_birthdays >>>>> ", this.active_birthdays);
                                
                                		for(var birthday of Object.keys(this.active_birthdays)) {
                                			for(var name of this.active_birthdays[birthday]) {
                                				var m = document.createElement("div");
                                				Log.log("BIRTHDAY >> " + birthday);
                                				Log.log("NAME >> " + name);
                                				m.innerText = birthday + " " + name;
                                				wrapper.appendChild(m);
                                			}
                                		}
                                
                                		// pass the created content back to MM to add to DOM.
                                		return wrapper;
                                	}
                                
                                S 1 Reply Last reply Reply Quote 0
                                • S Offline
                                  sdetweil @Perlchamp
                                  last edited by sdetweil

                                  @Perlchamp said in read csv-data and put it in an array:

                                    for(var birthday of Object.keys(this.active_birthdays)) {
                                    	for(var name of this.active_birthdays[birthday]) {
                                  

                                  that means one of those two for loops didn’t execute, as the list is empty

                                  do this

                                   			// tell MM to call and get our content
                                  Log.log(JSON.stringify(this.active_birthdays))    // add this line
                                  			self.updateDom();                          // before this line in the notification code..
                                  

                                  Sam

                                  How to add modules

                                  learning how to use browser developers window for css changes

                                  1 Reply Last reply Reply Quote 0
                                  • PerlchampP Offline
                                    Perlchamp
                                    last edited by

                                    and where i have to place it ?

                                    S 1 Reply Last reply Reply Quote 0
                                    • S Offline
                                      sdetweil @Perlchamp
                                      last edited by

                                      @Perlchamp see my updated post above

                                      Sam

                                      How to add modules

                                      learning how to use browser developers window for css changes

                                      1 Reply Last reply Reply Quote 0
                                      • PerlchampP Offline
                                        Perlchamp
                                        last edited by

                                        i get this for each entry (here i only posted one of them):

                                        [2020-05-02 17:50:36.803] [LOG]    LOOK >>>>>>{} (/home/dirk/MagicMirror/modules/MMM-Logging/node_helper.js:34 Class.socketNotificationReceived)
                                        

                                        empty: {}

                                        S 1 Reply Last reply Reply Quote 0
                                        • S Offline
                                          sdetweil @Perlchamp
                                          last edited by sdetweil

                                          @Perlchamp ok, so the data collection didn’t work…

                                          so I would step thru that code

                                          what is the 1st row of your csv file? the column headers

                                          Sam

                                          How to add modules

                                          learning how to use browser developers window for css changes

                                          1 Reply Last reply Reply Quote 0
                                          • PerlchampP Offline
                                            Perlchamp
                                            last edited by

                                            before you do this. stop. i make an error. the code you posted is not in the notification section. it’s in the getDom section … so i placed it there. was this wrong ?

                                            S 1 Reply Last reply Reply Quote 0

                                            Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                                            Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                                            With your input, this post could be even better 💗

                                            Register Login
                                            • 1
                                            • 2
                                            • 3
                                            • 4
                                            • 5
                                            • 6
                                            • 7
                                            • 8
                                            • 15
                                            • 16
                                            • 6 / 16
                                            • 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