# How to set device value on script

**URL:** <https://community.homey.app/t/how-to-set-device-value-on-script/99944>\
**Category:** Questions & Help\
**Created:** [January 4, 2024, 8:46am UTC](https://community.homey.app/t/how-to-set-device-value-on-script/99944 "2024-01-04T08:46:13Z")\
**Posts on this page:** 3\
**Page:** 1

<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:** [January 4, 2024, 8:46am UTC](https://community.homey.app/t/how-to-set-device-value-on-script/99944/1 "2024-01-04T08:46:13Z")

</div>

I would need to create function allowing me to change values on devices. Continued trials on on below topic  
[[Setting Logic Variable by HomeyScript - #53 by Eko\_taas](https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819/53)]

Example of myDevice (only part of actual AVD)

> “\_HiidenSite”: {  
> “devicecapabilities\_text-custom\_12.text6”: {  
> “title”: “currentOutTemp”,  
> “value”: -17.1  
> },  
> …  
> }

as wanted to use more title instead of cabability, made this code for testing **reading by title** (and working fine):

```auto
> const devices = await Homey.devices.getDevices();
> 
> const sensorName = "_HiidenSite";
> const capabilityTitle = "currentOutTemp";
> 
> for (const device of Object.values(devices)) {
> if (device.name === sensorName) {
> for (const capability of Object.values(device.capabilitiesObj)) {
> if (capability.title === capabilityTitle) {
> return capability.value; // Return the capability value (even if it's null)
> }
> }
> return -1000; // Indicate capability title not found for the sensor
> }
> }
> return -2000; // Indicate sensor not found

```

Now wanted to create code to **update device value by title** … but could only get working within script

```auto
//async function setSensorValueByTitle(sensorName, capabilityTitle, newValue) {
  // Get all devices
  const devices = await Homey.devices.getDevices();

const sensorName = "_HiidenSite";
const capabilityTitle = "currentOutTemp";
const newValue = -18.1;

  for (const device of Object.values(devices)) {
    if (device.name === sensorName) {
      for (const capability of Object.values(device.capabilitiesObj)) {
        if (capability.title === capabilityTitle) {
          capability.value = newValue; // Update the capability value with the new value
          device.capabilitiesObj.value = newValue
          return newValue;
        }
      }
      return -1000; // Indicate capability title not found for the sensor
    }
  }
  return -2000; // Indicate sensor not found
//}

```

Was not able to figure out how to modify this to get it to Homey-level (and not only till this script)

> device.capabilitiesObj.value = newValue

On earlier mentioned topic can see how to do that for variable, but what to do with when device?

Thanks in advance for any help…

---

<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:** [January 4, 2024, 10:51am UTC](https://community.homey.app/t/how-to-set-device-value-on-script/99944/2 "2024-01-04T10:51:11Z")

</div>

> [@Eko\_taas](#):
>
> On earlier mentioned topic can see how to do that for variable, but what to do with when device?

You need to create [a capability instance](https://athombv.github.io/node-homey-api/HomeyAPIV2.ManagerDevices.Device.html#makeCapabilityInstance).

---

<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:** [January 4, 2024, 1:36pm UTC](https://community.homey.app/t/how-to-set-device-value-on-script/99944/3 "2024-01-04T13:36:04Z")

</div>

Thanks for good advice and direction to go… needed some help with AI to create code but helped a lot how to push it to correct direction 👍

For records if someone else finds idea useful:

```auto
const devices = await Homey.devices.getDevices();

sensorName = "_HiidenSite";
capabilityTitle = "currentOutTemp";
newValue = "-18.2";

  for (const device of Object.values(devices)) {
    if (device.name === sensorName) {
      for (const capability of Object.values(device.capabilitiesObj)) {
        if (capability.title === capabilityTitle) {
          try {
            await device.setCapabilityValue(capability.id, newValue);
            console.log(`Changed '${capabilityTitle}' value to ${newValue} for ${sensorName}`);
            return newValue; // Indicate the value has been updated
          } catch (error) {
            console.error("Failed to update value:", error);
            return -999; // Indicate update failure
          }
        }
      }
      return -1999; // Indicate capability title not found for the sensor
    }
  }
  return -2999; // Indicate sensor not found

```

> Changed ‘currentOutTemp’ value to -18.2 for \_HiidenSite
> 
> ———————————————————  
> ✅ Script Success
> 
> ↩ Returned: “-18.2”

and

> ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/e/5/e54fe9f1933b7b879b444e5ce49b415fd91fcf59.png)
