I want to fetch an external xml file, and access a certain value in that xml file. Does anybody have an example for this in Homeyscript?
You may have more luck with the HTTP request flow cards app, which has XML support (as opposed to Homeyscript, which doesn’t).
Ik heb een vraag ik wil graag deze data uit mijn SAJ inverter uitlezen in een text variabele SAJxml
const url = ‘http://192.168.1.120/real_time_data.xml’;
<real_time_data>
Normal
227.2
0.14
49.99
22
9.3
0.0
0.6
15131.8
15086.40
41895.4
175.8
0.04
-
-
359.2
</real_time_data>
Ik probeer dit
var xmlhttp = new XMLHttpRequest();
xmlhttp.open(“GET”, “http://192.168.1.120/real_time_data.xml”, false);
xmlhttp.send();
SAJxml = xmlhttp.responseXML;
Hoe zou ik dit met HTTP request flow card kunnen doen. of beter via Homey Script omdat deze app niet meer ondersteund wordt door Homey Pro 2023
(English only)
HomeyScript supports fetch()
to retrieve data from a URL. However, it doesn’t support XML so you’ll have to extract the data “manually”.
I don’t know how exactly it’ll work with the HTTP Request Flow Cards app (haven’t used it for a long time), but it probably won’t be very difficult.
have you had any results with this?
Nope
I managed to read some data from the inverter using HomeyScripts (with a lot of help from ChatGPT, of course).
// Define the URL of the XML data
const url = ‘http://192.168.0.245/real_time_data.xml’;
// Make an HTTP GET request to fetch the XML data
fetch(url)
.then(response => response.text())
.then(data => {
// Parse the XML data using regular expressions
const parsedData = parseXML(data);
// Access specific values from the parsed data
const state = parsedData.state;
const voltageL1 = parsedData.Vac_l1;
const currentL1 = parsedData.Iac_l1;
const frequency = parsedData.Freq1;
const powerGenerated = parsedData.pac1;
const temperature = parsedData.temp;
const energyToday = parsedData['e-today'];
const energyTotal = parsedData['e-total'];
// Print the retrieved data
console.log('Inverter State:', state);
console.log('Voltage L1:', voltageL1, 'V');
console.log('Current L1:', currentL1, 'A');
console.log('Frequency:', frequency, 'Hz');
console.log('Power Generated:', powerGenerated, 'W');
console.log('Temperature:', temperature, '°C');
console.log('Energy Today:', energyToday, 'kWh');
console.log('Energy Total:', energyTotal, 'kWh');
})
.catch(error => {
console.error(‘Error fetching XML data:’, error.message);
});
// Function to parse XML data using regular expressions
function parseXML(xml) {
const regex = /<(\w+)>([^<]+)</\1>/g;
let match;
const data = {};
while ((match = regex.exec(xml)) !== null) {
const nodeName = match[1];
const nodeValue = match[2];
data[nodeName] = nodeValue;
}
return data;
}
Now, I really have no idea what I can do with it in Homey. I tried to create an app, but I haven’t made much progress with that