Skip to content

Instantly share code, notes, and snippets.

@iver56
Created October 22, 2022 07:06
Show Gist options
  • Save iver56/b2457152dc41e089db5135b2ea19db1b to your computer and use it in GitHub Desktop.
Save iver56/b2457152dc41e089db5135b2ea19db1b to your computer and use it in GitHub Desktop.
HomeyScript for "And" card that checks whether power price from Tibber is outdated
/*
* This script is meant to be used in a Homey Flow as an 'And' card.
*/
let sensorName = 'Relativ Strømpris'
log(`sensorName: ${sensorName}`);
// Get all devices
const devices = await Homey.devices.getDevices();
let device = null;
// Loop over all devices
for (const d of Object.values(devices)) {
// If this device isn't available, skip it.
if (!d.capabilitiesObj) continue;
if (d.name == sensorName) {
log(`\n=== Found device ${d.name} ===`);
device = d;
break;
}
}
if (null === device) {
throw new Error(`Could not find available device with name ${sensorName}`);
}
log(device.capabilitiesObj)
if (typeof device.capabilitiesObj.measure_price_total === 'undefined') {
throw new Error(`Could not find the property measure_price_total in device ${sensorName}`);
}
const lastUpdated = new Date(device.capabilitiesObj.measure_price_total.lastUpdated);
const now = new Date();
log(`lastUpdated: ${lastUpdated}`);
const diffInSeconds = (now - lastUpdated) / 1000;
// Deem it unresponsive if it has not been updated for the last 2.5 hours
return diffInSeconds > 2.5 * 60 * 60;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment