Hi David @David_Piper
I hope you are well, and it being quiet here is only because you are busy.
I got bored one weekend, and came up with my own mad scientist kind of bodge solution for automating Octopus events… and thought I’d share it here, just in case anyone else wanted to automate power down events. This is just an example, and I freely admit AI helped me write the Homey Script (as I am not great with java script). But it works okay-ish for me.
I also couldn’t find a way to automate signing on to the power down events. This only works if you click the link in the email they send you and accept the challenge first. Otherwise you’re just automatically powering down for no reason. But if like me, you’re so scattered that you click the accept button, but then forget all about it, this helps.
The code in the Homey Script Then run code with argument and return text-tag is:
const subject = args[0];
const match = subject.match(/[0-9]+-[0-9]+[a|p]m/);
const eventTimeRange = match ? match[0] : '';
return eventTimeRange;
And the Homey Script Then run script card points to a script containing this code:
// Get all Logic variables
const variables = await Homey.logic.getVariables();
// Find variables by name
const eventTimeRange = Object.values(variables).find(
v => v.name === 'OctopusEventTime'
);
const eventStartHr = Object.values(variables).find(
v => v.name === 'OctopusEventStartHour'
);
const eventEndHr = Object.values(variables).find(
v => v.name === 'OctopusEventEndHour'
);
if (!eventTimeRange) throw new Error('Variable "OctopusEventTime" not found');
if (!eventStartHr) throw new Error('Variable "OctopusEventStartHour" not found');
if (!eventEndHr) throw new Error('Variable "OctopusEventEndHour" not found');
// Example: "6-7pm"
const range = String(eventTimeRange.value).trim().toLowerCase();
// Extract hours and the single am/pm suffix
const match = range.match(/^(\d{1,2})\s*-\s*(\d{1,2})\s*(am|pm)$/);
if (!match) {
throw new Error(
Invalid time range "${range}". Expected format such as "6-7pm" or "6-7am".
);
}
let startHour = Number(match[1]);
let endHour = Number(match[2]);
const period = match[3];
// Convert both hours to 24-hour format
if (period === 'pm') {
if (startHour !== 12) startHour += 12;
if (endHour !== 12) endHour += 12;
} else {
// AM
if (startHour === 12) startHour = 0;
if (endHour === 12) endHour = 0;
}
// Update Homey number variables
await Homey.logic.updateVariable({
id: eventStartHr.id,
variable: {
value: startHour
}
});
await Homey.logic.updateVariable({
id: eventEndHr.id,
variable: {
value: endHour
}
});
return OctopusEventTime "${range}" → Start: ${startHour}, End: ${endHour};
This assumes that you have a Text variable named: OctopusEventTime and two Number variables named: OctopusEventStartHour & OctopusEventEndHour
The Homey Apps used are:
Mail Hook [scans an IMAP mailbox at a set interval for the Octopus event email]
Homey Script
Action Scheduler [with 2 cron devices added and “Run Once” enabled on them]
Advanced Triggers [I used this, but it can easily be done without it if you wish. I just like the ability to trigger actions from multiple different Flows, without having to repeat the logic.]
I hope one day to be able to do without all this, with native app support for power down/up events… as I hope to get solar and batteries going soon, and automating charging or going off-grid for events would be a game changer. Meantime I shall keep on bodging things ha ha