How to read a sensor with a homey script

I am writing my first script and want to read the temperature of an Aqara sensor with name “Aqara ute”.
What is missing in this script?

var temp
let devices = await Homey.devices.getDevices();
_.forEach(devices, device => {
if (device.name == “Aqara ute”) {
temp = device.state.measure_temperature;
}
});
return temp;

Script Error:
Cannot read property ‘measure_temperature’ of undefined

I’m not sure why you’re using device.state, AFAIK that property doesn’t exist.

Reading the current value of a capability from Homeyscript was discussed here.

Okay, I am really a newbie in scripting… I have copy/pasted from the best of my knowledge, but I fail!
I am trying to read the Wind angle from a Netatmo Aneometer but this script return: Undefined

var angle
let devices = await Homey.devices.getDevices();
_.forEach(devices, device => {
if (device.name ==‘Netatmo - Vind’) {
angle = device.capabilities.measure_wind_angle;
}
});
return angle;

You could try device.capabilities[‘measure_wind_angle’]
Or
device.capabilities.measure[‘wind_angle’]

That should at least be device.capabilities['measure_wind_angle'] (which is exactly the same as device.capabilities.measure_wind_angle, though).

@Peter_Johnsson to find the correct name of the capability, find the device here and look at its list of capabilities.

1 Like

Right you are. Always forget those ‘:shushing_face:

1 Like

Well, thank you both. I did look in the delveloper tools and copy/pasted from there. Tried with device.capabilities[‘measure_wind_angle’]; but nothing else than Undefined…sadly

Try this:

angle = device.capabilitiesObj?.measure_wind_angle?.value;
1 Like

YES!!!

Extremely many thanks!

//Peter

Hi Guys!

Sorry to bother you again…

My script works just fine, thanks to you. But now I want to use the output, directions[ section ], to be used as a logical alpha for use in a flow. This is how the script looks like:

// Part I - getting the wind direction in degrees from my Netatomo Anemometer
var angle
let devices = await Homey.devices.getDevices();
_.forEach(devices, device => {
if (device.name ==‘Netatmo - Vind’) {
angle = device.capabilitiesObj?.measure_wind_angle?.value;
}
});

// Part II - converting wind direction in degrees to Cardinals
let directions = [“N”,“NNE”,“NE”,“ENE”,“E”,
“ESE”, “SE”, “SSE”,“S”,
“SSW”,“SW”,“WSW”,“W”,
“WNW”,“NW”,“NNW” ];
let section = parseInt( angle/22.5 + 0.5 );
section = section % 16;
return directions[ section ];

await tag(‘Directions’, directions);
This way you get a tag you can use in your flows

Thanks Marcel, where in the script should I insert this line? Tried some logical places but get errors…

After the variable you want. Is your variable directions[section]? In that case:
await tag(‘Directions’, directions[section]).
‘Directions’ maybe any given name you want as Tag, directions[section] has to be the name of your variable

All right! Cool man, now it works.

Again: Many thanks!

//Peter

another YES right here!

I’m just starting with script, but the example that comes with homey doesn’t even work and also mentions device.state so how do I check if a light is on? I have tried device.on device.onoff device.setcapabilities.onoff (it probably should be getcapabilities but that is not recognized either) but nothing seems to return anything useful.
The only thing I get to work is device.name

device.capabilitiesObj?.onoff?.value

Thanks, I finally found it already somewhere yesterday, I did think it should have been easier to find and a bit strange that an example that you get doesn’t work…Anyway, it works now, at least for the lights, I still do not get any details about lights that are behind a Hue socket unfortunately, script is as underneath now. (I have the undefined rule in there otherwise I get some errors)
Maybe a bit off topic, but is there anyway to reset the on/off memory of a device?
I also see some hue light that was on before the power got disconnected, and thus the memory stays on, and thus it shows up in the list, but I cannot switch it off because it doesn’t find it anymore (because of poweroff through a physical wallswitch).

forEach(devices, device =>
{
if(device.class != ‘light’ && device.virtualClass != ‘light’) return;
if (device.capabilitiesObj.onoff ==undefined) return;
if (device.capabilitiesObj.onoff.value)
{
console.log(device.name);
}
}
);

I think the check should be:

if (device.class != 'light' || device.virtualClass != 'light') return;

Homeyscript only reflects the state of the device as it’s known to Homey internally, so if Homey doesn’t know the correct state, there’s nothing you can do from Homeyscript to fix that.

if the comparison would be an OR, then it would return always? because if the class is a light, then the virtualclass isn’t a light and the other way around also, that is why I used an AND