Node Red Function to Compare Two Temperatures (Numbers) then send message

I’m trying to create a Flow where I obtain the current temperature of the meat on my smoker and compare it to an input_number boolean input into HA.

Eventually, I would like the flow to warn me when the current temperature approaches the input_number boolean, but first ‘baby steps’. Right now I cannot get the function to trigger on the following Flow:

[

{"id":"233eb56d27d9ba5e","type":"api-call-service","z":"f1e719e48475ba92","name":"Notify Justin","server":"6e751b1b.8f17c4","version":5,"debugenabled":false,"domain":"notify","service":"mobile_app_justin_iphone_14_pro","areaId":[],"deviceId":[],"entityId":[],"data":"{\t   \"message\": \"{{payload}}\",\t   \"title\": \"Meat Temperature\"\t}","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1130,"y":2600,"wires":[[]]},{"id":"8e1787cd31ce39de","type":"function","z":"f1e719e48475ba92","name":"function 1","func":"var sensorValue = msg.payload; // Assuming sensor value is in msg.payload\nvar targetValue = flow.get(\"meat_target\"); // Assuming input_number value is stored in flow context variable named \"meat_target\"\n\nif (sensorValue === undefined || targetValue === undefined) {\n    return null; // Ignore the message if either value is undefined\n}\n\nif (sensorValue < targetValue) {\n    // Temperature is greater than the target, send a notification\n    msg.payload = \"Temperature is too high. Take action!\";\n    return msg;\n} else {\n    // Temperature is within the acceptable range, do nothing\n    return null;\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":720,"y":2600,"wires":[["233eb56d27d9ba5e","7f2c898f0e2d93ad"]]},{"id":"82c1097ed5dab1e1","type":"api-current-state","z":"f1e719e48475ba92","name":"Meat Current Temp","server":"6e751b1b.8f17c4","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"sensor.meat_temperature","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"meat_target","propertyType":"flow","value":"","valueType":"entityState"},{"property":"data","propertyType":"flow","value":"","valueType":"entityState"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":530,"y":2600,"wires":[["8e1787cd31ce39de"]]},{"id":"7f2c898f0e2d93ad","type":"debug","z":"f1e719e48475ba92","name":"debug 46","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":880,"y":2560,"wires":[]},{"id":"916910d4bb0a4bbf","type":"api-current-state","z":"f1e719e48475ba92","name":"Meat Target","server":"6e751b1b.8f17c4","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"input_number.meat_target","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"meat_target","propertyType":"flow","value":"","valueType":"entityState"},{"property":"data","propertyType":"flow","value":"","valueType":"entityState"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":330,"y":2600,"wires":[["82c1097ed5dab1e1","7f2c898f0e2d93ad"]]},{"id":"580d48305f29f860","type":"server-state-changed","z":"f1e719e48475ba92","name":"Meat Temperature","server":"6e751b1b.8f17c4","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"sensor.meat_temperature","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"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":150,"y":2600,"wires":[["916910d4bb0a4bbf"]]},{"id":"9bcee8aa4735b5c3","type":"inject","z":"f1e719e48475ba92","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":380,"y":2520,"wires":[["916910d4bb0a4bbf"]]},{"id":"6e751b1b.8f17c4","type":"server","name":"Home Assistant","version":5,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30,"areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m","enableGlobalContextStore":true}]

!

NB I recognize that I reversed the greater than sign within the function as it made testing easier.

What is incorrect about the function and/or flow that will not forward msg.payload.

Firstly, you have not marked the code correctly, so you can’t copy/paste into NR (the opening [ is not in the code, and it includes ! for some reason).

Your two current state nodes both store the state in flow.meat_target, which is not what you’d want.

You can do what you want without the function node. If the current state node for “meat target” stores the state in “msg.target_state” (no need for it to be a flow context variable), then the current state node for “meat current temp” can just check “> msg.target_state” within the node. You would also have to change “state type” to number for both nodes. Something along the lines of:

image

You can delete all of the unused output properties other than entity state on the “meat target” current state node.