Fading color light not working

I’m completely lost now.
Both kids want RGB bulbs and I figured it would be nice with a remote to control the colors and light and all that.
So bought two Shelly RGB bulbs and two IKEA remotes. The exact same models.

I managed to make a flow that could change colors and all that and two “custom” effects.
One is fading light and one is “disco” light.

Everything works fine on one bulb, but not on the other and I can’t figure out why.

Sorry about the small image, but this is pressing the off button.
Single click is “off”. (the upper sequence from the function node)
double click is fading light ( middle sequence from function node)
tripple clikc is disco (bottom sequence from function node)

[{"id":"1dca530c8a167bbe","type":"delay","z":"a787a77.9c16458","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"0.3","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":true,"allowrate":false,"outputs":1,"x":370,"y":3600,"wires":[["c8d6e2046f5017d0"]]},{"id":"67435f82a00074af","type":"ha-device","z":"a787a77.9c16458","name":"Liten sol klick","server":"4bbca37b.1700ec","version":0,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"inputs":0,"deviceType":"trigger","device":"76af6fdaa2160386fecfe18d1fde51a4","event":{"device_id":"76af6fdaa2160386fecfe18d1fde51a4","domain":"zha","platform":"device","type":"remote_button_short_press","subtype":"turn_off"},"capabilities":[],"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":170,"y":3600,"wires":[["1dca530c8a167bbe"]]},{"id":"c8d6e2046f5017d0","type":"join","z":"a787a77.9c16458","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"1","count":"3","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":530,"y":3600,"wires":[["7f55fdbe23385524"]]},{"id":"7f55fdbe23385524","type":"function","z":"a787a77.9c16458","name":"","func":"if (msg.payload.length == 1){\n    return [msg, null, null];\n}else if (msg.payload.length == 2){\n    return [null, msg, null];\n}else{\n    return [null, null, msg];\n}","outputs":3,"noerr":0,"initialize":"","finalize":"","libs":[],"x":660,"y":3600,"wires":[["b586b518c8f4d5b3","b22e3591da89bb64"],["8cabe05849784fea","edfc51a8634857b4"],["8094e06caf7504b0","55e55a1d7319372d"]]},{"id":"b586b518c8f4d5b3","type":"delay","z":"a787a77.9c16458","name":"","pauseType":"delay","timeout":"100","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":870,"y":3480,"wires":[["350b447d83b2bba1"]]},{"id":"b22e3591da89bb64","type":"link out","z":"a787a77.9c16458","name":"","mode":"link","links":["2c1ecdead88cb71d"],"x":835,"y":3540,"wires":[]},{"id":"8cabe05849784fea","type":"api-current-state","z":"a787a77.9c16458","name":"","server":"4bbca37b.1700ec","version":2,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"light.davids_lampa","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":1000,"y":3600,"wires":[["40ecf90f247ea0bc"],["5d58a7c72c6cb8a3"]]},{"id":"edfc51a8634857b4","type":"change","z":"a787a77.9c16458","name":"","rules":[{"t":"set","p":"disco_david","pt":"flow","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":500,"y":3700,"wires":[[]]},{"id":"8094e06caf7504b0","type":"api-current-state","z":"a787a77.9c16458","name":"","server":"4bbca37b.1700ec","version":2,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"light.davids_lampa","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":940,"y":3700,"wires":[["5fbe07e28102b4da"],["dbfb0412f60c87d5"]]},{"id":"55e55a1d7319372d","type":"change","z":"a787a77.9c16458","name":"","rules":[{"t":"set","p":"fade_david","pt":"flow","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":490,"y":3660,"wires":[[]]},{"id":"350b447d83b2bba1","type":"api-call-service","z":"a787a77.9c16458","name":"","server":"4bbca37b.1700ec","version":3,"debugenabled":false,"service_domain":"light","service":"turn_off","entityId":"light.davids_lampa","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1050,"y":3480,"wires":[[]]},{"id":"40ecf90f247ea0bc","type":"change","z":"a787a77.9c16458","name":"","rules":[{"t":"set","p":"fade_david","pt":"flow","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":1550,"y":3600,"wires":[["f3d56674b48d434d"]]},{"id":"5d58a7c72c6cb8a3","type":"api-call-service","z":"a787a77.9c16458","name":"","server":"4bbca37b.1700ec","version":3,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.davids_lampa","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1310,"y":3640,"wires":[["40ecf90f247ea0bc"]]},{"id":"2c1ecdead88cb71d","type":"link in","z":"a787a77.9c16458","name":"","links":["b22e3591da89bb64","f81723c3b553d2f5","bb7c74e2c95ca857","315840fce90951ab","1d159ca74c236cd6","3d6b35e9800134c3"],"x":255,"y":3680,"wires":[["55e55a1d7319372d","edfc51a8634857b4"]]},{"id":"5fbe07e28102b4da","type":"change","z":"a787a77.9c16458","name":"","rules":[{"t":"set","p":"disco_david","pt":"flow","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":1500,"y":3700,"wires":[["cdde86e74bd4db4e"]]},{"id":"dbfb0412f60c87d5","type":"api-call-service","z":"a787a77.9c16458","name":"","server":"4bbca37b.1700ec","version":3,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.davids_lampa","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1210,"y":3740,"wires":[["5fbe07e28102b4da"]]},{"id":"f3d56674b48d434d","type":"api-current-state","z":"a787a77.9c16458","name":"","server":"4bbca37b.1700ec","version":2,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"light.davids_lampa","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":2100,"y":3600,"wires":[["59d803e748cbb2c3"]]},{"id":"cdde86e74bd4db4e","type":"function","z":"a787a77.9c16458","name":"","func":"\n\nmsg.payload = { data: {\n    'entity_id': 'light.davids_lampa',\n    'hs_color': [Math.ceil(Math.random() * 360), Math.ceil(Math.random()  * (100 - 50) + 50)]\n } };\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1740,"y":3720,"wires":[["90f488bddd8ea9af"]]},{"id":"39d11d8f5198b3f1","type":"switch","z":"a787a77.9c16458","name":"","property":"fade_david","propertyType":"flow","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":2430,"y":3540,"wires":[["f3d56674b48d434d"]]},{"id":"59d803e748cbb2c3","type":"function","z":"a787a77.9c16458","name":"","func":"sat = msg.data.attributes.hs_color[1]\nnode.warn(sat);\nnode.warn(msg.dir);\n\nif (msg.dir == true){\n    if (msg.data.attributes.hs_color[1] > 70){\n        sat = msg.data.attributes.hs_color[1]+1;\n    }else{\n        sat = msg.data.attributes.hs_color[1]+2;\n    }\n    if (sat > 100) sat = 100;\n    msg.dir = true;\n    if (sat == 100) msg.dir = false;\n}else{\n    if(msg.data.attributes.hs_color[1] > 70){\n        sat = msg.data.attributes.hs_color[1]-1;\n    }else{\n        sat = msg.data.attributes.hs_color[1]-2;\n    }\n    if (sat < 40) sat = 40;\n    msg.dir = false;\n    if (sat <= 40) msg.dir = true;\n}\n\nmsg.payload = { data: {\n    'entity_id': 'light.davids_lampa',\n    'hs_color': [(msg.data.attributes.hs_color[0]+3)%360, sat]\n } };\n\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2340,"y":3600,"wires":[["62401d7a17d53bab"]]},{"id":"ec855fb7f1aa9ac3","type":"switch","z":"a787a77.9c16458","name":"","property":"disco_david","propertyType":"flow","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":1830,"y":3660,"wires":[["cdde86e74bd4db4e"]]},{"id":"90f488bddd8ea9af","type":"api-call-service","z":"a787a77.9c16458","name":"","server":"4bbca37b.1700ec","version":3,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.davids_lampa","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1930,"y":3720,"wires":[["f99df91b2cae4226"]]},{"id":"92fd8960bd4ebe8e","type":"delay","z":"a787a77.9c16458","name":"","pauseType":"delay","timeout":"50","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":2750,"y":3600,"wires":[["39d11d8f5198b3f1"]]},{"id":"62401d7a17d53bab","type":"api-call-service","z":"a787a77.9c16458","name":"","server":"4bbca37b.1700ec","version":3,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.davids_lampa","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":2530,"y":3600,"wires":[["92fd8960bd4ebe8e"]]},{"id":"f99df91b2cae4226","type":"delay","z":"a787a77.9c16458","name":"","pauseType":"delay","timeout":"250","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":2150,"y":3720,"wires":[["ec855fb7f1aa9ac3"]]},{"id":"4bbca37b.1700ec","type":"server","name":"Home Assistant","version":1,"legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

As I said all functions work on the other remote and bulb, and I just made a copy, pasted and changed bulb, variables, and remote entities.

I have added a node.warn inside the function node to output what it’s doing.
So the below is after a current state node and in the fade loop. The intention is to keep the saturation above 40 at all the time but vary it up and down, and loop the hs color.

sat = msg.data.attributes.hs_color[1]
node.warn(sat);
node.warn(msg.dir);

if (msg.dir == true){
    if (msg.data.attributes.hs_color[1] > 70){
        sat = msg.data.attributes.hs_color[1]+1;
    }else{
        sat = msg.data.attributes.hs_color[1]+2;
    }
    if (sat > 100) sat = 100;
    msg.dir = true;
    if (sat == 100) msg.dir = false;
}else{
    if(msg.data.attributes.hs_color[1] > 70){
        sat = msg.data.attributes.hs_color[1]-1;
    }else{
        sat = msg.data.attributes.hs_color[1]-2;
    }
    if (sat < 40) sat = 40;
    msg.dir = false;
    if (sat <= 40) msg.dir = true;
}

msg.payload = { data: {
    'entity_id': 'light.davids_lampa',
    'hs_color': [(msg.data.attributes.hs_color[0]+3)%360, sat]
 } };

return msg

And here is the output it the debug window.
The bulb started at 100 saturation and some color. Not sure what it was.
So it starts with saturation 71, even though the bulb was at 100 and then it just drops down far below what it should be (and with much larger steps than the code says it to do) and even though msg.dir becomes true it still goes down.

I just don’t understand it…
Anyone see the error?

2021-12-30 15:00:09node: 59d803e748cbb2c3
function : (warn)
71.765 
2021-12-30 15:00:09node: 59d803e748cbb2c3
function : (warn)
undefined
2021-12-30 15:00:09node: 59d803e748cbb2c3
function : (warn)
50.98
2021-12-30 15:00:09node: 59d803e748cbb2c3
function : (warn)
false
2021-12-30 15:00:09node: 59d803e748cbb2c3
function : (warn)
35.294
2021-12-30 15:00:09node: 59d803e748cbb2c3
function : (warn)
false
2021-12-30 15:00:10node: 59d803e748cbb2c3
function : (warn)
28.627
2021-12-30 15:00:10node: 59d803e748cbb2c3
function : (warn)
true
2021-12-30 15:00:10node: 59d803e748cbb2c3
function : (warn)
22.353
2021-12-30 15:00:10node: 59d803e748cbb2c3
function : (warn)
true
2021-12-30 15:00:10node: 59d803e748cbb2c3
function : (warn)
17.647
2021-12-30 15:00:10node: 59d803e748cbb2c3
function : (warn)
true

I just noticed this entity can’t get the saturation higher than 71. It just won’t go higher.

I managed to solve it.
I had to remove the integration several times and add it again.
Factory reset the bulb twice and integrate it, then it just worked. As soon as the bulb showed saturation 100 then the sequence worked as it should.