# Alternating or random brightness

**URL:** <https://discourse.nodered.org/t/alternating-or-random-brightness/18704>\
**Category:** General\
**Created:** [2 December 2019 18:35 UTC](https://discourse.nodered.org/t/alternating-or-random-brightness/18704 "2019-12-02T18:35:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mdelandgraaf](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mdelandgraaf/32/13989_2.png) [@mdelandgraaf](https://discourse.nodered.org/u/mdelandgraaf)\
**Post date:** [2 December 2019 18:35 UTC](https://discourse.nodered.org/t/alternating-or-random-brightness/18704/1 "2019-12-02T18:35:27Z")

</div>

Hi All!

I've got two lights connected via Home assistant to Node-Red. They are dimmable ikea lights and are inside a christmas star. I want the lights to change brightness subtile, alternating, to mimic real star light. They do not have to alternate simultanious, they can work independent from each other to make it more natural.  
Any idea how i can make this work? Any help to point me in the correct direction is appreciated.  
Thanks  
Mike

---

<div class="post-metadata">

**Author:** ![Mobile\_NR\_Env](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@Mobile\_NR\_Env](https://discourse.nodered.org/u/Mobile_NR_Env)\
**Post date:** [10 October 2020 14:50 UTC](https://discourse.nodered.org/t/alternating-or-random-brightness/18704/2 "2020-10-10T14:50:20Z")

</div>

Just in time for this xmas! lol

One way of using payloads to change Home Assistant Node-Red lighting entities. Brightness etc...

(Using a random and template node)

If you take a look inside the template node you can alter the transition times to make it more of a snap or fade change.

Any attribute of the light can be changed inside this node: RGB etc....  
I've setup random lighting level changes (between 50 and 100%) for two lights in the attached flow.

A place to start!  
Just adjust the HA nodes with your lighting entities and deploy.

```auto
[{"id":"f92e782e.404ad","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"d4cabfb3.1e922","type":"template","z":"f92e782e.404ad","name":"Payload/transittion time","field":"payload","fieldType":"msg","format":"json","syntax":"mustache","template":"{\"brightness_pct\":{{payload}},\"transition\":1}","output":"json","x":730,"y":200,"wires":[["aead64eb.d37938"]]},{"id":"f4499f26.94f5d","type":"inject","z":"f92e782e.404ad","name":"2 second inject","props":[{"p":"payload"}],"repeat":"2","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":120,"y":160,"wires":[["aa01ee28.fed7","536b1e06.c058e"]]},{"id":"3e6835b2.1b8eb2","type":"api-call-service","z":"f92e782e.404ad","name":"Light 1","server":"2cdd61c7.88dc4e","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.firefly_lights","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1130,"y":200,"wires":[[]]},{"id":"aead64eb.d37938","type":"change","z":"f92e782e.404ad","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"payload.data","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":950,"y":200,"wires":[["3e6835b2.1b8eb2"]]},{"id":"aa01ee28.fed7","type":"random","z":"f92e782e.404ad","name":"","low":"50","high":"100","inte":"true","property":"payload","x":320,"y":100,"wires":[["d4cabfb3.1e922"]]},{"id":"834b9089.c4d178","type":"template","z":"f92e782e.404ad","name":"Payload/transittion time","field":"payload","fieldType":"msg","format":"json","syntax":"mustache","template":"{\"brightness_pct\":{{payload}},\"transition\":0.5}","output":"json","x":730,"y":260,"wires":[["4a38c175.217878"]]},{"id":"175d85c1.f458fa","type":"api-call-service","z":"f92e782e.404ad","name":"Light 2","server":"2cdd61c7.88dc4e","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.firefly_lights","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1130,"y":260,"wires":[[]]},{"id":"4a38c175.217878","type":"change","z":"f92e782e.404ad","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"payload.data","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":950,"y":260,"wires":[["175d85c1.f458fa"]]},{"id":"536b1e06.c058e","type":"random","z":"f92e782e.404ad","name":"","low":"50","high":"100","inte":"true","property":"payload","x":320,"y":160,"wires":[["834b9089.c4d178"]]},{"id":"8630af70.c555b","type":"inject","z":"f92e782e.404ad","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"10","payloadType":"num","x":390,"y":260,"wires":[["d4cabfb3.1e922","834b9089.c4d178"]]},{"id":"36a09c1.f608464","type":"inject","z":"f92e782e.404ad","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"100","payloadType":"num","x":390,"y":300,"wires":[["834b9089.c4d178","d4cabfb3.1e922"]]},{"id":"6de2eee6.36ed9","type":"comment","z":"f92e782e.404ad","name":"Auto 2 second inject","info":"","x":110,"y":100,"wires":[]},{"id":"5f5e1f45.e48f3","type":"comment","z":"f92e782e.404ad","name":"Or inject brightness via payload manually or another node","info":"","x":230,"y":220,"wires":[]},{"id":"2cdd61c7.88dc4e","type":"server","z":"","name":"Home Assistant- Raspberry"}]

```

---

<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 October 2020 10:20 UTC](https://discourse.nodered.org/t/alternating-or-random-brightness/18704/3 "2020-10-11T10:20:15Z")

</div>

Here is my Twinkle, using standard nodes.

```auto
[{"id":"a412177f.82c2e8","type":"inject","z":"7760f563.ea0324","name":"2 second inject","props":[{"p":"payload"},{"p":"delay","v":"3000","vt":"num"},{"p":"brightness_pct","v":"[50,100]","vt":"json"}],"repeat":"1","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"{}","payloadType":"json","x":160,"y":780,"wires":[["2268f397.91efe4","523dd43e.9bded4"]]},{"id":"2268f397.91efe4","type":"change","z":"7760f563.ea0324","name":"","rules":[{"t":"set","p":"delay","pt":"msg","to":"$round($random() * delay)","tot":"jsonata"},{"t":"set","p":"payload.data","pt":"msg","to":"{\t \"brightness_pct\": (\t $round(\t $random() * (brightness_pct[1] - brightness_pct[0])\t ) + brightness_pct[0]\t ),\t \"transition\":1\t}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":430,"y":860,"wires":[["40a45f5f.40974"]]},{"id":"523dd43e.9bded4","type":"change","z":"7760f563.ea0324","name":"","rules":[{"t":"set","p":"delay","pt":"msg","to":"$round($random() * delay)","tot":"jsonata"},{"t":"set","p":"payload.data","pt":"msg","to":"{\t \"brightness_pct\": (\t $round(\t $random() * (brightness_pct[1] - brightness_pct[0])\t ) + brightness_pct[0]\t ),\t \"transition\":1\t}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":410,"y":900,"wires":[["aff72da.382ead"]]},{"id":"aff72da.382ead","type":"delay","z":"7760f563.ea0324","name":"","pauseType":"delayv","timeout":"5","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":600,"y":900,"wires":[["cffa6bdd.b3836"]]},{"id":"40a45f5f.40974","type":"delay","z":"7760f563.ea0324","name":"","pauseType":"delayv","timeout":"5","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":620,"y":860,"wires":[["cffa6bdd.b3836"]]},{"id":"cffa6bdd.b3836","type":"debug","z":"7760f563.ea0324","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.data.brightness_pct","targetType":"msg","statusVal":"","statusType":"auto","x":870,"y":860,"wires":[]}]

```
