# Setting Logic Variable by HomeyScript

**URL:** <https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819>\
**Category:** Questions & Help\
**Created:** [June 25, 2019, 3:11pm UTC](https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819 "2019-06-25T15:11:16Z")\
**Posts on this page:** 1\
**Showing post:** 53

<div class="post-metadata">

**Author:** ![Eko\_taas](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/eko_taas/32/74256_2.png) [@Eko\_taas](https://community.homey.app/u/Eko_taas)\
**Post date:** [November 28, 2023, 6:49pm UTC](https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819/53 "2023-11-28T18:49:25Z")

</div>

Just is case some other newbees (like me) around, made one also for sensors (as need those values as well in several scripts), combined above advice and Homey example-script:

> \_.myReadSensor = async function (sensorName, capabilityName) {  
> const devices = await Homey.devices.getDevices();
> 
> for (const device of Object.values(devices)) {  
> if (!device.capabilitiesObj || device.class !== ‘sensor’) continue;
> 
> ```
> if (device.name === sensorName) {
> const sensorCapability = device.capabilitiesObj[capabilityName];
> if (sensorCapability) {
> return sensorCapability.value;
> } else {
> return null; 
> }
> }
> 
> ```
> 
> }  
> return null;  
> }

---

_[View the full topic](https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819)._
