I’m also getting this error. Tried it with or without adding port 8899
I’m on the older ECU-R (2160xxxxxxxx series)
Please advise.
——————————————————— Script Success Returned: undefined
Error fetching the URL: FetchError: request to http://192.168.1.82:8899/index.php/realtimedata failed, reason: socket hang up
at ClientRequest. (/app/node_modules/node-fetch/lib/index.js:1501:11)
at ClientRequest.emit (node:events:517:28)
at Socket.socketOnEnd (node:_http_client:525:9)
at Socket.emit (node:events:529:35)
at endReadableNT (node:internal/streams/readable:1400:12)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
type: ‘system’,
errno: ‘ECONNRESET’,
code: ‘ECONNRESET’
}
——————————————————— Script Success Returned: undefined
Error fetching the URL: FetchError: request to http://192.168.0.175:8899/index.php/realtimedata failed, reason: socket hang up
at ClientRequest. (/app/node_modules/node-fetch/lib/index.js:1501:11)
at ClientRequest.emit (node:events:517:28)
at Socket.socketOnEnd (node:_http_client:525:9)
at Socket.emit (node:events:529:35)
at endReadableNT (node:internal/streams/readable:1400:12)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
type: ‘system’,
errno: ‘ECONNRESET’,
code: ‘ECONNRESET’
}
I agree with you because not everyone has the capability to programming code and I’ll hope if there’s anybody who can develop an APP for the ApSystems EMA to use it in Homey to control and use the appliances while the sun is shining.
Hope someone pick this up.
Create a Virtual Device (requires virtual devices app) :
This device will display the power value from the number variable.
Set Up the Homey Script:
Copy the script provided below.
Modify the urlAPsystems constant to match the IP address of your APsystems ECU-R (replace 0.0.0.0 with the correct address).
Adjust the variableStatus constant to match the name of your text variable.
Adjust the variablePower constant to match the name of your number variable.
Create a Flow:
Trigger: Set it to run at a regular interval (e.g., every 2 minutes).
Action 1: Execute the Homey script.
Action 2: Update the virtual device with the value from the number variable.
const vars = await Homey.logic.getVariables();
const startTime = Date.now();
// Define the URL of the webpage to scrape
const urlAPsystems = 'http://0.0.0.0/index.php/realtimedata';
const variableStatus = 'solarStatus';
const variablePower = 'solarPower';
// Function to fetch the webpage content
async function fetchWebPage(urlAPsystems) {
const response = await fetch(urlAPsystems);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return await response.text();
}
// Function to parse the HTML content and extract the power values
function extractPowerValues(html) {
const regex = /<td>\s*(\d+)\s*W\s*<\/td>/g;
let match;
const powerValues = [];
while ((match = regex.exec(html)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (match.index === regex.lastIndex) {
regex.lastIndex++;
}
const powerValue = parseInt(match[1], 10);
if (!isNaN(powerValue)) {
powerValues.push(powerValue);
}
}
return powerValues;
}
async function main() {
try {
const htmlContent = await fetchWebPage(urlAPsystems);
const powerValues = extractPowerValues(htmlContent);
// Sum the power values
const totalPower = powerValues.reduce((sum, value) => sum + value, 0);
const SolarPowerDevice = _.find(vars, (o) => o.name === variablePower)
await Homey.logic.updateVariable({id: SolarPowerDevice.id, variable: {value: -totalPower}});
const elapsedTime = Date.now() - startTime
// Get current date and time
const currentDate = new Date();
// Get day, month, year, hours, and minutes
const day = String(currentDate.getDate()).padStart(2, '0');
const month = String(currentDate.getMonth() + 1).padStart(2, '0');
const year = currentDate.getFullYear();
const hours = String(currentDate.getHours()).padStart(2, '0');
const minutes = String(currentDate.getMinutes()).padStart(2, '0');
const SolarStatusMessage = `${day}-${month}-${year} ${hours}:${minutes} | ms ${elapsedTime} | `;
const SolarErrorMessage = _.find(vars, (o) => o.name === variableStatus)
await Homey.logic.updateVariable({id: SolarErrorMessage.id, variable: {value: `${SolarStatusMessage} Status: OK`}});
console.log('Total power: ', totalPower, 'W');
console.log('Status: ', SolarStatusMessage);
} catch (error) {
console.error(`${SolarStatusMessage} Status:`, error.message);
const SolarErrorMessage = _.find(vars, (o) => o.name === variableStatus)
await Homey.logic.updateVariable({id: SolarErrorMessage.id, variable: {value: error.message}});
}
}
main();
Please note that I initially used the APsystems ECU-R via a Wi-Fi connection, but it was very unstable and unreliable. After switching to a LAN connection, everything started working fine.
@Mombasa Thanks for developing the script! I followed your instructions but it doesn’t seem to update my virtual device with correct Power value. It doesn’t overwrite the default value “0” that I used to create the Number Variable.
This is my script:
const vars = await Homey.logic.getVariables();
const startTime = Date.now();
// Define the URL of the webpage to scrape
const urlAPsystems = 'http://192.168.1.82/index.php/realtimedata';
const variableStatus = 'solarStatus';
const variablePower = 'solarPower';
// Function to fetch the webpage content
async function fetchWebPage(urlAPsystems) {
const response = await fetch(urlAPsystems);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return await response.text();
}
// Function to parse the HTML content and extract the power values
function extractPowerValues(html) {
const regex = /<td>\s*(\d+)\s*W\s*<\/td>/g;
let match;
const powerValues = [];
while ((match = regex.exec(html)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (match.index === regex.lastIndex) {
regex.lastIndex++;
}
const powerValue = parseInt(match[1], 10);
if (!isNaN(powerValue)) {
powerValues.push(powerValue);
}
}
return powerValues;
}
async function main() {
try {
const htmlContent = await fetchWebPage(urlAPsystems);
const powerValues = extractPowerValues(htmlContent);
// Sum the power values
const totalPower = powerValues.reduce((sum, value) => sum + value, 0);
const SolarPowerDevice = _.find(vars, (o) => o.name === variablePower)
await Homey.logic.updateVariable({id: SolarPowerDevice.id, variable: {value: -totalPower}});
const elapsedTime = Date.now() - startTime
// Get current date and time
const currentDate = new Date();
// Get day, month, year, hours, and minutes
const day = String(currentDate.getDate()).padStart(2, '0');
const month = String(currentDate.getMonth() + 1).padStart(2, '0');
const year = currentDate.getFullYear();
const hours = String(currentDate.getHours()).padStart(2, '0');
const minutes = String(currentDate.getMinutes()).padStart(2, '0');
const SolarStatusMessage = `${day}-${month}-${year} ${hours}:${minutes} | ms ${elapsedTime} | `;
const SolarErrorMessage = _.find(vars, (o) => o.name === variableStatus)
await Homey.logic.updateVariable({id: SolarErrorMessage.id, variable: {value: `${SolarStatusMessage} Status: OK`}});
console.log('Total power: ', totalPower, 'W');
console.log('Status: ', SolarStatusMessage);
} catch (error) {
console.error(`${SolarStatusMessage} Status:`, error.message);
const SolarErrorMessage = _.find(vars, (o) => o.name === variableStatus)
await Homey.logic.updateVariable({id: SolarErrorMessage.id, variable: {value: error.message}});
}
}
main();
YOU WROTE Create two variables:
Text variable: Used for troubleshooting.
Numeric variable: stores the calculated power.
Create a virtual device (requires a virtual device app):
This device will display the power value from the numeric variable.
Hello, could you detail the operations to be done above?
THANK YOU
In the script, variableStatus and variablePower are placeholders. They represent the actual variables names in your code, which in this example are solarStatus and solarPower. You can customize these names to fit your specific needs. Rename your variables to solarStatus and solarPower, or choose your own names. If you opt for different names, remember to update the script accordingly to reflect these changes.
A virtual device can be created directly from the home screen by clicking the ‘+’ sign in the web browser. I use this app for that purpose: Virtual Devices.
Hello, I’m sorry but I don’t understand at all how to do it, please give me more details which will surely be useful for the whole community, thank you in advance Arnaud
It doesn’t seem to connect to this URL, although my ECU-R is perfectly accessible via the EMA App. Furthermore, I have recently set up an active API connection via HomeWizard’s Energy App that works perfectly, although it only updates every 15 minutes. When I try to ping the IP everything looks good. Very strange.
This has been my problem from the beginning. The website is not accessable from within my home (eventhough the ip address is visible in my ubiquity system). Also adding the varios mentioned ports here and on Github do not give me access. hence any and all script fails, as they cannot get any data from the website…
Anybody getting data from their ECU-R through the internal IP address?
some here, I can also connect to that external address. Unfortunately I was also not able to scrape the information through a direct connection to the website, as it does not represent in an easy number anywhere on the site, the current amount of power being produced
My ECU-R is connected wirelessly through my router.
I noticed that the ECU-R has a poor uptime when connected via wireless. The moment I switched to a wired connection, the uptime was 100%. Please note that the IP adress will change the moment you switch to a cabel connection, so the url needs to be adjusted accordingly.
Please note that my script crawls this page and calculates all values from the Current Power column. If the page is inaccessible, the script will fail to function. Therefore, it is essential that this page loads correctly.