<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Switch URL based on value]]></title><description><![CDATA[<p dir="auto">In the script it will assume you have 2 different IP addresses to query an URL.<br />
1 IP for the HomeWizard P1 meter<br />
1 IP for the HomeWizard Water Meter<br />
This is because I like to show the information of both in 1 script. Everything works fine if both are used, but when someone only has the P1 meter, the IP for the Water Meter is not used and results in a console fetch error (normal behavior)</p>
<pre><code>[ERROR] Error: TypeError: fetch failed
    at node:internal/deps/undici/undici:13178:13
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  [cause]: Error: getaddrinfo ENOTFOUND null
      at GetAddrInfoReqWrap.onlookupall [as oncomplete] (node:dns:120:26) {
    errno: -3008,
    code: 'ENOTFOUND',
    syscall: 'getaddrinfo',
    hostname: 'null'
  }
} 
</code></pre>
<p dir="auto">Is there a way to switch URL in case one of both URL are NULL? I’ve tried, but it doesn’t work with if-else</p>
<pre><code>	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.MHW_P1 = [];	        // &lt;-- Create empty MHW_P1 array
		this.MHW_WM = [];		// &lt;-- Create empty MHW_WM array
		this.scheduleUpdate();       	// &lt;-- When the module updates (see below)
	},

</code></pre>
]]></description><link>https://forum.magicmirror.builders/topic/19215/switch-url-based-on-value</link><generator>RSS for Node</generator><lastBuildDate>Wed, 13 May 2026 00:52:54 GMT</lastBuildDate><atom:link href="https://forum.magicmirror.builders/topic/19215.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 11 Dec 2024 22:14:42 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Switch URL based on value on Thu, 12 Dec 2024 16:38:59 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sdetweil" aria-label="Profile: sdetweil">@<bdi>sdetweil</bdi></a><br />
Meanwhile I was doing this, and I found the error (stupid me)</p>
<pre><code>	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 = [];	        // &lt;-- Create empty MHW_P1 array
		this.MHW_WM = [];		// &lt;-- Create empty MHW_WM array
		this.scheduleUpdate();       	// &lt;-- When the module updates (see below)
	},
</code></pre>
<p dir="auto">The line with "requiresVersion: “2.9.0”;	" was ending with an “,” instead of “;”</p>
<p dir="auto">without the “if” statement, I don’t get a failure with a “,”<br />
So changed it now into “;” and everythins seems to work.<br />
I continue testing.</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mmrize" aria-label="Profile: MMRIZE">@<bdi>MMRIZE</bdi></a> Thanks for the alternative way. I’m going to look into it.<br />
So with the “promise” statement, it looks like I can eleminate the node_helper.js</p>
]]></description><link>https://forum.magicmirror.builders/post/121883</link><guid isPermaLink="true">https://forum.magicmirror.builders/post/121883</guid><dc:creator><![CDATA[htilburgs]]></dc:creator><pubDate>Thu, 12 Dec 2024 16:38:59 GMT</pubDate></item><item><title><![CDATA[Reply to Switch URL based on value on Fri, 13 Dec 2024 19:01:07 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/htilburgs" aria-label="Profile: htilburgs">@<bdi>htilburgs</bdi></a> said in <a href="/post/121883">Switch URL based on value</a>:</p>
<blockquote>
<p dir="auto">So with the “promise” statement, it looks like I can eleminate the node_helper.js</p>
</blockquote>
<p dir="auto">correct, now that electron (and nodejs) have fetch built in(since node 18), you don’t NEED to use node_helper to do it</p>
]]></description><link>https://forum.magicmirror.builders/post/121886</link><guid isPermaLink="true">https://forum.magicmirror.builders/post/121886</guid><dc:creator><![CDATA[sdetweil]]></dc:creator><pubDate>Fri, 13 Dec 2024 19:01:07 GMT</pubDate></item><item><title><![CDATA[Reply to Switch URL based on value on Thu, 12 Dec 2024 20:36:33 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/htilburgs" aria-label="Profile: htilburgs">@<bdi>htilburgs</bdi></a> said in <a href="/post/121883">Switch URL based on value</a>:</p>
<blockquote>
<p dir="auto">The line with "requiresVersion: “2.9.0”; " was ending with an “,” instead of “;”</p>
</blockquote>
<p dir="auto">requiresVersion should be outside the start function… (and then needs a trailing comma</p>
]]></description><link>https://forum.magicmirror.builders/post/121885</link><guid isPermaLink="true">https://forum.magicmirror.builders/post/121885</guid><dc:creator><![CDATA[sdetweil]]></dc:creator><pubDate>Thu, 12 Dec 2024 20:36:33 GMT</pubDate></item><item><title><![CDATA[Reply to Switch URL based on value on Thu, 12 Dec 2024 16:38:59 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sdetweil" aria-label="Profile: sdetweil">@<bdi>sdetweil</bdi></a><br />
Meanwhile I was doing this, and I found the error (stupid me)</p>
<pre><code>	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 = [];	        // &lt;-- Create empty MHW_P1 array
		this.MHW_WM = [];		// &lt;-- Create empty MHW_WM array
		this.scheduleUpdate();       	// &lt;-- When the module updates (see below)
	},
</code></pre>
<p dir="auto">The line with "requiresVersion: “2.9.0”;	" was ending with an “,” instead of “;”</p>
<p dir="auto">without the “if” statement, I don’t get a failure with a “,”<br />
So changed it now into “;” and everythins seems to work.<br />
I continue testing.</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mmrize" aria-label="Profile: MMRIZE">@<bdi>MMRIZE</bdi></a> Thanks for the alternative way. I’m going to look into it.<br />
So with the “promise” statement, it looks like I can eleminate the node_helper.js</p>
]]></description><link>https://forum.magicmirror.builders/post/121883</link><guid isPermaLink="true">https://forum.magicmirror.builders/post/121883</guid><dc:creator><![CDATA[htilburgs]]></dc:creator><pubDate>Thu, 12 Dec 2024 16:38:59 GMT</pubDate></item><item><title><![CDATA[Reply to Switch URL based on value on Thu, 12 Dec 2024 16:34:12 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/htilburgs" aria-label="Profile: htilburgs">@<bdi>htilburgs</bdi></a> well, if the code breaks, then …</p>
<p dir="auto">so, look at that line and see what is missing from before it</p>
<p dir="auto">a quick way to find syntax errors</p>
<p dir="auto">in the module folder, run the pre-compiler to check for errors</p>
<pre><code>node -c modulename.js
</code></pre>
<p dir="auto">(or any js file)</p>
]]></description><link>https://forum.magicmirror.builders/post/121882</link><guid isPermaLink="true">https://forum.magicmirror.builders/post/121882</guid><dc:creator><![CDATA[sdetweil]]></dc:creator><pubDate>Thu, 12 Dec 2024 16:34:12 GMT</pubDate></item><item><title><![CDATA[Reply to Switch URL based on value on Thu, 12 Dec 2024 16:30:33 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sdetweil" aria-label="Profile: sdetweil">@<bdi>sdetweil</bdi></a><br />
In the console - sources it says:</p>
<pre><code>Uncaught SyntaxError: Unexpected token 'if'
</code></pre>
]]></description><link>https://forum.magicmirror.builders/post/121881</link><guid isPermaLink="true">https://forum.magicmirror.builders/post/121881</guid><dc:creator><![CDATA[htilburgs]]></dc:creator><pubDate>Thu, 12 Dec 2024 16:30:33 GMT</pubDate></item><item><title><![CDATA[Reply to Switch URL based on value on Thu, 12 Dec 2024 16:22:13 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/htilburgs" aria-label="Profile: htilburgs">@<bdi>htilburgs</bdi></a> 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)</p>
]]></description><link>https://forum.magicmirror.builders/post/121880</link><guid isPermaLink="true">https://forum.magicmirror.builders/post/121880</guid><dc:creator><![CDATA[sdetweil]]></dc:creator><pubDate>Thu, 12 Dec 2024 16:22:13 GMT</pubDate></item><item><title><![CDATA[Reply to Switch URL based on value on Thu, 12 Dec 2024 16:19:38 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/htilburgs" aria-label="Profile: htilburgs">@<bdi>htilburgs</bdi></a><br />
Just for your reference</p>
<pre><code class="language-js">const ip_a = "..."
const ip_b = "..."

const apiQuery = (index, url) =&gt; {
  if (!url) return Promise.reject({ index, reason: 'url is not defined' })
  return new Promise((resolve, reject) =&gt; {
    try {
      fetch(url)
        .then(response =&gt; response.json())
        .then(data =&gt; {
          resolve({ index, value: data })
        })
        .catch(error =&gt; {
          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) =&gt; {
  console.log('All jobs are done')
  results.values().forEach((result) =&gt; {
    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)
    }
  })
})
</code></pre>
]]></description><link>https://forum.magicmirror.builders/post/121879</link><guid isPermaLink="true">https://forum.magicmirror.builders/post/121879</guid><dc:creator><![CDATA[MMRIZE]]></dc:creator><pubDate>Thu, 12 Dec 2024 16:19:38 GMT</pubDate></item><item><title><![CDATA[Reply to Switch URL based on value on Thu, 12 Dec 2024 05:32:03 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sdetweil" aria-label="Profile: sdetweil">@<bdi>sdetweil</bdi></a></p>
<pre><code>
	// 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
	},

</code></pre>
]]></description><link>https://forum.magicmirror.builders/post/121874</link><guid isPermaLink="true">https://forum.magicmirror.builders/post/121874</guid><dc:creator><![CDATA[htilburgs]]></dc:creator><pubDate>Thu, 12 Dec 2024 05:32:03 GMT</pubDate></item><item><title><![CDATA[Reply to Switch URL based on value on Wed, 11 Dec 2024 22:39:09 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/htilburgs" aria-label="Profile: htilburgs">@<bdi>htilburgs</bdi></a> said in <a href="/post/121866">Switch URL based on value</a>:</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/htilburgs" aria-label="Profile: htilburgs">@<bdi>htilburgs</bdi></a> said in <a href="/post/121866">Switch URL based on value</a>:</p>
<blockquote>
<p dir="auto">if (this.config.P1_IP != null)</p>
</blockquote>
<p dir="auto">what is the setting for that in the defaults:{} section?</p>
]]></description><link>https://forum.magicmirror.builders/post/121867</link><guid isPermaLink="true">https://forum.magicmirror.builders/post/121867</guid><dc:creator><![CDATA[sdetweil]]></dc:creator><pubDate>Wed, 11 Dec 2024 22:39:09 GMT</pubDate></item></channel></rss>