Homey Community Forum

Rest API call for setting asleep

Hi,

I currently have presence and sleep detection setup using BL. But was looking into using the built in one instead. I am no programmer, so I’m just going on by trial and error. So far, only error. How can I do a rest call to set the value?

I can read all users by calling /api/manager/users/user/ or
a specific user by /api/manager/users/user//

But no matter how I format the body I cannot change the value. It just stays as:
“asleep”: false

Any help would be appreciated.

\ Andreas

To find out…

Open a Browser to the API documentation,
https://developer.athom.com/docs/api/HomeyAPI.ManagerPresence.html
Open a Web Api Playground
https://developer.athom.com/tools/api-playground

Open Inspect (Ctr-Shift-I)

Get your ID

Homey.users.getUserMe();

Try…
Homey.users.updateUser({id:'43ba47df-4026-4a12-b3cc-90c44a025549',user:{asleep:true}})
Error: Parameter 'asleep' is read-only.

And don’t give up…

Homey.presence.setAsleep({id:'43ba47df-4026-4a12-b3cc-90c44a025549',value:true})
image

image

image

Happy Hacking :wink:

2 Likes

Thank you so much! Worked perfect :slight_smile:

Funny thing, I’ve been using almost exactly that to get a new Bearer token last few days due to a bug in 2.1.1 expiring them once a day.

Have a great weekend!

Hmm, guess I was a bit quick. Seems like I ran into another noob issue right away. It appears as it works as intended from the Web API Playground. But when trying in Postman or RESTask in Tasker it toggles the value every time I run a PUT. I was expecting it to change to or not change if already the same.

I’m probably not formatting the value {“asleep”:true} (and a few variants) in the body correctly. If I remove it completely, I still get the toggle behavior.

Again, any help would be appreciated.

I read {value:true} above as request payload …

I’m trying my best, but still cannot succeed. I found an old post describing how to use RESTask properly. https://forum.athom.com/discussion/996/tutorial-using-tasker-and-the-homey-api
So I did the same with the proper URL but it still only toggles every time I try to set it.


Maybe somethings have changed since 1.5 when it comes to the API? I also tried to set the value as header or parameter. Trying all variants of with or without curly brackets and dubble quotes. Still only toggles. Feel almost like the mad scientist here, trying everything without hitting the perfect combination :smiley:

Maybe all this is for nothing since the Bearer tokens now expire after 24h. I thought it was a bug in 2.1.1 fixed in 2.1.2. But it seems not. If this is the case, then Homey just lost a point until I can figure out how to authenticate properly.

Great info, helped me a lot. But just another question you might be able to answer; When a create an app and use the same function

‘this.api.presence.setPresent({id:‘xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx’,value:true})’

I get an error:

(node:20292) UnhandledPromiseRejectionWarning: missing_scopes: Je hebt geen toegang om dit te doen.

I don’t get it; the this.api.users.getUsers() command does work.

If I can get this solved I can get my Life360 connector working, and that would be awsome!

TIA,

Cheers,
/Cacti

It says you have not the authorization scope necessary to do that.
According to:
https://developer.athom.com/docs/api/HomeyAPI.ManagerPresence.html

Required OAuth scopes

  • homey.presence

Before v2 it was possible from within an HomeyApp and from HomeyScript, since the new authorization this is not possible there as the scope is mostly *.readonly (sorry, cant find it in documentation)

Edit:
Found!!
https://apps.developer.athom.com/tutorial-SDK%20Changelog.html

use of Web API from Apps SDK: Changed in v2.0.0

Web API
Usage of the Web API has been limited in apps with the homey:manager:api permission to the following scopes: homey.alarm homey.system.readonly homey.updates.readonly homey.geolocation.readonly homey.zone.readonly homey.device.readonly homey.device.control homey.flow.readonly homey.user.readonly homey.user.self homey.app.readonly homey.app.control homey.presence.readonly homey.insights.readonly homey.logic homey.speech .
Users wishing to use the Web API in their app must include athom-api v2.1 or higher in their app.

Thanks for clarifying that, although it isn’t exactly the anwser I was hoping for :thinking:

Too bad; now I have to make it unnecessary complicated using webhooks and flow cards to get the same result.

Back to the drawing board :slight_smile:

Blockquote
Web API
Usage of the Web API has been limited in apps with the homey:manager:api permission to the following scopes: homey.alarm homey.system.readonly homey.updates.readonly homey.geolocation.readonly homey.zone.readonly homey.device.readonly homey.device.control homey.flow.readonly homey.user.readonly homey.user.self homey.app.readonly homey.app.control homey.presence.readonly homey.insights.readonly homey.logic homey.speech .
Users wishing to use the Web API in their app must include athom-api v2.1 or higher in their app.

Since a lot of stuff is explicitly mentioned as readonly while some or not one would asume that homey.logic is not readonly.

Still I get the same “UnhandledPromiseRejectionWarning: missing_scopes: Je hebt geen toegang om dit te doen.” as with the presence stuff. :angry: Am I missing something here or is the SDK documentation unclear?

TIA,