• Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
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.

Switch URL based on value

Scheduled Pinned Locked Moved Solved Troubleshooting
10 Posts 3 Posters 486 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.
  • S Offline
    sdetweil @htilburgs
    last edited by Dec 11, 2024, 10:39 PM

    @htilburgs said in Switch URL based on value:

    @htilburgs said in Switch URL based on value:

    if (this.config.P1_IP != null)

    what is the setting for that in the defaults:{} section?

    Sam

    How to add modules

    learning how to use browser developers window for css changes

    H 1 Reply Last reply Dec 12, 2024, 5:32 AM Reply Quote 0
    • H Offline
      htilburgs @sdetweil
      last edited by Dec 12, 2024, 5:32 AM

      @sdetweil

      
      	// Default values
      	defaults: {
      		P1_IP: null,				// IP Address P1 Meter
      		WM_IP: null,				// IP Address Water Meter
      		maxWidth: "500px",			// Max width wrapper
      		extraInfo: false,			// Show extra information
      		showFooter: false,			// Show footer (name Power Meter)
      		currentPower: true,			// Show current power usage
      		initialLoadDelay: 1000,
      		updateInterval: 10000			// Every 10 seconds
      	},
      
      

      (still trying to learn JS, but not afraid to ask) ☺

      M S 2 Replies Last reply Dec 12, 2024, 4:19 PM Reply Quote 0
      • M Offline
        MMRIZE @htilburgs
        last edited by Dec 12, 2024, 4:19 PM

        @htilburgs
        Just for your reference

        const ip_a = "..."
        const ip_b = "..."
        
        const apiQuery = (index, url) => {
          if (!url) return Promise.reject({ index, reason: 'url is not defined' })
          return new Promise((resolve, reject) => {
            try {
              fetch(url)
                .then(response => response.json())
                .then(data => {
                  resolve({ index, value: data })
                })
                .catch(error => {
                  reject({ index, reason: error })
                })
            } catch (error) {
              reject({ index, reason: error })
            }
          })
        }
        
        const queries = []
        queries.push(apiQuery('A', ip_a))
        queries.push(apiQuery('B', ip_b))
        Promise.allSettled(queries).then((results) => {
          console.log('All jobs are done')
          results.values().forEach((result) => {
            if (result.status === 'fulfilled') {
              console.log(`Index: ${result.value.index}, Value:`, result.value.value)
            } else {
              console.log(`Index: ${result.reason.index}, Error:`, result.reason.reason)
            }
          })
        })
        
        1 Reply Last reply Reply Quote 0
        • S Offline
          sdetweil @htilburgs
          last edited by Dec 12, 2024, 4:22 PM

          @htilburgs hm… looks ok, I would use the developer window sources tab to debug the code and look at the variables… (mouse over name usage will show contents)

          Sam

          How to add modules

          learning how to use browser developers window for css changes

          H 1 Reply Last reply Dec 12, 2024, 4:30 PM Reply Quote 0
          • H Offline
            htilburgs @sdetweil
            last edited by Dec 12, 2024, 4:30 PM

            @sdetweil
            In the console - sources it says:

            Uncaught SyntaxError: Unexpected token 'if'
            

            (still trying to learn JS, but not afraid to ask) ☺

            S 1 Reply Last reply Dec 12, 2024, 4:34 PM Reply Quote 0
            • S Offline
              sdetweil @htilburgs
              last edited by Dec 12, 2024, 4:34 PM

              @htilburgs well, if the code breaks, then …

              so, look at that line and see what is missing from before it

              a quick way to find syntax errors

              in the module folder, run the pre-compiler to check for errors

              node -c modulename.js
              

              (or any js file)

              Sam

              How to add modules

              learning how to use browser developers window for css changes

              H 1 Reply Last reply Dec 12, 2024, 4:38 PM Reply Quote 0
              • H Offline
                htilburgs @sdetweil
                last edited by Dec 12, 2024, 4:38 PM

                @sdetweil
                Meanwhile I was doing this, and I found the error (stupid me)

                	start: function () {
                		Log.info("Starting module: " + this.name);
                		requiresVersion: "2.9.0";	
                			
                		// Set locales
                
                		if (this.config.P1_IP != null) {
                			this.urlP1 = "http://" + this.config.P1_IP + "/api/v1/data/";
                		} else {
                			this.urlP1 = "https://dummyjson.com/c/7e24-36ab-48e0-a96d";
                		}
                
                		if (this.config.WM_IP != null) {
                			this.urlWM = "http://" + this.config.WM_IP + "/api/v1/data/";
                		} else {
                			this.urlWM = "https://dummyjson.com/c/704a-9a96-4845-bc72";
                		}
                //		this.urlP1 = "http://" + this.config.P1_IP + "/api/v1/data/";
                //		this.urlWM = "http://" + this.config.WM_IP + "/api/v1/data/";
                		
                    		this.MHW_P1 = [];	        // <-- Create empty MHW_P1 array
                		this.MHW_WM = [];		// <-- Create empty MHW_WM array
                		this.scheduleUpdate();       	// <-- When the module updates (see below)
                	},
                

                The line with "requiresVersion: “2.9.0”; " was ending with an “,” instead of “;”

                without the “if” statement, I don’t get a failure with a “,”
                So changed it now into “;” and everythins seems to work.
                I continue testing.

                @MMRIZE Thanks for the alternative way. I’m going to look into it.
                So with the “promise” statement, it looks like I can eleminate the node_helper.js

                (still trying to learn JS, but not afraid to ask) ☺

                S 2 Replies Last reply Dec 12, 2024, 5:02 PM Reply Quote 0
                • S Offline
                  sdetweil @htilburgs
                  last edited by sdetweil Dec 12, 2024, 8:36 PM Dec 12, 2024, 5:02 PM

                  @htilburgs said in Switch URL based on value:

                  The line with "requiresVersion: “2.9.0”; " was ending with an “,” instead of “;”

                  requiresVersion should be outside the start function… (and then needs a trailing comma

                  Sam

                  How to add modules

                  learning how to use browser developers window for css changes

                  1 Reply Last reply Reply Quote 0
                  • S Offline
                    sdetweil @htilburgs
                    last edited by sdetweil Dec 13, 2024, 7:01 PM Dec 12, 2024, 5:03 PM

                    @htilburgs said in Switch URL based on value:

                    So with the “promise” statement, it looks like I can eleminate the node_helper.js

                    correct, now that electron (and nodejs) have fetch built in(since node 18), you don’t NEED to use node_helper to do it

                    Sam

                    How to add modules

                    learning how to use browser developers window for css changes

                    1 Reply Last reply Reply Quote 0
                    • S sdetweil has marked this topic as solved on Dec 14, 2024, 2:44 PM
                    • 1 / 1
                    1 / 1
                    • First post
                      6/10
                      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