Created
October 22, 2022 07:15
-
-
Save iver56/796320c8e64aa3396c1ea449cc3e2c1a to your computer and use it in GitHub Desktop.
HomeyScript for "And" card that checks whether a Tibber Pulse is unresponsive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* This script is meant to be used in a Homey Flow as an 'And' card. | |
*/ | |
let sensorName = '<insert pulse name/address here>' | |
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}`); | |
} | |
if (typeof device.capabilitiesObj.measure_power === 'undefined') { | |
throw new Error(`Could not find the property measure_power in device ${sensorName}`); | |
} | |
const lastUpdated = new Date(device.capabilitiesObj.measure_power.lastUpdated); | |
const now = new Date(); | |
log(`lastUpdated: ${lastUpdated}`); | |
const diffInSeconds = (now - lastUpdated) / 1000; | |
// Deem it unresponsive if it has not reported for the last 15 minutes | |
return diffInSeconds > 15 * 60; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment