Need Help - Senor Values

How can I get all sensor having the capability “measure_generic.plant.nameX” with the string “problem”. NameX are values from 1 to 70.

I tried this with the following script, but I don´t get any value.

// Loop over all devices
// 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.class === ‘sensor’) {
if (device.capabilitiesObj[measure_generic].value === problem){
log (\n ${device.name} braucht Wasser);
}
}
}

Lot of thanks for your help

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’.

Andy

Thanks
I added it. Here are two outputs

‘**measure_generic.plant.gelbbunte_kriechspindel_51’**: {
id: ‘measure_generic.plant.gelbbunte_kriechspindel_51’,
type: ‘string’,
iconObj: {
id: ‘2b41045f0246a9312cec9161a2bc8079’,
url: ‘/api/icon/a4dd0ba4-0aed-4a16-a08a-7520c704fdb0’
},
title: ‘gelbbunte Kriechspindel (51)’,
getable: true,
setable: false,
insights: true,
insightsTitleTrue: null,
insightsTitleFalse: null,
value: ‘ok’,
lastUpdated: 2024-12-05T16:49:53.924Z

Another one is

'measure_generic.plant.passionsblume_59’: {
id: ‘measure_generic.plant.passionsblume_59’,
type: ‘string’,
iconObj: {
id: ‘2b41045f0246a9312cec9161a2bc8079’,
url: ‘/api/icon/a4dd0ba4-0aed-4a16-a08a-7520c704fdb0’
},
title: ‘Passionsblume (59)’,
getable: true,
setable: false,
insights: true,
insightsTitleTrue: null,
insightsTitleFalse: null,
value: ‘problem’,
lastUpdated: 2024-12-09T11:05:48.746Z
},

The common capability name is measure_generic.plant. The rest is different.

Can you help me how I can add now a check about the 70 sensors to get a list of the sensors with value: “problem” in measure_generic_plant.xxxxxx

Lot of thanks

Looks like you’re close.

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`);
}
}

Try replacing ${device.name} with:

${device.capabilitiesObj['measure_generic'].id}

Does that give you what you want?

Andy

Thanks a lot Andy,
I replaced it but still no output and no error message. :frowning:
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`);
  }
}

I changed the apostrophe.
Butt still the same…

// 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");
  }
}

Definitely won’t work, you need to use back ticks for templated strings.

If you’re not seeing a log message or an error message, the issue must be the test:

if (device.capabilitiesObj['measure_generic'].value === "problem"){

I can suggest a couple of things, but then I’m out of ideas:

  • Try replacing the strict equality === with equality ==
    I don’t see why that would be an issue, though
  • Try using != ‘ok’ instead; at the least it should output something!

Also, I just saw:

device.capabilitiesObj['measure_generic'].title

Would give you nicer output than using the id.

Andy

Unfortunatly none idea worked. But thanks a lot for your help and suggestions
But I found some code snippets. It is working using

// Get all devices
let devices = await Homey.devices.getDevices();

_.forEach(devices, device =>
  {
    if (device.class !=`sensor`) return;
    if (device.capabilitiesObj?.measure_generic == `problem`) return;
    log (`\n${device.name} braucht Wasser`);
  }
);

Now I get some sensors.
But there measure_generic.xxxxx value ist not like I want with value “problem”

Can any developer jump in and help.
It is hard stuff to understand for a newbie

Thanks

It seems you are using a device imported from HA :grinning:

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`);
  })
})

Thanks a lot Ronny. It is working like I want