Help Needed With Function Node for Ventiltion Fan Control

This is the code I wrote to compare inside and outside temp. I have an error that says my variables are out of scope. I’m not good at this as I’m sure you can see!

if (msg.topic === "sensor.dark_sky_temperature") {
var outside=msg.payload;

 }
if (msg.topic === "sensor.inside_temperature") {
var inside=msg.payload; 
 }
if (inside > outside) {msg.payload="turn_on";
}else {msg.payload="turn_off";
 }
return msg;

Any help would be appreciated.
Clay

Try this with the input from the inside temp as the trigger.

var outside = global.get('homeassistant.homeAssistant.states["sensor.dark_sky_temperature"].state'); 
var inside = msg.payload

if(inside > outside) {
    diff = "on";
} else {
    diff = "off";
}

node.status({ text: diff });

msg.payload = diff; 
return msg;

Thanks for the reply. However it doesn’t seem to work, always sends turn_off whether I set inside temp higher or lower then the outside temp. Perhaps the entire flow would be helpful? (Hope I sent the correct export data.) My original thinking was to have it trigger on either inside or outside temp, but it doesn’t make much difference it should get the job done either way.

FYI, I did try changing the upper case A in homeassistant to lower just to check. No difference. Also changed “on” and “off” to “turn_on” and “turn_off”.

[{"id":"4b836c9f.4c4014","type":"tab","label":"Upstairs Fan Control","disabled":false,"info":""},{"id":"d8929f1c.47bcb","type":"api-call-service","z":"4b836c9f.4c4014","name":"Fan On/Off","server":"dfc47a0d.c63b28","version":3,"debugenabled":false,"service_domain":"switch","service":"","entityId":"switch.02200194ecfabc869339","data":"","dataType":"json","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":430,"y":140,"wires":[["ce89307e.751d1"]]},{"id":"ee00b5b7.a7d4d8","type":"server-state-changed","z":"4b836c9f.4c4014","name":"Outside Temp","server":"dfc47a0d.c63b28","version":3,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"sensor.dark_sky_temperature","entityidfiltertype":"exact","outputinitially":true,"state_type":"num","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":"","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":70,"y":140,"wires":[["f7fae196.28993"]]},{"id":"15635c48.cd77d4","type":"server-state-changed","z":"4b836c9f.4c4014","name":"Inside Temp","server":"dfc47a0d.c63b28","version":3,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"sensor.inside_temperature","entityidfiltertype":"exact","outputinitially":true,"state_type":"num","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":"","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":70,"y":80,"wires":[["f7fae196.28993"]]},{"id":"2989e9a6.ab71d6","type":"server-state-changed","z":"4b836c9f.4c4014","name":"Fan Control","server":"dfc47a0d.c63b28","version":3,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_boolean.fan","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":"","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":70,"y":200,"wires":[["5eec4101.df602","fe4f4af2.c7e618"]]},{"id":"f7fae196.28993","type":"function","z":"4b836c9f.4c4014","name":"","func":"if (msg.topic === \"sensor.dark_sky_temperature\") {\nvar outside=msg.payload;\n\n }\nif (msg.topic === \"sensor.inside_temperature\") {\nvar inside=msg.payload; \n }\nif (inside > outside) {msg.payload=\"turn_on\";\n}else {msg.payload=\"turn_off\";\n }\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":250,"y":80,"wires":[["d695d9f8.f16128","d9c052e1.d38d8"]]},{"id":"5eec4101.df602","type":"traffic","z":"4b836c9f.4c4014","name":"","property_allow":"payload","filter_allow":"on","ignore_case_allow":false,"negate_allow":false,"send_allow":false,"property_stop":"payload","filter_stop":"off","ignore_case_stop":false,"negate_stop":false,"send_stop":false,"default_start":false,"differ":false,"x":270,"y":140,"wires":[["d8929f1c.47bcb"]]},{"id":"d9c052e1.d38d8","type":"change","z":"4b836c9f.4c4014","name":"Payload.Service","rules":[{"t":"move","p":"payload","pt":"msg","to":"payload.service","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":80,"wires":[["5eec4101.df602"]]},{"id":"fe4f4af2.c7e618","type":"switch","z":"4b836c9f.4c4014","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"off","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":300,"y":200,"wires":[["3e89e432.cf55cc"]]},{"id":"3e89e432.cf55cc","type":"api-call-service","z":"4b836c9f.4c4014","name":"Fan Off","server":"dfc47a0d.c63b28","version":3,"debugenabled":false,"service_domain":"switch","service":"turn_off","entityId":"switch.02200194ecfabc869339","data":"","dataType":"json","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":440,"y":200,"wires":[["da3964c4.c678f8"]]},{"id":"e1872c42.a23ea","type":"comment","z":"4b836c9f.4c4014","name":"Upstais Fan Control","info":"","x":90,"y":20,"wires":[]},{"id":"ce89307e.751d1","type":"debug","z":"4b836c9f.4c4014","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":590,"y":140,"wires":[]},{"id":"da3964c4.c678f8","type":"debug","z":"4b836c9f.4c4014","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":590,"y":200,"wires":[]},{"id":"d695d9f8.f16128","type":"debug","z":"4b836c9f.4c4014","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":410,"y":40,"wires":[]},{"id":"dfc47a0d.c63b28","type":"server","name":"Home Assistant-1","version":1,"legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

The problem is that you are not saving the variables anywhere for them to be compared. You should be able to use the autogen entries from ha. Could you just try this flow and post the debug output, the A is supposed to be capitalized.

[{"id":"95fc293b.362e78","type":"inject","z":"7d105d71.cc18e4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":470,"y":3000,"wires":[["e109960f.37c65"]]},{"id":"e109960f.37c65","type":"function","z":"7d105d71.cc18e4","name":"","func":"var value = global.get('homeassistant.homeAssistant.states[\"sensor.dark_sky_temperature\"].state');\nmsg.payload = value;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":730,"y":3000,"wires":[["36b67073.997948"]]},{"id":"36b67073.997948","type":"debug","z":"7d105d71.cc18e4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1020,"y":3000,"wires":[]}]

edit: If you get an error please look in the global menu for the path to darksky. The hamburger menu… under global… hit refresh icon… homeassistant… states… darksky value… copy path.

Here’s the path:

[“homeAssistant-1”].states[“sensor.dark_sky_temperature”]

The problem was the path, it’s working now!

Thanks!