# Homey.devices.updateDevice in HomeyScript

**URL:** <https://community.homey.app/t/homey-devices-updatedevice-in-homeyscript/88005>\
**Category:** Developers\
**Created:** [August 25, 2023, 11:45pm UTC](https://community.homey.app/t/homey-devices-updatedevice-in-homeyscript/88005 "2023-08-25T23:45:10Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![torarnv](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/torarnv/32/83065_2.png) [@torarnv](https://community.homey.app/u/torarnv)\
**Post date:** [August 25, 2023, 11:45pm UTC](https://community.homey.app/t/homey-devices-updatedevice-in-homeyscript/88005/1 "2023-08-25T23:45:10Z")

</div>

Trying to update the class of a device, via:

```auto
for (const device of Object.values(devices)) {
  if (device.zone != zombieZone.id)
    continue;
  if (device.class == "light") {
    device.virtualClass = "other";
    await Homey.devices.updateDevice({ id: device.id, device });
  }
}

```

But results in

```auto
———————————————————
❌ Script Error
⚠️ TypeError: Cannot read properties of null (reading 'approximation')
    at Function.value (/app/node_modules/athom-api/dist/index.js:1:863528)
    at o.<anonymous> (/app/node_modules/athom-api/dist/index.js:1:847079)
    at x (/app/node_modules/athom-api/dist/index.js:1:346562)
    at u._invoke (/app/node_modules/athom-api/dist/index.js:1:346357)
    at e.<computed> [as next] (/app/node_modules/athom-api/dist/index.js:1:346741)
    at t (/app/node_modules/athom-api/dist/index.js:1:9216)
    at /app/node_modules/athom-api/dist/index.js:1:9312
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

```

How can I update the device from HomeyScript?

---

<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:** [August 26, 2023, 6:13am UTC](https://community.homey.app/t/homey-devices-updatedevice-in-homeyscript/88005/2 "2023-08-26T06:13:45Z")

</div>

> [@torarnv](#):
>
> How can I update the device from HomeyScript?

Looks like you can’t:

```auto
await Homey.devices.updateDevice({ id : device.id, device : { virtualClass : 'other' } });
...
❌ Script Error
⚠️ Missing Scopes: Missing Scopes
    at /app/node_modules/athom-api/dist/index.js:1:1247257
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

```

---

<div class="post-metadata">

**Author:** ![torarnv](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/torarnv/32/83065_2.png) [@torarnv](https://community.homey.app/u/torarnv)\
**Post date:** [August 26, 2023, 8:00am UTC](https://community.homey.app/t/homey-devices-updatedevice-in-homeyscript/88005/3 "2023-08-26T08:00:19Z")

</div>

Yeah, I tried the simple object form as well, without success. I’ve been able to update the device via e.g ` curl -X PUT -H 'Content-Type: application/json' -d '{"class":"other"}' <homey-url>/api/manager/devices/device/<device-id>` but I was hoping to do the same thing from HomeyScript.

---

<div class="post-metadata">

**Author:** ![torarnv](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/torarnv/32/83065_2.png) [@torarnv](https://community.homey.app/u/torarnv)\
**Post date:** [August 26, 2023, 8:04am UTC](https://community.homey.app/t/homey-devices-updatedevice-in-homeyscript/88005/4 "2023-08-26T08:04:03Z")

</div>

And I can’t use `await fetch('api/manager/devices/device', ...);` as only absolute URLs are supported.

---

<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:** [August 26, 2023, 8:04am UTC](https://community.homey.app/t/homey-devices-updatedevice-in-homeyscript/88005/5 "2023-08-26T08:04:25Z")

</div>

I don’t really understand how Athom blocks this operation from HomeyScript but you can still do it from an HTTP request 🤷🏼‍♂️

But you can make HTTP requests from HomeyScript using `fetch()`. Bit of a strange workaround, but if it works, it works.

---

<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:** [August 26, 2023, 8:05am UTC](https://community.homey.app/t/homey-devices-updatedevice-in-homeyscript/88005/6 "2023-08-26T08:05:01Z")

</div>

You can probably use `http://127.0.0.1/api/manager/devices/device`

---

<div class="post-metadata">

**Author:** ![torarnv](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/torarnv/32/83065_2.png) [@torarnv](https://community.homey.app/u/torarnv)\
**Post date:** [August 26, 2023, 8:11am UTC](https://community.homey.app/t/homey-devices-updatedevice-in-homeyscript/88005/7 "2023-08-26T08:11:23Z")

</div>

Thanks! This worked in the end:

```auto
for (const device of Object.values(devices)) {
  if (device.zone != zombieZone.id)
    continue;
  if (device.class == "light") {
    await fetch('http://127.0.0.1/api/manager/devices/device/' + device.id, {
      method: 'PUT',
      headers: { 'Content-Type': 'application/json', 'Authorization' : "Bearer <token>"},
      body: JSON.stringify({ 'class': 'other' })
    });
  }
}

```

A bit awkward to have to hard-code the authorization 🙂

(For context, I used this to turn a bunch of bulb light devices that I have aggregated into a single light via into class:other, so that Homey doesn’t report them as part of the “15 lights are on” in the latest mobile app update).

---

<div class="post-metadata">

**Author:** ![Baldhor](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/baldhor/32/27028_2.png) [@Baldhor](https://community.homey.app/u/Baldhor)\
**Post date:** [August 27, 2023, 4:38pm UTC](https://community.homey.app/t/homey-devices-updatedevice-in-homeyscript/88005/8 "2023-08-27T16:38:20Z")

</div>

updateDevice and deleteDevice can only be used from the [Homey Developer Tools](https://tools.developer.homey.app/tools/api-playground).

As about changing the class of a device, you can just do: `myDevice.setClass('...');`

---

<div class="post-metadata">

**Author:** ![Baldhor](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/baldhor/32/27028_2.png) [@Baldhor](https://community.homey.app/u/Baldhor)\
**Post date:** [August 27, 2023, 4:40pm UTC](https://community.homey.app/t/homey-devices-updatedevice-in-homeyscript/88005/9 "2023-08-27T16:40:01Z")

</div>

It means we can actually delete a device from a driver through this API! Good news.

---

<div class="post-metadata">

**Author:** ![torarnv](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/torarnv/32/83065_2.png) [@torarnv](https://community.homey.app/u/torarnv)\
**Post date:** [August 31, 2023, 9:34pm UTC](https://community.homey.app/t/homey-devices-updatedevice-in-homeyscript/88005/10 "2023-08-31T21:34:19Z")

</div>

Thanks for the tip about not being able to do updateDevice from HomeyScript, it worked i the developer tools as you say. But setClass doesn’t seem to work: `device.setClass is not a function`. No problem, I just use the REST endpoint 🙂

---

<div class="post-metadata">

**Author:** ![Baldhor](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/baldhor/32/27028_2.png) [@Baldhor](https://community.homey.app/u/Baldhor)\
**Post date:** [September 3, 2023, 8:33am UTC](https://community.homey.app/t/homey-devices-updatedevice-in-homeyscript/88005/11 "2023-09-03T08:33:47Z")

</div>

Surprising, my app ESPhome does it from the driver on existing device:

```auto
                    // Get virtual device
                    let virtualDevice = this.getDevices().filter(virtualDevice => virtualDevice.getData().id === virtualDeviceId)[0];

                    // Modify the class
                    if (virtualDevice.getClass() !== newDeviceClass) {
                        virtualDevice.setClass(newDeviceClass);
                    }

```

remark:: yes I could use find instead of filter 🙂

---

<div class="post-metadata">

**Author:** ![torarnv](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/torarnv/32/83065_2.png) [@torarnv](https://community.homey.app/u/torarnv)\
**Post date:** [September 3, 2023, 8:45am UTC](https://community.homey.app/t/homey-devices-updatedevice-in-homeyscript/88005/12 "2023-09-03T08:45:38Z")

</div>

Right, the app can do it on its own devices, via the App SDK, but it’s not part of the JS API for the Web API

---

<div class="post-metadata">

**Author:** ![GD91](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/gd91/32/78099_2.png) [@GD91](https://community.homey.app/u/GD91)\
**Post date:** [September 27, 2023, 5:45pm UTC](https://community.homey.app/t/homey-devices-updatedevice-in-homeyscript/88005/13 "2023-09-27T17:45:23Z")

</div>

> [@torarnv](#):
>
> ```auto
> for (const device of Object.values(devices)) {
> if (device.zone != zombieZone.id)
> continue;
> if (device.class == "light") {
> await fetch('http://127.0.0.1/api/manager/devices/device/' + device.id, {
> method: 'PUT',
> headers: { 'Content-Type': 'application/json', 'Authorization' : "Bearer <token>"},
> body: JSON.stringify({ 'class': 'other' })
> });
> }
> }
> 
> ```

I have exactly the same issue : change the class of some devices from ‘light’ to ‘heater’.  
But, I was not successful using your code !!  
Could you give me more detailed process ?  
Regards

---

<div class="post-metadata">

**Author:** ![GD91](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/gd91/32/78099_2.png) [@GD91](https://community.homey.app/u/GD91)\
**Post date:** [September 28, 2023, 10:04am UTC](https://community.homey.app/t/homey-devices-updatedevice-in-homeyscript/88005/14 "2023-09-28T10:04:04Z")

</div>

I try this code, it seems working, but the new class does not remain after the end of the code …  
 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/4/8/48b0acfa243834ce1969608ab74deb47f0f10ebc.png)  
And the result :  
 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/3/9/3955103f52449d96c2bdb3994a26c19994c78da6.png)

---

<div class="post-metadata">

**Author:** ![GD91](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/gd91/32/78099_2.png) [@GD91](https://community.homey.app/u/GD91)\
**Post date:** [September 28, 2023, 10:16am UTC](https://community.homey.app/t/homey-devices-updatedevice-in-homeyscript/88005/15 "2023-09-28T10:16:05Z")

</div>

I’ve tried your code, and there is no error , but no result, the class is not modified :

 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/5/d/5d55f1bdf97bd694f44a17489280fdce8362936f.png)  
and the result :  
 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/f/d/fd8dec5a5db68beb7a42c85999bb53e592329d4b.png)  
Could you help me ?  
REgards

---

<div class="post-metadata">

**Author:** ![torarnv](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/torarnv/32/83065_2.png) [@torarnv](https://community.homey.app/u/torarnv)\
**Post date:** [September 28, 2023, 10:17am UTC](https://community.homey.app/t/homey-devices-updatedevice-in-homeyscript/88005/16 "2023-09-28T10:17:16Z")

</div>

> [@torarnv](#):
>
> `<token>`

You have to replace this with your actual bearer token. There are a few ways to do this, not sure which one is the best one: [homey get bearer token - Google Search](https://www.google.com/search?q=homey+get+bearer+token)

---

<div class="post-metadata">

**Author:** ![GD91](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/gd91/32/78099_2.png) [@GD91](https://community.homey.app/u/GD91)\
**Post date:** [September 28, 2023, 1:05pm UTC](https://community.homey.app/t/homey-devices-updatedevice-in-homeyscript/88005/17 "2023-09-28T13:05:07Z")

</div>

I understand, now. It is so obvious …  
I look for this token, but I was not successful …

I still need some extra 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:** [September 28, 2023, 1:08pm UTC](https://community.homey.app/t/homey-devices-updatedevice-in-homeyscript/88005/18 "2023-09-28T13:08:10Z")

</div>

`device` is not updated dynamically, after you make the call to `fetch()` you need to reload the script to see if the class has changed (or go to [this page](https://tools.developer.homey.app/tools/devices)).

---

<div class="post-metadata">

**Author:** ![GD91](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/gd91/32/78099_2.png) [@GD91](https://community.homey.app/u/GD91)\
**Post date:** [September 28, 2023, 4:40pm UTC](https://community.homey.app/t/homey-devices-updatedevice-in-homeyscript/88005/19 "2023-09-28T16:40:23Z")

</div>

Finally, I got the bearer token by the mean of Robert Klepp code.  
And I was succesful changing the class of devices from light to Heater.  
Thanks a lot for your help
