# Help with a function

**URL:** <https://discourse.nodered.org/t/help-with-a-function/41686>\
**Category:** General\
**Created:** [26 February 2021 17:14 UTC](https://discourse.nodered.org/t/help-with-a-function/41686 "2021-02-26T17:14:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![877](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/877/32/8207_2.png) [@877](https://discourse.nodered.org/u/877)\
**Post date:** [26 February 2021 17:14 UTC](https://discourse.nodered.org/t/help-with-a-function/41686/1 "2021-02-26T17:14:48Z")

</div>

I would like to write a function that does the following:

Filters messages from topic `zigbee2mqtt/Hue_dimmer_main_bedroom` on payload.action = "off\_press"

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/1/c1b011cf0cf4492be5d8d6e0234fe51836830b6a.png)

For each matching received message, the function sends one of the following msg.payload on topic `zigbee2mqtt/Hue_light_main_bedroom` :

```auto
{"color_temp": "coolest", "transition":0.5}
{"color_temp": "cool", "transition":0.5}
{"color_temp": "neutral", "transition":0.5}
{"color_temp": "warm", "transition":0.5}
{"color_temp": "warmest", "transition":0.5}

```

So each click of the button cycles through from coolest to warmest, then starts again at coolest.

Any tips would be appreciated.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [26 February 2021 18:26 UTC](https://discourse.nodered.org/t/help-with-a-function/41686/2 "2021-02-26T18:26:27Z")

</div>

here you go...

```auto
var opts = [
    {"color_temp": "coolest", "transition":0.5},
    {"color_temp": "cool", "transition":0.5},
    {"color_temp": "neutral", "transition":0.5},
    {"color_temp": "warm", "transition":0.5},
    {"color_temp": "warmest", "transition":0.5}
];

var opt = context.get("opt") || 0;
msg.payload = opts[opt];

opt++;
if(opt >= opts.length) opt = 0;
context.set("opt", opt); 

return msg;

```

demo flow...

```auto
[{"id":"f1e7cf45.48393","type":"inject","z":"b872cb4b.5a6448","name":"fake mqtt","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":800,"y":240,"wires":[["43f0731d.f5780c"]]},{"id":"43f0731d.f5780c","type":"function","z":"b872cb4b.5a6448","name":"","func":"var opts = [\n {\"color_temp\": \"coolest\", \"transition\":0.5},\n {\"color_temp\": \"cool\", \"transition\":0.5},\n {\"color_temp\": \"neutral\", \"transition\":0.5},\n {\"color_temp\": \"warm\", \"transition\":0.5},\n {\"color_temp\": \"warmest\", \"transition\":0.5}\n];\n\nvar opt = context.get(\"opt\") || 0;\nmsg.payload = opts[opt];\n\nopt++;\nif(opt >= opts.length) opt = 0;\ncontext.set(\"opt\", opt); \n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":960,"y":240,"wires":[["c54e199.d982fe8"]]},{"id":"c54e199.d982fe8","type":"debug","z":"b872cb4b.5a6448","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1140,"y":240,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![877](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/877/32/8207_2.png) [@877](https://discourse.nodered.org/u/877)\
**Post date:** [26 February 2021 19:16 UTC](https://discourse.nodered.org/t/help-with-a-function/41686/3 "2021-02-26T19:16:48Z")

</div>

That's great thank you, I did not expect an answer so soon! It works!

Do I understand this correctly:

You store the variables in a table (?)  
var opt = context.get("opt") || 0; (don't understand, why OR 0 ?)  
Set msg.payload

Increment by one  
If length is greater than table entries, reset to 0  
Set opt value as opt

return the payload..

I'm sure I got a lot wrong there! 🙃

I've never used context.set / context.get.

---

<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:** [12 March 2021 19:46 UTC](https://discourse.nodered.org/t/help-with-a-function/41686/5 "2021-03-12T19:46:19Z")

</div>

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