Read the statement by Michael Teeuw here.
Switch URL based on value
-
In the script it will assume you have 2 different IP addresses to query an URL.
1 IP for the HomeWizard P1 meter
1 IP for the HomeWizard Water Meter
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)[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' } }
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
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 = []; // <-- Create empty MHW_P1 array this.MHW_WM = []; // <-- Create empty MHW_WM array this.scheduleUpdate(); // <-- When the module updates (see below) },
-
@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 -
@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?
-
// 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 },
-
@htilburgs
Just for your referenceconst 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) } }) })
-
@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)
-
@sdetweil
In the console - sources it says:Uncaught SyntaxError: Unexpected token 'if'
-
@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)
-
@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 -
@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
-
@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
-