Node Red Function Problem

I’m having trouble creating a function with two outputs. One output is to pass the message along if output of weather.thermostat value is “exceptional” or “sunny” or “partlycloudy”. If it’s not one of these three values then the other output will pass the message along. Unfortunately the formula I’ve created isn’t working correctly. Todays weather is “cloudy”. Debug nodes indicate this value, yet when evaluated by the Function Node the result is incorrect.

I’m using a Current State node to determine weather.thermostat. A debug node on the output of this node indicates it’s “cloudy”. When this value is passed to the function for evaluation, the function node evaluates as true even though none of three conditions are met. Code under the On Message tab of the function reads;

if ((msg.payload === "exceptional") || (msg.payload === "sunny") || (msg.payload === "partlycloudy")) {
    return [null, msg];
}
return [msg, null];

I’m not familiar with Node Red coding thus suspect the formula I’ve created is incorrect.

Please advise how I may change the code to evaluate what I’m attempting to achieve.

Just in case it’s needed, here is the flow where the Function is being evaluated;

[{"id":"3396837c.86304c","type":"tab","label":"Morning Wakeup Routine","disabled":false,"info":""},{"id":"9f625be6.049908","type":"server-state-changed","z":"3396837c.86304c","name":"Start Morning Routine","server":"fcf9b76b.256a88","version":3,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_boolean.morning_routine","entityidfiltertype":"exact","outputinitially":true,"state_type":"habool","haltifstate":"true","halt_if_type":"bool","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":160,"y":140,"wires":[["6fb9a82c.7a5148","5f9bf701.688538","587e2e15.5938"],[]]},{"id":"b6e0493c.5e3a08","type":"api-call-service","z":"3396837c.86304c","name":"Turn desk lamp on","server":"fcf9b76b.256a88","version":3,"debugenabled":false,"service_domain":"switch","service":"turn_on","entityId":"switch.desk_lamp","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":970,"y":340,"wires":[["ae67da4f.db8d88"]]},{"id":"8b3eaa37.d13b98","type":"comment","z":"3396837c.86304c","name":"Start Morning Routine","info":"","x":160,"y":80,"wires":[]},{"id":"6fb9a82c.7a5148","type":"time-range-switch","z":"3396837c.86304c","name":"Is sun above horizon?","lat":"43.388451","lon":"-80.994888","startTime":"sunrise","endTime":"sunsetStart","startOffset":"75","endOffset":"30","x":460,"y":280,"wires":[["36f4ec5a.b764a4"],["b6e0493c.5e3a08"]],"outputLabels":["Yes","No"]},{"id":"d2204a6.d48b2b8","type":"api-call-service","z":"3396837c.86304c","name":"Turn desk lamp off","server":"fcf9b76b.256a88","version":3,"debugenabled":false,"service_domain":"switch","service":"turn_off","entityId":"switch.desk_lamp","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1410,"y":260,"wires":[["eca9e7af.c4ea68"]]},{"id":"ae67da4f.db8d88","type":"stoptimer","z":"3396837c.86304c","duration":"15","units":"Minute","payloadtype":"num","payloadval":"0","name":"Wait 15 minutes then recheck sun conditions.","x":1290,"y":340,"wires":[["6fb9a82c.7a5148"],[]]},{"id":"5f9bf701.688538","type":"delay","z":"3396837c.86304c","name":"Reset Morning Routine in 5 sec","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":490,"y":140,"wires":[["a866f0a.d24491"]]},{"id":"a866f0a.d24491","type":"api-call-service","z":"3396837c.86304c","name":"Reset Morning Routine","server":"fcf9b76b.256a88","version":3,"debugenabled":false,"service_domain":"input_boolean","service":"turn_off","entityId":"input_boolean.morning_routine","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":780,"y":140,"wires":[[]]},{"id":"4b323fbf.c2ef","type":"sonos-universal","z":"3396837c.86304c","confignode":"ab05293e.ef1178","command":"player.set.volume","state":"25","stateType":"str","avoidCheckPlayerAvailability":false,"name":"Set Breakfast Volume","x":780,"y":200,"wires":[["8b1ca4ab.dcd448"]]},{"id":"587e2e15.5938","type":"sonos-universal","z":"3396837c.86304c","confignode":"ab05293e.ef1178","command":"household.separate.group","state":"","stateType":"str","avoidCheckPlayerAvailability":false,"name":"Leave Group - Breakfast","x":470,"y":200,"wires":[["4b323fbf.c2ef"]]},{"id":"8b1ca4ab.dcd448","type":"sonos-universal","z":"3396837c.86304c","confignode":"ab05293e.ef1178","command":"group.play.tunein","state":"s20305","stateType":"str","avoidCheckPlayerAvailability":false,"name":"Play CBC Radio One Toronto","x":1060,"y":200,"wires":[[]]},{"id":"eca9e7af.c4ea68","type":"change","z":"3396837c.86304c","name":"Stop Timer","rules":[{"t":"set","p":"payload","pt":"msg","to":"STOP","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1630,"y":260,"wires":[["ae67da4f.db8d88"]]},{"id":"3dfe91d5.04c4ae","type":"link in","z":"3396837c.86304c","name":"Stop Desk Lmap Timer","links":["f3959d27.4647a"],"x":1475,"y":200,"wires":[["eca9e7af.c4ea68"]],"info":"If weather condition is not sunny when then the Suns condition will be continueosly checked unless the timer that performs the recheck is stopped when house lights are turned on at scheduled time. "},{"id":"a85a3108.66f59","type":"function","z":"3396837c.86304c","name":"Do weather conditions necessitate lighting?","func":"if ((msg.payload === \"exceptional\") || (msg.payload === \"sunny\") || (msg.payload === \"partlycloudy\")) {\n    return [null, msg];\n}\nreturn [msg, null];","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1050,"y":260,"wires":[["d2204a6.d48b2b8","54ab97bb.871648"],["b6e0493c.5e3a08","4aa167ae.0c5f18"]],"outputLabels":["No","Yes"],"info":"The weather platform only knows the below listed conditions. The reason for this is that for these conditions is an icon from Material Design Icons available and mapped in the frontend.\r\n\r\n‘clear-night’\r\n‘cloudy’\r\n‘fog’\r\n‘hail’\r\n‘lightning’\r\n‘lightning-rainy’\r\n‘partlycloudy’\r\n‘pouring’\r\n‘rainy’\r\n‘snowy’\r\n‘snowy-rainy’\r\n‘sunny’\r\n‘windy’\r\n‘windy-variant’\r\n‘exceptional’\r\n\r\n\r\n\r\ncondition: state\r\nentity_id: weather.thermostat\r\nattribute: forecast\r\nstate: sunny"},{"id":"41670de0.7f11c4","type":"inject","z":"3396837c.86304c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":170,"y":380,"wires":[["6fb9a82c.7a5148"]]},{"id":"54ab97bb.871648","type":"debug","z":"3396837c.86304c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1330,"y":100,"wires":[]},{"id":"4aa167ae.0c5f18","type":"debug","z":"3396837c.86304c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1370,"y":140,"wires":[]},{"id":"36f4ec5a.b764a4","type":"api-current-state","z":"3396837c.86304c","name":"Current Forecast","server":"fcf9b76b.256a88","version":2,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"weather.thermostat","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"x":730,"y":260,"wires":[["474fe0bf.0f2ec","a85a3108.66f59"]]},{"id":"474fe0bf.0f2ec","type":"debug","z":"3396837c.86304c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1220,"y":660,"wires":[]},{"id":"fcf9b76b.256a88","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},{"id":"ab05293e.ef1178","type":"sonos-config","name":"Breakfast","serialnum":"","ipaddress":"192.168.86.84"}]

TIA

I imported your flow and I think you may just have the array in the wrong order? You labelled the node “Do weather conditions necessitate lighting?” and the top output is labelled “No”, the lower one is labelled “Yes”. Based on what you’re doing it seems like you want to send the message to the “No” output when its nice out (i.e. weather is exceptional, sunny or partlycloudy) and to the “Yes” output otherwise because its not bright enough out.

But your function code is reversing that order. The first element of the array you return corresponds to the topmost output, 2nd to the lower output. So I think your function should just be like this:

if ((msg.payload === "exceptional") || (msg.payload === "sunny") || (msg.payload === "partlycloudy")) {
    return [msg, null];
}
return [null, msg];

Seems that was the case! Changed the outputs around and now lights stay on as expected.

Thanks, Mike

1 Like