Extract string from list with a function node

Hi,

I don't know how to search for this question that I have so I'll ask. If there already is a post about this let me know.

I would like to have a nodered setup were I can have a list of items and based on the number that is passed through it will send that list item in my payload. For example:

var list = [effect1, effect2, effect3, effect4]

if (msg.count === 1) {
    msg.payload = { "effect": "#list_item1" }
    return msg;
}

if (msg.count === 2) {
    msg.payload = { "effect": "#list_item2" }
    return msg;

}

if (msg.count === 3) {
    msg.payload = { "effect": "#list_item3" }
    return msg;

}

This will then passthrough a home assistant node that calls a service. For example:

[{"id":"023769838aa17645","type":"function","z":"ab6ef931.883208","name":"effect list","func":"\nvar list = [effect1, effect2, effect3, effect4]\n\nif (msg.count === 1) {\n    msg.payload = { \"effect\": \"#list_item1\" }\n    return msg;\n}\n\nif (msg.count === 2) {\n    msg.payload = { \"effect\": \"#list_item2\" }\n    return msg;\n\n}\n\nif (msg.count === 3) {\n    msg.payload = { \"effect\": \"#list_item3\" }\n    return msg;\n\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":720,"y":580,"wires":[["03a9de00a40cc8d0"]]},{"id":"bb0251f6073343ae","type":"function","z":"ab6ef931.883208","name":"Last -1","func":"msg.decrement = 1;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":440,"y":560,"wires":[["3911f3ded3b1c024"]]},{"id":"836e34c650d67da1","type":"function","z":"ab6ef931.883208","name":"Next +1","func":"msg.increment = 1;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":440,"y":600,"wires":[["3911f3ded3b1c024"]]},{"id":"3911f3ded3b1c024","type":"counter","z":"ab6ef931.883208","name":"","init":"0","step":1,"lower":null,"upper":null,"mode":"increment","outputs":"1","x":580,"y":580,"wires":[["023769838aa17645"]]},{"id":"5773db3243e9f741","type":"inject","z":"ab6ef931.883208","name":"-1","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":280,"y":560,"wires":[["bb0251f6073343ae"]]},{"id":"c9f7b959181fc212","type":"inject","z":"ab6ef931.883208","name":"+1","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":280,"y":600,"wires":[["836e34c650d67da1"]]},{"id":"03a9de00a40cc8d0","type":"api-call-service","z":"ab6ef931.883208","name":"change effect","server":"4fc31d47.aa1574","version":3,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.ledstrip_keuken_wled","data":"{{ msg.payload }}","dataType":"jsonata","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":880,"y":580,"wires":[[]]},{"id":"4fc31d47.aa1574","type":"server","name":"Home Assistant","legacy":false,"addon":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

If this is not clear for you I am sorry I do not know how to I can explain it more.
Thanks in advance!

something like below maybe.

const list = ["effect1", "effect2", "effect3", "effect4"];
msg.payload = {"effect": list[msg.count -1 ]};
return msg;

msg.count has to be 1 to 4

That worked thanks!

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