# Twinkly Lights Control - Brightness HTTP POST

**URL:** <https://discourse.nodered.org/t/twinkly-lights-control-brightness-http-post/37327>\
**Category:** General\
**Created:** [10 December 2020 21:55 UTC](https://discourse.nodered.org/t/twinkly-lights-control-brightness-http-post/37327 "2020-12-10T21:55:39Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brew](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/brew/32/19776_2.png) [@Brew](https://discourse.nodered.org/u/Brew)\
**Post date:** [10 December 2020 21:55 UTC](https://discourse.nodered.org/t/twinkly-lights-control-brightness-http-post/37327/1 "2020-12-10T21:55:39Z")

</div>

Hi there, it's that time of year and adventures in Christmas lights are a plenty, I've got some [twinkly](https://www.twinkly.com/) lights and having taken a bit from this [guide](https://xled.readthedocs.io/en/latest/_modules/xled/control.html) I can turn them on and off.

> import xled  
> control = xled.ControlInterface('10.1.1.115')  
> control.set\_mode('off')  
> return msg

However I'm banging my head as to how to control the brightness there are instructions [here](https://xled-docs.readthedocs.io/en/latest/rest_api.html?highlight=movie#set-brightness) on how to set the brightness but I can't fathom how to lay this out within my flow to assemble the right thing.

Can any one give me some pointers?

> POST /xled/v1/led/out/brightness HTTP/1.1  
> Host: 192.168.4.1  
> X-Auth-Token: 5jPe+ONhwUY=  
> Content-Type: application/json  
> Content-Length: 45  
> {"mode":"enabled","type": "A","value": "100"}

If I pop a HTTP request node with POST and the IP address of my lights in and inject the list line as json I just get a 404 response.

As ever I'm sure it's something simple. Please help.

![Screenshot 2020-12-10 at 21.54.11](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/5/d5e4e5002f0d0268be803fffe4a58bbbef935766.png)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [11 December 2020 07:47 UTC](https://discourse.nodered.org/t/twinkly-lights-control-brightness-http-post/37327/2 "2020-12-11T07:47:59Z")

</div>

Show us how the http request node is configured, and a debug before showing the msg.payload.

---

<div class="post-metadata">

**Author:** ![Brew](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/brew/32/19776_2.png) [@Brew](https://discourse.nodered.org/u/Brew)\
**Post date:** [11 December 2020 10:35 UTC](https://discourse.nodered.org/t/twinkly-lights-control-brightness-http-post/37327/3 "2020-12-11T10:35:57Z")

</div>

![Screenshot 2020-12-11 at 10.14.35](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/7/972def2ff04a5d5126d4c80ec7b079e9e5413216.png)

 ![Screenshot 2020-12-11 at 10.14.53](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/c/fc20ee3b55c9cfa4673c2127de3e99512de80822.png)

![Screenshot 2020-12-11 at 10.23.45](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/6/c6891e03770c428b689e5e021f3c7bef753974b3.png)

Does the attached help?

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [11 December 2020 10:52 UTC](https://discourse.nodered.org/t/twinkly-lights-control-brightness-http-post/37327/4 "2020-12-11T10:52:57Z")

</div>

try the url as  
`http://10.1.1.115/xled/v1/led/out/brightness`

---

<div class="post-metadata">

**Author:** ![Brew](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/brew/32/19776_2.png) [@Brew](https://discourse.nodered.org/u/Brew)\
**Post date:** [11 December 2020 11:18 UTC](https://discourse.nodered.org/t/twinkly-lights-control-brightness-http-post/37327/5 "2020-12-11T11:18:16Z")

</div>

![Screenshot 2020-12-11 at 11.16.59](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/d/0d794454d0f8d69fe6d030208c003619093229e4.png)

Thanks @E1cid I'm a step closer now as I get the invalid token champions! I guess it's now just a case of building all the necessary info into that payload inject

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [11 December 2020 11:30 UTC](https://discourse.nodered.org/t/twinkly-lights-control-brightness-http-post/37327/6 "2020-12-11T11:30:58Z")

</div>

Based on you screenshot you send your "value" : "50" as string in the Json  
what if you send it as integer .. by removing the double quotes around the number

---

<div class="post-metadata">

**Author:** ![Brew](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/brew/32/19776_2.png) [@Brew](https://discourse.nodered.org/u/Brew)\
**Post date:** [11 December 2020 11:38 UTC](https://discourse.nodered.org/t/twinkly-lights-control-brightness-http-post/37327/7 "2020-12-11T11:38:34Z")

</div>

Andy great shout, I think I'm still a step behind that as I need to assemble the message to let me in before I can start giving it direction.

> msg.payload = {  
> 'challenge':'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\a',  
> 'authentication\_token':'5jPe+ONhwUY=',  
> };  
> return msg;

I've dropped a function node in with the above and changed the HTTP request to the picture below, at least I'm now getting a connection.

 ![Screenshot 2020-12-11 at 11.38.16](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/4/f4f949734663e4668b2ea9aabf8ca3f8ee3399b7.png)

I guess I just need to assemble the message correctly now.

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [11 December 2020 12:00 UTC](https://discourse.nodered.org/t/twinkly-lights-control-brightness-http-post/37327/8 "2020-12-11T12:00:38Z")

</div>

In the function you can try to also set the authentication token you received from the Login step with :

`msg.headers["X-Auth-Token"] = "5jPe+ONhwUY=";`

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [11 December 2020 12:02 UTC](https://discourse.nodered.org/t/twinkly-lights-control-brightness-http-post/37327/9 "2020-12-11T12:02:41Z")

</div>

Once you recieve the auth token you will have to send it in the header. for the brightness request.

---

<div class="post-metadata">

**Author:** ![bryzi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bryzi/32/30918_2.png) [@bryzi](https://discourse.nodered.org/u/bryzi)\
**Post date:** [12 December 2020 08:47 UTC](https://discourse.nodered.org/t/twinkly-lights-control-brightness-http-post/37327/10 "2020-12-12T08:47:18Z")

</div>

I wanted to do the same and couldn’t find any useful nodes for this so made my own in the end which may fit your needs. Currently I’ve added power, brightness and a few colour templates...

[https://flows.nodered.org/node/thingzi-logic-twinkly](https://flows.nodered.org/node/thingzi-logic-twinkly)

---

<div class="post-metadata">

**Author:** ![Brew](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/brew/32/19776_2.png) [@Brew](https://discourse.nodered.org/u/Brew)\
**Post date:** [12 December 2020 22:03 UTC](https://discourse.nodered.org/t/twinkly-lights-control-brightness-http-post/37327/11 "2020-12-12T22:03:40Z")

</div>

Well would you look at that, I am embarrassed I didn't stumble across that in my searching for how to achieve this before.

To try and redeem myself I'll try and crack the HTTP request route also.

---

<div class="post-metadata">

**Author:** ![bryzi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bryzi/32/30918_2.png) [@bryzi](https://discourse.nodered.org/u/bryzi)\
**Post date:** [13 December 2020 02:19 UTC](https://discourse.nodered.org/t/twinkly-lights-control-brightness-http-post/37327/12 "2020-12-13T02:19:05Z")

</div>

the twinkly authentication needs a couple of steps which aren't obvious in the docs. this follows the form...

- call the login and supply the challenge
- get authentication\_token & challenge\_resonse from the reply
- 1st usage of the token **must** be to the verify end point, pass challenge\_resonse in body (see docs)
- if all ok you can use the authentication\_token

its a bit convoluted really but there are quite a few examples in github., ive linked one of the simpler ones. It does an extra check before login that you dont specifically need though.

> <https://github.com/linkineo/tinky-twinkly/blob/master/xmas.js>

---

<div class="post-metadata">

**Author:** ![bryzi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bryzi/32/30918_2.png) [@bryzi](https://discourse.nodered.org/u/bryzi)\
**Post date:** [13 December 2020 10:55 UTC](https://discourse.nodered.org/t/twinkly-lights-control-brightness-http-post/37327/13 "2020-12-13T10:55:00Z")

</div>

also note, if you have an existing token i think they are pretty short lived, so unless you are handling expiry you will probably need to do the login on every call. Hence why i made a node 🙂

---

<div class="post-metadata">

**Author:** ![Brew](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/brew/32/19776_2.png) [@Brew](https://discourse.nodered.org/u/Brew)\
**Post date:** [16 December 2020 11:10 UTC](https://discourse.nodered.org/t/twinkly-lights-control-brightness-http-post/37327/14 "2020-12-16T11:10:07Z")

</div>

Success - Thank you all for the help on this and thanks AndyF for the last the final mile.

Just if anyone is interested this is what I've created, I appreciate it is what @bryzi nodes already do, but here it is all the same.

 ![Screenshot 2020-12-16 at 11.02.49](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/5/95f25ea8612a2d674d0ff5fdc3c2239c0724515a.png)

```auto
[{"id":"4167d048.8c39d8","type":"http request","z":"5b9bb1a9.d41be8","name":"","method":"POST","ret":"txt","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","x":470,"y":1520,"wires":[["53729e87.354d78"]]},{"id":"53729e87.354d78","type":"debug","z":"5b9bb1a9.d41be8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":470,"y":1560,"wires":[]},{"id":"b656f4d9.6153b8","type":"function","z":"5b9bb1a9.d41be8","name":"Brightness","func":"//msg.headers = {\"X-Auth-Token\":global.get(\"authtoken\")};\nmsg.payload = {\"mode\":\"enabled\",\"type\": \"A\",\"value\": msg.payload};\nmsg.url= \"http://10.1.1.115/xled/v1/led/out/brightness\"\nreturn msg;\n\n\n//flow.set(\"authtoken\",msg.payload.authentication_token);\n//flow.set(\"authchalrsp\",msg.payload[\"challenge-response\"]);","outputs":1,"noerr":0,"initialize":"","finalize":"","x":290,"y":1500,"wires":[["ad51bc9f.7f2c8"]]},{"id":"5bb6c869.12c728","type":"http request","z":"5b9bb1a9.d41be8","name":"GetToken","method":"POST","ret":"txt","paytoqs":"ignore","url":"http://10.1.1.115/xled/v1/login","tls":"","persist":false,"proxy":"","authType":"","x":240,"y":1380,"wires":[["ec214898.695e28","f369655b.a23338"]]},{"id":"ec214898.695e28","type":"debug","z":"5b9bb1a9.d41be8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":410,"y":1340,"wires":[]},{"id":"5f7b365.bad90c8","type":"function","z":"5b9bb1a9.d41be8","name":"Get token","func":"if (msg.payload === false){return}\nmsg.payload = {\n//msg.headers = {\n'challenge':'AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8=',\n//'authentication_token':'5jPe+ONhwUY=',\n};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":240,"y":1340,"wires":[["5bb6c869.12c728"]]},{"id":"6f9aeb0d.292d44","type":"inject","z":"5b9bb1a9.d41be8","name":"1 hour","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"3600","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":100,"y":1300,"wires":[["8f241eda.4edda"]]},{"id":"dc7507c5.71fd78","type":"http request","z":"5b9bb1a9.d41be8","name":"Verify","method":"POST","ret":"txt","paytoqs":"ignore","url":"http://10.1.1.115/xled/v1/verify","tls":"","persist":false,"proxy":"","authType":"","x":670,"y":1380,"wires":[["4b49608e.4b2b2"]]},{"id":"4b49608e.4b2b2","type":"debug","z":"5b9bb1a9.d41be8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":670,"y":1340,"wires":[]},{"id":"3b20a5e0.ea932a","type":"function","z":"5b9bb1a9.d41be8","name":"SetGlobalVar","func":"global.set(\"authtoken\",msg.payload.authentication_token);\nglobal.set(\"authchalrsp\",msg.payload[\"challenge-response\"]);\n\nmsg.headers = {\"X-Auth-Token\":global.get(\"authtoken\")};\nmsg.payload = {\"challenge-response\":global.get(\"authchalrsp\")};\n\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":530,"y":1380,"wires":[["dc7507c5.71fd78"]]},{"id":"f369655b.a23338","type":"json","z":"5b9bb1a9.d41be8","name":"","property":"payload","action":"","pretty":false,"x":390,"y":1380,"wires":[["3b20a5e0.ea932a"]]},{"id":"eda93b11.1254e8","type":"inject","z":"5b9bb1a9.d41be8","name":"10","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"10","payloadType":"num","x":130,"y":1480,"wires":[["b656f4d9.6153b8"]]},{"id":"2beee86e.4f9d18","type":"inject","z":"5b9bb1a9.d41be8","name":"100","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"100","payloadType":"num","x":130,"y":1520,"wires":[["b656f4d9.6153b8"]]},{"id":"8f241eda.4edda","type":"ping","z":"5b9bb1a9.d41be8","mode":"triggered","name":"","host":"10.1.1.115","timer":"20","inputs":1,"x":240,"y":1300,"wires":[["5f7b365.bad90c8","84e8124a.5f1dd"]]},{"id":"84e8124a.5f1dd","type":"debug","z":"5b9bb1a9.d41be8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":430,"y":1300,"wires":[]},{"id":"ad51bc9f.7f2c8","type":"function","z":"5b9bb1a9.d41be8","name":"SetHeaders","func":"msg.headers = {\"X-Auth-Token\":global.get(\"authtoken\")};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":470,"y":1480,"wires":[["4167d048.8c39d8"]]}]

```

---

<div class="post-metadata">

**Author:** ![bryzi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bryzi/32/30918_2.png) [@bryzi](https://discourse.nodered.org/u/bryzi)\
**Post date:** [16 December 2020 11:41 UTC](https://discourse.nodered.org/t/twinkly-lights-control-brightness-http-post/37327/15 "2020-12-16T11:41:00Z")

</div>

Cool, looks great, nice one.

One small tweak id make but you probably wouldn't notice would be to swap SetGlobalVar and Verify around and only set the global if verify was successful. There is a remote possibility that verify could fail or an event could use the token in the tiny window before verify is called. Probably nothing you would see in practice, thats just my software engineering OCD kicking in 🙂

---

<div class="post-metadata">

**Author:** ![SierraLimaOscar](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sierralimaoscar/32/34090_2.png) [@SierraLimaOscar](https://discourse.nodered.org/u/SierraLimaOscar)\
**Post date:** [22 December 2020 13:15 UTC](https://discourse.nodered.org/t/twinkly-lights-control-brightness-http-post/37327/16 "2020-12-22T13:15:44Z")

</div>

Hi,

Slightly off-topic. I've installed your node and while the ON and OFF commands work, the twinkly light will not switch on back in the "playlist" mode regardless of the fact that it was the mode it was in when switched off using the OFF command.  
If I do the same with an external switch (cut the power to the lights) it will resume correctly when the power comes back on.

P.S. The home-assistant integration suffers from the same issue, the timer in the app however works correctly.

---

<div class="post-metadata">

**Author:** ![Brew](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/brew/32/19776_2.png) [@Brew](https://discourse.nodered.org/u/Brew)\
**Post date:** [23 December 2020 08:28 UTC](https://discourse.nodered.org/t/twinkly-lights-control-brightness-http-post/37327/17 "2020-12-23T08:28:20Z")

</div>

Interesting, I've not had any trouble, my process it to tick the effect and then the sequence resumes. Do you have a single effect or a playlist?

---

<div class="post-metadata">

**Author:** ![SierraLimaOscar](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sierralimaoscar/32/34090_2.png) [@SierraLimaOscar](https://discourse.nodered.org/u/SierraLimaOscar)\
**Post date:** [23 December 2020 11:18 UTC](https://discourse.nodered.org/t/twinkly-lights-control-brightness-http-post/37327/18 "2020-12-23T11:18:58Z")

</div>

Hi,

No it is specific to the playlist mode. If I have just an effect selected, then it comes back to it and everything is fine. However it does not return to the playlist. I think it goes back to the first effect in the playlist and stays there.

---

<div class="post-metadata">

**Author:** ![SierraLimaOscar](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sierralimaoscar/32/34090_2.png) [@SierraLimaOscar](https://discourse.nodered.org/u/SierraLimaOscar)\
**Post date:** [23 December 2020 11:20 UTC](https://discourse.nodered.org/t/twinkly-lights-control-brightness-http-post/37327/19 "2020-12-23T11:20:45Z")

</div>

My workaround is to not switch the lights off, but to switch the brightness to 0 and when I switch them back to 100% the playlist is still going.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [6 January 2021 11:20 UTC](https://discourse.nodered.org/t/twinkly-lights-control-brightness-http-post/37327/20 "2021-01-06T11:20:47Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
