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.

    Config option with array of multiple values?

    Scheduled Pinned Locked Moved Development
    25 Posts 3 Posters 8.0k 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.
    • UncleRogerU Offline
      UncleRoger
      last edited by

      I’m working on a module and it mostly works. I’d like to expand its functionality a bit though and to do that I’m thinking about changing an array I have into an array of multiple values. That is, I currently have defined and am using:

      validSenders: [ "mom@example.com",
                      "dad@example.com",
                      "son@example.com",
                    ]
      

      My code uses it thusly:

      if (!that.config.validSenders || that.config.validSenders.includes(mailObj.sender[0].address)) {
      
      

      What I’d like to do is change that to look like this:

      validSenders: [ { "mom@example.com", "Mom", "#ff0000" },
                      { "dad@example.com", "Dad", "#00ff00" },
                      { "son@example.com", "Son", "#0000ff" },
                    ]
      

      but I don’t know a) if that’s right, b) how I would do the check in the if statement above, and c) how I would access the other parts of each element. Heck, I don’t even know if that syntax is right. (I know nothing of Javascript.)

      Any help with this would be much appreciated. It’s totally unnecessary but I think it could enhance the usability of the module for others.

      And if I might digress for a moment, I have this code in my module:

      if (that.config.validSenders.includes(mailObj.sender[0].address)) {
         if (daysAgo >= 0 && daysAgo <= that.config.daysToDisplay) {
      

      which works fine.

      But if I combine those two IF statements, it doesn’t.

      if (that.config.validSenders.includes(mailObj.sender[0].address) && \
          daysAgo >= 0 && daysAgo <= that.config.daysToDisplay) {
      

      It doesn’t work even if I remember to remove the second closing brace at the end.

      Thanks!

      S 2 Replies Last reply Reply Quote 0
      • S Do not disturb
        sdetweil @UncleRoger
        last edited by sdetweil

        @UncleRoger said in Config option with array of multiple values?:

        great conversation topic

        validSenders: [ { "mom@example.com", "Mom", "#ff0000" },
                        { "dad@example.com", "Dad", "#00ff00" },
                        { "son@example.com", "Son", "#0000ff" },
                      ]
        

        generally when you expand the number of items in an array to objects ({}) you start to get thinking about what happens if you decide to add another entry in the object…

        so in javascript you can NAME the elements

        validSenders: [ 
                        {  name:"Mom", color:"#ff0000",url:"mom@example.com", },
                        { url"dad@example.com", name:"Dad", color:"#00ff00" },
                        {  name:"Son", url:"son@example.com",color:"#0000ff" },
                      ]
        

        that way the code is not sensitive to the order of the elements

             validSenders.forEach(sender =>{
                     if (sender.name =="Dad") {
                          do_something(sender.url)
                     }
             })
        

        you can also use the array.filter() function

        let selected_sender = validSenders.filter(sender=>{
              if(sender.url==mailObj.sender[0].address)
                   return true
             else
                   return false
         })
        if(selected_sender.length>0){
            // we found a matching sender
        }
        

        the filter function passes each element array in turn to the function
        if you want the element in the output array return true,
        if not return false

        on the combined statements you don’t need the backslash

        if (that.config.validSenders.includes(mailObj.sender[0].address) && 
            daysAgo >= 0 && daysAgo <= that.config.daysToDisplay) {
        

        but this is THREE comparisons

        • that.config.validSenders.includes(mailObj.sender[0].address)
        • daysAgo >= 0
        • daysAgo <= that.config.daysToDisplay

        the last two CANNOT be true at the same time

        maybe what you wanted was

        if (
             that.config.validSenders.includes(mailObj.sender[0].address) && 
            (daysAgo >= 0 && daysAgo <= that.config.daysToDisplay)  
             ) {
        

        this is two outer compares (with one inner)

        Sam

        How to add modules

        learning how to use browser developers window for css changes

        UncleRogerU 2 Replies Last reply Reply Quote 2
        • S Do not disturb
          sdetweil @UncleRoger
          last edited by

          @UncleRoger note that I just edited the new validSenders array to move the element properties around… having them named insulates the code from any positioning issues…(and YOU don’t have to add any code to handle that)

          Sam

          How to add modules

          learning how to use browser developers window for css changes

          1 Reply Last reply Reply Quote 1
          • UncleRogerU Offline
            UncleRoger @sdetweil
            last edited by

            @sdetweil said in Config option with array of multiple values?:

            generally when you expand the number of items in an array to objects ({}) you start to get thinking about what happens if you decide to add another entry in the object…

            so in javascript you can NAME the elements

            validSenders: [ 
                            { url:"mom@example.com", name:"Mom", color:"#ff0000" },
                            { url"dad@example.com", name:"Dad", color:"#00ff00" },
                            { url:"son@example.com", name:"Son", color:"#0000ff" },
                          ]
            

            that way the code is not sensitive to the order of the elements

                 validSenders.forEach(sender =>{
                         if (sender.name =="Dad") {
                              do_something(sender.url)
                         }
                 })
            

            So in this scenario, you have validSenders the array and, for example, validSenders.url as one attribute(?) of the array? In your example, where does “sender” come from? It’s not one of the named elements. Is it a sort of temporary variable that gets handed the array … object(?) … so in the first iteration of the forEach (which I assume loops through each entry in the validSenders array), sender would contain(?) url:"mom@example.com", name:“Mom”, and color:“#ff0000”? And sender.url would be “mom@example.com”?

            That makes sense. I apologize for not knowing the terminology; I’m very new to the whole OOP sort of thing.

            you can also use the array.filter() function

            let selected_sender = validSenders.filter(sender=>{
                  if(sender.url==mailObj.sender[0].address)
                       return true
                 else
                       return false
             })
            if(selected_sender.length>0){
                // we found a matching sender
            }
            

            the filter function passes each element array in turn to the function
            if you want the element in the output array return true,
            if not return false

            Could this return false if it doesn’t match but an index if it matches so that one could refer to validSenders.color[selected_sender] to get the right attribute?

            I really kinda wish I had a spare RPi laying around so I could do this testing on something other than my production mirror. (I mean, everyone’s aware that it’s a work-in-progress but I don’t want to muck it up.)

            • that.config.validSenders.includes(mailObj.sender[0].address)
            • daysAgo >= 0
            • daysAgo <= that.config.daysToDisplay

            the last two CANNOT be true at the same time

            I’m not sure I understand… if daysToDisplay is, say, 50 and daysAgo = 25, then the last two would both be true?

            What I’m trying to do is make sure that the sender of an e-mail is in the list of valid senders and that the date the e-mail was sent is not more than daysToDisplay days ago.

            maybe what you wanted was

            if (
                 that.config.validSenders.includes(mailObj.sender[0].address) && 
                (daysAgo >= 0 && daysAgo <= that.config.daysToDisplay)  
                 ) {
            

            this is two outer compares (with one inner)

            I thought I tried that but even so, I don’t understand how that’s different from what I had. I thought that “If A && B && C” would be the same as “If A && (B && C)” – in either case, all three have to be true for the whole thing to be true.

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

              @sdetweil said in Config option with array of multiple values?:

              maybe what you wanted was

              if (
                   that.config.validSenders.includes(mailObj.sender[0].address) && 
                  (daysAgo >= 0 && daysAgo <= that.config.daysToDisplay)  
                   ) {
              

              this is two outer compares (with one inner)

              I just tried this and it works. Not sure why the extra set of parentheses are needed but I’m not going to argue. 8^)

              Thanks!

              1 Reply Last reply Reply Quote 0
              • S Do not disturb
                sdetweil @UncleRoger
                last edited by sdetweil

                @UncleRoger array.filter() returns an array with the items that the matching function returned true
                so there is only one. no need for the index. it returned the entire object, item in the list

                sender is the name of the parameter passed to the matching function, the element of the array

                Sam

                How to add modules

                learning how to use browser developers window for css changes

                S UncleRogerU 2 Replies Last reply Reply Quote 0
                • S Do not disturb
                  sdetweil @sdetweil
                  last edited by

                  @UncleRoger >I really kinda wish I had a spare RPi laying

                  MagicMirror will run on windows. you don’t need a pi

                  Sam

                  How to add modules

                  learning how to use browser developers window for css changes

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

                    @sdetweil
                    I got it working with the .filter function:

                    let selSender = that.config.validSenders.filter(mySender => {
                    	if (mySender.addr.toLowerCase() == mailObj.sender[0].address.toLowerCase()) 
                    		return true
                    	 else 
                    		return false
                    });
                    

                    but now I’m having a problem trying to use the returned array. In this code the adding the color element to the subject works fine (that’s there for testing), as does the setting the style.color, but if I uncomment the if statement, it crashes:

                    subject = subject + selSender[0].color;
                    //		if (selSender[0].color != undefined) {
                    			subjectWrapper.style.color = selSender[0].color;
                    //		} else {
                    

                    Is there a reason it doesn’t like that in the IF statement? Note: I also tried it as just “if (selSender[0].color) {” and assigning the value of selSender[0].color to another variable and using that other variable in the IF statement. In at least one test case there is a value in color.

                    UncleRogerU 1 Reply Last reply Reply Quote 0
                    • UncleRogerU Offline
                      UncleRoger @UncleRoger
                      last edited by

                      @UncleRoger said in Config option with array of multiple values?:

                      subject = subject + selSender[0].color;
                      // if (selSender[0].color != undefined) {
                      subjectWrapper.style.color = selSender[0].color;
                      // } else {

                      Is there a reason it doesn’t like that in the IF statement? Note: I also tried it as just “if (selSender[0].color) {” and assigning the value of selSender[0].color to another variable and using that other variable in the IF statement. In at least one test case there is a value in color.

                      Okay, so it looks like Javascript wants “!==”, not “!=”. Ugh.

                      Anyway, I got it working using a “switch (true)” block.

                      Thanks for all the help!

                      S 1 Reply Last reply Reply Quote 0
                      • S Do not disturb
                        sdetweil @UncleRoger
                        last edited by sdetweil

                        @UncleRoger yes the compares

                        !=
                        !==
                        !===
                        

                        are different

                        Screenshot_20231027_125711_Chrome.jpg

                        one compare s the data type too
                        2 is a number
                        “2” is a string

                        Sam

                        How to add modules

                        learning how to use browser developers window for css changes

                        UncleRogerU 1 Reply Last reply Reply Quote 1
                        • MZ-BERM Offline
                          MZ-BER
                          last edited by

                          Hello @UncleRoger - are you planning to publish your module? And can I may ask you what this module will do?

                          UncleRogerU 2 Replies Last reply Reply Quote 1
                          • UncleRogerU Offline
                            UncleRoger @MZ-BER
                            last edited by

                            @MZ-BER said in Config option with array of multiple values?:

                            Hello @UncleRoger - are you planning to publish your module? And can I may ask you what this module will do?

                            Yes, I am planning to publish it, once I’ve tested it reasonably well. Mostly, it seems to work thus far. I’m doing more testing and working on documentation.

                            The module checks an e-mail address and then shows the subject of any e-mails on the MM. Basically, I wanted a way for my wife and I to post messages for the whole family to see. This is an idea I’ve had for a long time (going back to the days of pagers) and is kinda obsolete – we use text messages and instagram messages a lot these days – but I still wanted to make this happen.

                            Probably next week I’ll be posting a message “how do I put a module in github for everyone to use?” as I’m totally new to all of this. 8^)

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

                              @sdetweil said in Config option with array of multiple values?:

                              yes the compares
                              !=
                              !==
                              !===

                              are different

                              Heh. Back in my day, we got one equality operator and one inequality operator and we were dang thankful for that. Now git off my lawn! 8^)

                              Seriously, though, thanks for all your help!

                              S 1 Reply Last reply Reply Quote 0
                              • S Do not disturb
                                sdetweil @UncleRoger
                                last edited by

                                @UncleRoger I agree. I came in before these fancy new languages. before web.

                                Sam

                                How to add modules

                                learning how to use browser developers window for css changes

                                1 Reply Last reply Reply Quote 1
                                • S Do not disturb
                                  sdetweil @UncleRoger
                                  last edited by sdetweil

                                  @UncleRoger logon to GitHub, click repositories , new, make the name match your module name, create.

                                  it will give you a set of commands to execute in your module folder to upload it to that repo…

                                  easy peasy

                                  note that you need to create an access key thru the GitHub profile menu to be able to upload.(aka push)
                                  this replaces the requested password now

                                  Sam

                                  How to add modules

                                  learning how to use browser developers window for css changes

                                  UncleRogerU S 2 Replies Last reply Reply Quote 1
                                  • UncleRogerU Offline
                                    UncleRoger @sdetweil
                                    last edited by

                                    @sdetweil said in Config option with array of multiple values?:

                                    it will give you a set of commands to execute in your module folder to.upload it to that repo…
                                    easy peasy

                                    I’ve got a repository set up with a README.md file but I don’t see anything about how to upload the module.

                                    Also, is there anywhere with a list of what files I need to upload? These are the files in my directory:

                                     ll
                                    total 32
                                    -rw-r--r--  1 roger roger   59 Oct 25 07:43 email.css.orig
                                    -rw-r--r--  1 roger roger 6420 Oct 27 11:26 MMM-MailMessage.js
                                    -rw-r--r--  1 roger roger 3566 Oct 25 12:44 MMM-MailMessage.js.save
                                    -rw-r--r--  1 roger roger 3117 Oct 25 07:58 node_helper.js
                                    drwxr-xr-x 12 roger roger 4096 Oct 25 12:44 node_modules
                                    -rw-r--r--  1 roger roger  355 Oct 27 12:37 package.json
                                    -rw-r--r--  1 roger roger 3755 Oct 25 12:44 package-lock.json
                                    

                                    I’m pretty sure the node_modules directory comes from the emailjs-imap-client that it needs (which came from an npm install) so I don’t think that should be in the repository for this module. I’m not sure about package-lock.json. I think package.json is needed – it basically just has the name & description and says it needs the emailjs thing.

                                    Other than that, it’s MMM-MailMessage.js and node_helper.js so I think the files needed are:

                                    • MMM-MailMessage.js
                                    • node_helper.js
                                    • package.json

                                    Is that right?

                                    S 1 Reply Last reply Reply Quote 0
                                    • S Do not disturb
                                      sdetweil @sdetweil
                                      last edited by sdetweil

                                      @UncleRoger then to update your repo

                                      on the pi in your module folder

                                      git status
                                      will tell you the files that changed

                                      git add . 
                                      

                                      will add them all

                                      git add filename filename2 
                                      

                                      will add only those to the pending commit

                                      git commit -m "some message why you made these changes"
                                      

                                      then

                                      git push 
                                      

                                      to upload

                                      Sam

                                      How to add modules

                                      learning how to use browser developers window for css changes

                                      1 Reply Last reply Reply Quote 1
                                      • S Do not disturb
                                        sdetweil @UncleRoger
                                        last edited by

                                        @UncleRoger when you created the repo on GitHub you should have seen this output

                                        I named my repo fribble2 for this test
                                        Screenshot_20231028_053654_Chrome.jpg

                                        in the section on create new from commandline

                                        I would not do the git add for the readme as I have that already on the GitHub side

                                        Sam

                                        How to add modules

                                        learning how to use browser developers window for css changes

                                        UncleRogerU 1 Reply Last reply Reply Quote 1
                                        • UncleRogerU Offline
                                          UncleRoger @sdetweil
                                          last edited by

                                          @sdetweil

                                          I think I’ve managed to thoroughly muck things up. Apparently my changes are in a branch called “master” and the repository has a “main” and never the twain shall meet.

                                          If I go to the “pull requests” tab in github, it shows me the differences between the file there and my changed file but there’s no way to merge them. This is what I’m seeing:

                                          60809309-baf5-41c9-a45d-1f279659f590-image.png

                                          I have the module working for me on my system but I don’t seem to be able to get it to a state where I can publish it.

                                          I’ve never used git/github before (other than to download modules) and, frankly, there’s a lot of base knowledge I don’t have that seems to be required to understand it and get it working.

                                          S 2 Replies Last reply Reply Quote 0
                                          • S Do not disturb
                                            sdetweil @UncleRoger
                                            last edited by sdetweil

                                            @UncleRoger ok

                                            ok, the easiest way to handle this

                                            rename your module folder to some other name

                                            then git clone your github repo

                                            then copy the files from your renamed folder to the clone

                                            then in the clone folder do

                                             git add . 
                                            git commit -m "adding 1st commit"
                                            git push
                                            

                                            then the local branch will be the same as the remote(github) branch
                                            and all will be well from then on, and u can delete the renamed folder

                                            Sam

                                            How to add modules

                                            learning how to use browser developers window for css changes

                                            1 Reply Last reply Reply Quote 1

                                            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
                                            • 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