Function Node for startTime in Time Range Node

I've got a temp sensor that i want to use to control electric blankets. My flow looks like the following;

Flow

My issues appears to be i can't get the Temp function node to output the correct output to input into the Time Range Switch.

Any ideas?

I think you have set some of your 'test-conditions' set incorrectly.
As it is, you should get an output if 'temp' is less than or equal to 12, or EXACTLY 13 or EXACTLY 14, etc..

I think you need to change it to <= 13, <= 14 etc..

I also think the last test-condition should be >17 (rather than >18) otherwise you'll have a gap in your range.

Thanks dynamicdave. Good pickup. Still doesnt seem to be able to parse the startTime through to the Time Range Switch...

OK - I suggest you put a debug node after the function node so you can see what's coming out.
Also I would change line-21 to... return msg;

Could you post your flow, as people could check-it-out for you?

(post deleted by author)

Your flow & JSON are invalid.

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote ```)

``` 
   code goes here 
```

You can edit and correct your post by clicking the pencil :pencil2: icon.

See this post for more details - How to share code or flow json

1 Like
[{"id":"98119225.3816a8","type":"tab","label":"Electric Blankets","disabled":false,"info":""},{"id":"4dcb3ffb.74026","type":"inject","z":"98119225.3816a8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"*/15 18-20 * * *","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":210,"y":740,"wires":[["8e0ff38.ee7781"]]},{"id":"8e0ff38.ee7781","type":"api-current-state","z":"98119225.3816a8","name":"Ambient Temp","server":"dfd95e6a.ada1","version":2,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"sensor.ble_temperature_a4c13880c2c5","state_type":"num","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"}],"override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":280,"y":880,"wires":[["8c1a35a1.dc082","211d5742.10fba8"]]},{"id":"8c1a35a1.dc082","type":"function","z":"98119225.3816a8","name":"Temp Function","func":"let temp = msg.payload;\nif (temp <= 12) { \n x = '18:00'\n} else if ( temp <= 13) { \n x = '18:30'\n} else if ( temp <= 14) { \n x = '19:00'\n} else if ( temp <= 15) {\n x = '19:30'\n} else if ( temp <= 16) {\n x = '20:00'\n} else if ( temp <= 17) {\n x = '20:30'\n} else if ( temp > 17) {\n x = '21:14'\n}\nmsg.__config = {\n startTime : x\n }\n\nreturn msg.payload;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":,"x":480,"y":880,"wires":[["c2443f28.b41fa","6fada0b5.e02b48"]]},{"id":"dfc6aa43.c94718","type":"api-call-service","z":"98119225.3816a8","name":"Turn off Electric Bed Blanket","server":"dfd95e6a.ada1","version":3,"debugenabled":false,"service_domain":"switch","service":"turn_off","entityId":"switch.bedroom_indi_side, switch.bedroom_damien_side","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":,"queue":"none","x":1020,"y":1020,"wires":[]},{"id":"7e901c50.6ef704","type":"api-call-service","z":"98119225.3816a8","name":"Turn on Electric Bed Blanket","server":"dfd95e6a.ada1","version":3,"debugenabled":false,"service_domain":"switch","service":"turn_on","entityId":"switch.bedroom_indi_side, switch.bedroom_damien_side","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":,"queue":"none","x":1020,"y":880,"wires":[]},{"id":"c2443f28.b41fa","type":"debug","z":"98119225.3816a8","name":"payload1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":720,"y":760,"wires":},{"id":"8fee313c.d8f738","type":"debug","z":"98119225.3816a8","name":"payload 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":960,"y":760,"wires":},{"id":"6fada0b5.e02b48","type":"time-range-switch","z":"98119225.3816a8","name":"","lat":"","lon":"","startTime":"","endTime":"21:15","startOffset":0,"endOffset":0,"x":680,"y":920,"wires":[["7e901c50.6ef704","8fee313c.d8f738"],["dfc6aa43.c94718"]]},{"id":"211d5742.10fba8","type":"debug","z":"98119225.3816a8","name":"temp","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":510,"y":660,"wires":},{"id":"dfd95e6a.ada1","type":"server","name":"Home Assistant","version":1,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

As you are using Home Assistant nodes most people will not be able to import the flows to see what the issue is

Can i suggest a different approach ?

Install within time · rdmtc/node-red-contrib-sun-position Wiki · GitHub

The Within time node will allow you to have multiple nodes that cascade - if the time in one node meets the condition is outputs a payload on output 1 - if not it cascades down to the 2nd output where you can do your next test etc.

Once you have this working you may decide you like a function node better - but this will be easier to maintain etc

BTW i can not import the latest flow either gives a JSON error

Craig

1 Like

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