I’d remove the test for device.class. It seems to me unlikely that a device with the measure_generic capability would not be a sensor. But you’ll know your setup better than me.
If you add log(device.capabilitiesObj); immediately before testing the value, you’ll do two things:
Check you are testing for the correct capability (it won’t log anything if not)
See how capabilities are stored in the object
I suspect you’ll need to burrow down into the object since measure_generic.plant.nameX suggests that measure_generic contains ‘plant’ and that ‘plant’ contains several ‘namex’s’.
It would help if you use the ‘Preformated text’ icon to enclose your code since the forum has done something with your quotes. I’m guessing your code looks something like this:
// Get all devices
const devices = await Homey.devices.getDevices();
// Loop over all devices
for (const device of Object.values(devices)) {
// If this device isn’t available, skip it.
if (!device.capabilitiesObj[`measure_generic`]) continue;
if (device.capabilitiesObj[`measure_generic`].value === `problem`){
log (`\n ${device.name} Braucht Wasser`);
}
}
Thanks a lot Andy,
I replaced it but still no output and no error message.
Here my actual code
// Get all devices
const devices = await Homey.devices.getDevices();
// Loop over all devices
for (const device of Object.values(devices)) {
// If this device isn’t available, skip it.
if (!device.capabilitiesObj[`measure_generic`]) continue;
if (device.capabilitiesObj[`measure_generic`].value === `problem`){
log (`\n ${device.capabilitiesObj[`measure_generic`].id} braucht Wasser`);
}
}
// Get all devices
const devices = await Homey.devices.getDevices();
// Loop over all devices
for (const device of Object.values(devices)) {
// If this device isn’t available, skip it.
if (!device.capabilitiesObj['measure_generic']) continue;
if (device.capabilitiesObj['measure_generic'].value === "problem"){
log ("\n ${device.capabilitiesObj['measure_generic'].id} braucht Wasser");
}
}
What do do you want to read from your device?
You want to get all devices with value ‘problem’?
measure_generic is a string capability. And it seems you added it from an entity in repair view. Then a subcapability is used.
What about adding an alarm capability in addition that is set if the entity state is ‘problem’? Then you also can see this in Homey. And use it in your script.
Or you can add the entity as main capability (only measure_generic) - if it’s the only capability with this ID in the device.
Or you can just check the value independent of the capability ID. Let me buil da short script…
Check this. It just checks all sensor devices if any measure_generic* capability has a value ‘problem’.
let devices = await Homey.devices.getDevices();
Object.keys(devices).forEach( (key) => {
let device = devices[key];
if (device.class != 'sensor') return;
if (!device.capabilitiesObj) return;
Object.keys(device.capabilitiesObj).forEach( (key) => {
if (key.startsWith('measure_generic') && device.capabilitiesObj[key].value == 'problem')
log (`\n${device.name} braucht Wasser`);
})
})