# How to read a sensor with a homey script

**URL:** <https://community.homey.app/t/how-to-read-a-sensor-with-a-homey-script/31962>\
**Category:** Questions & Help\
**Created:** [July 2, 2020, 8:19pm UTC](https://community.homey.app/t/how-to-read-a-sensor-with-a-homey-script/31962 "2020-07-02T20:19:47Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Oscar\_Andersson](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/oscar_andersson/32/26933_2.png) [@Oscar\_Andersson](https://community.homey.app/u/Oscar_Andersson)\
**Post date:** [July 2, 2020, 8:19pm UTC](https://community.homey.app/t/how-to-read-a-sensor-with-a-homey-script/31962/1 "2020-07-02T20:19:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [July 2, 2020, 8:57pm UTC](https://community.homey.app/t/how-to-read-a-sensor-with-a-homey-script/31962/2 "2020-07-02T20:57:31Z")

</div>

I’m not sure why you’re using `device.state`, AFAIK [that property doesn’t exist](https://developer.athom.com/docs/api/HomeyAPI.ManagerDevices.Device.html).

Reading the current value of a capability from Homeyscript [was discussed here](https://community.homey.app/t/device-capabilitiesobj-in-homeyscript-stays-the-same-value-when-triggered-in-a-flow/14529/3).

---

<div class="post-metadata">

**Author:** ![Peter\_Johnsson](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/peter_johnsson/32/563_2.png) [@Peter\_Johnsson](https://community.homey.app/u/Peter_Johnsson)\
**Post date:** [February 15, 2023, 11:57am UTC](https://community.homey.app/t/how-to-read-a-sensor-with-a-homey-script/31962/3 "2023-02-15T11:57:52Z")

</div>

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;

---

<div class="post-metadata">

**Author:** ![Marcel\_Ubels](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcel_ubels/32/18860_2.png) [@Marcel\_Ubels](https://community.homey.app/u/Marcel_Ubels)\
**Post date:** [February 15, 2023, 12:34pm UTC](https://community.homey.app/t/how-to-read-a-sensor-with-a-homey-script/31962/4 "2023-02-15T12:34:47Z")

</div>

> [@Peter\_Johnsson](#):
>
> angle = device.capabilities.measure\_wind\_angle;

You could try device.capabilities[‘measure\_wind\_angle’]  
Or  
device.capabilities.measure[‘wind\_angle’]

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [February 15, 2023, 12:36pm UTC](https://community.homey.app/t/how-to-read-a-sensor-with-a-homey-script/31962/5 "2023-02-15T12:36:35Z")

</div>

> [@Marcel\_Ubels](#):
>
> 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](https://tools.developer.homey.app/tools/devices) and look at its list of capabilities.

---

<div class="post-metadata">

**Author:** ![Marcel\_Ubels](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcel_ubels/32/18860_2.png) [@Marcel\_Ubels](https://community.homey.app/u/Marcel_Ubels)\
**Post date:** [February 15, 2023, 12:38pm UTC](https://community.homey.app/t/how-to-read-a-sensor-with-a-homey-script/31962/6 "2023-02-15T12:38:37Z")

</div>

Right you are. Always forget those ‘🤫

---

<div class="post-metadata">

**Author:** ![Peter\_Johnsson](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/peter_johnsson/32/563_2.png) [@Peter\_Johnsson](https://community.homey.app/u/Peter_Johnsson)\
**Post date:** [February 15, 2023, 12:44pm UTC](https://community.homey.app/t/how-to-read-a-sensor-with-a-homey-script/31962/7 "2023-02-15T12:44:37Z")

</div>

> [@robertklep](#):
>
> [‘measure\_wind\_angle’]

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

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [February 15, 2023, 12:55pm UTC](https://community.homey.app/t/how-to-read-a-sensor-with-a-homey-script/31962/8 "2023-02-15T12:55:46Z")

</div>

Try this:

```auto
angle = device.capabilitiesObj?.measure_wind_angle?.value;

```

---

<div class="post-metadata">

**Author:** ![Peter\_Johnsson](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/peter_johnsson/32/563_2.png) [@Peter\_Johnsson](https://community.homey.app/u/Peter_Johnsson)\
**Post date:** [February 15, 2023, 12:59pm UTC](https://community.homey.app/t/how-to-read-a-sensor-with-a-homey-script/31962/9 "2023-02-15T12:59:27Z")

</div>

> [@robertklep](#):
>
> `device.capabilitiesObj?.measure_wind_angle?.value;`

YES!!!

Extremely many thanks!

//Peter

---

<div class="post-metadata">

**Author:** ![Peter\_Johnsson](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/peter_johnsson/32/563_2.png) [@Peter\_Johnsson](https://community.homey.app/u/Peter_Johnsson)\
**Post date:** [February 15, 2023, 3:06pm UTC](https://community.homey.app/t/how-to-read-a-sensor-with-a-homey-script/31962/10 "2023-02-15T15:06:55Z")

</div>

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];

---

<div class="post-metadata">

**Author:** ![Marcel\_Ubels](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcel_ubels/32/18860_2.png) [@Marcel\_Ubels](https://community.homey.app/u/Marcel_Ubels)\
**Post date:** [February 15, 2023, 3:27pm UTC](https://community.homey.app/t/how-to-read-a-sensor-with-a-homey-script/31962/11 "2023-02-15T15:27:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Peter\_Johnsson](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/peter_johnsson/32/563_2.png) [@Peter\_Johnsson](https://community.homey.app/u/Peter_Johnsson)\
**Post date:** [February 15, 2023, 4:12pm UTC](https://community.homey.app/t/how-to-read-a-sensor-with-a-homey-script/31962/12 "2023-02-15T16:12:02Z")

</div>

> [@Marcel\_Ubels](#):
>
> await tag(‘Directions’, directions);

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

---

<div class="post-metadata">

**Author:** ![Marcel\_Ubels](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcel_ubels/32/18860_2.png) [@Marcel\_Ubels](https://community.homey.app/u/Marcel_Ubels)\
**Post date:** [February 15, 2023, 4:39pm UTC](https://community.homey.app/t/how-to-read-a-sensor-with-a-homey-script/31962/13 "2023-02-15T16:39:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Peter\_Johnsson](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/peter_johnsson/32/563_2.png) [@Peter\_Johnsson](https://community.homey.app/u/Peter_Johnsson)\
**Post date:** [February 15, 2023, 5:02pm UTC](https://community.homey.app/t/how-to-read-a-sensor-with-a-homey-script/31962/14 "2023-02-15T17:02:53Z")

</div>

All right! Cool man, now it works.

Again: Many thanks!

//Peter

---

<div class="post-metadata">

**Author:** ![TheR](https://avatars.discourse-cdn.com/v4/letter/t/977dab/32.png) [@TheR](https://community.homey.app/u/TheR)\
**Post date:** [March 14, 2023, 7:38pm UTC](https://community.homey.app/t/how-to-read-a-sensor-with-a-homey-script/31962/15 "2023-03-14T19:38:26Z")

</div>

another YES right here!

---

<div class="post-metadata">

**Author:** ![Arjan\_Neet](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/arjan_neet/32/17808_2.png) [@Arjan\_Neet](https://community.homey.app/u/Arjan_Neet)\
**Post date:** [March 11, 2024, 8:43pm UTC](https://community.homey.app/t/how-to-read-a-sensor-with-a-homey-script/31962/17 "2024-03-11T20:43:24Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/7/7/7735fcfd0ea8c13c1796eb5b98599a851b55f19f.png)

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [March 12, 2024, 6:14am UTC](https://community.homey.app/t/how-to-read-a-sensor-with-a-homey-script/31962/18 "2024-03-12T06:14:13Z")

</div>

`device.capabilitiesObj?.onoff?.value`

---

<div class="post-metadata">

**Author:** ![Arjan\_Neet](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/arjan_neet/32/17808_2.png) [@Arjan\_Neet](https://community.homey.app/u/Arjan_Neet)\
**Post date:** [March 12, 2024, 7:07am UTC](https://community.homey.app/t/how-to-read-a-sensor-with-a-homey-script/31962/19 "2024-03-12T07:07:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [March 12, 2024, 7:13am UTC](https://community.homey.app/t/how-to-read-a-sensor-with-a-homey-script/31962/20 "2024-03-12T07:13:51Z")

</div>

> [@Arjan\_Neet](#):
>
> I still do not get any details about lights that are behind a Hue socket unfortunately

I think the check should be:

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

```

> [@](#):
>
> Maybe a bit off topic, but is there anyway to reset the on/off memory of a device?

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.

---

<div class="post-metadata">

**Author:** ![Arjan\_Neet](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/arjan_neet/32/17808_2.png) [@Arjan\_Neet](https://community.homey.app/u/Arjan_Neet)\
**Post date:** [March 12, 2024, 7:33am UTC](https://community.homey.app/t/how-to-read-a-sensor-with-a-homey-script/31962/21 "2024-03-12T07:33:38Z")

</div>

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

[Next page](https://community.homey.app/t/how-to-read-a-sensor-with-a-homey-script/31962.md?page=2)
