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!