Carl, @Welshsmarthome , @Dd3 ,
Well, it should fetch this data:
Now, the script only shows account data, because the other three data types are “commented out”, meaning, it doesn’t act as executable code, it just shows as comment because of the leading //
So, remove the leading //
to get the meterdata and billing details
To write the data to tags, which you can use in flows, you can add this, right below the respective console.log lines positions in the script:
console.log('Electricity Meter Data:', data);
await tag('Electricity Meter Data:', `"${data}"`);
console.log('Gas Meter Data:',data);
await tag('Gas Meter Data:', `"${data}"`);
console.log('Billing Details:', data);
await tag('Billing Details:', `"${data}"`);
(Maybe you’ll have to comment out the 'console.log… ’ lines)
Here is my homeyscript which pulls electrical usage and works !! Which returns data in this format ! Now I need someone expert in Homey to show me how to get the data into homey and graph it up (NON HOMEY EXPERT)
Electricity Consumption Data:
Date: 2025-03-14
Interval: 2025-03-14T23:30:00Z ➝ 2025-03-15T00:00:00Z
Usage: 3.085 kWh
Date: 2025-03-14
Interval: 2025-03-14T23:00:00Z ➝ 2025-03-14T23:30:00Z
Usage: 2.473 kWh
// Octopus Energy API credentials
const API_KEY = 'sk_live_XXXXX'; // Replace with your Octopus API Key
const MPAN = '20020119223456’; // Your Octopus MPAN
const SERIAL_NUMBER = '19LXXXXX’; // Your Meter Serial Number
// Encode API Key for Basic Authentication
const encodedAuth = Buffer.from(`${API_KEY}:`).toString('base64');
// Octopus Energy API Endpoint for Consumption Data
const CONSUMPTION_URL = `https://api.octopus.energy/v1/electricity-meter-points/${MPAN}/meters/${SERIAL_NUMBER}/consumption/`;
// Function to fetch consumption data from Octopus API
async function fetchConsumptionData() {
try {
let response = await fetch(CONSUMPTION_URL, {
headers: { 'Authorization': `Basic ${encodedAuth}` }
});
if (!response.ok) throw new Error(`Error: ${response.status}`);
let data = await response.json();
return data;
} catch (error) {
console.error('❌ Failed to fetch data:', error);
return null;
}
}
// Function to process and display consumption data
async function processConsumptionData() {
let consumptionData = await fetchConsumptionData();
if (consumptionData && consumptionData.results) {
console.log('📊 Electricity Consumption Data:');
let totalConsumption = 0;
let binaryConsumption = [];
consumptionData.results.forEach(entry => {
console.log(`📅 Date: ${entry.interval_start.substring(0, 10)}`);
console.log(`⏳ Interval: ${entry.interval_start} ➝ ${entry.interval_end}`);
console.log(`⚡ Usage: ${entry.consumption.toFixed(3)} kWh`);
console.log("----------------------------");
// Add to total consumption
totalConsumption += entry.consumption;
});
console.log(`🔥 Total Daily Consumption: ${totalConsumption.toFixed(2)} kWh`);
} else {
console.log('❌ No consumption data available.');
}
}
// Execute function
processConsumptionData();
You can write the numeric values (no emoticons!) to pre-created numeric variables; then they’ll pop up in Insights.
Example on how to find (by var ID) and update variables:
let testDay = new Date();
let dateNum = testDay.getDate();
let monthTxtShort = testDay.toLocaleString('fefault', { month: 'short'})
let TodaysDate = Object.values(await Homey.logic.getVariables()).find(({name}) => name === "TodaysDate");
let TodaysDateId = TodaysDate.id
let MonthTextShort = Object.values(await Homey.logic.getVariables()).find(({name}) => name === "MonthTextShort");
let MonthTextShortId = MonthTextShort.id
Homey.logic.updateVariable({id: TodaysDateId, variable: {'value' : dateNum}} )
Homey.logic.updateVariable({id: MonthTextShortId, variable: {'value' : monthTxtShort}} )
With this app, you can create graphs from Insights data if you like:
https://homey.app/a/nl.mennovanhout.insights-graphs/
These graphs can be displayed in a virtual cam f.i.