# Function Node for startTime in Time Range Node

**URL:** https://discourse.nodered.org/t/function-node-for-starttime-in-time-range-node/62289
**Category:** Developing Nodes
**Tags:** function-node
**Created:** [6 May 2022 10:55 UTC](https://discourse.nodered.org/t/function-node-for-starttime-in-time-range-node/62289 "2022-05-06T10:55:30Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![theotherd](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/theotherd/32/61182_2.png) [@theotherd](https://discourse.nodered.org/u/theotherd)
#### Post date: [6 May 2022 10:55 UTC](https://discourse.nodered.org/t/function-node-for-starttime-in-time-range-node/62289/1 "2022-05-06T10:55:30Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/d/3dd5bf19b0a454102351f92324f201f35434a49f.png)

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.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/b/8bf4b1f94dc475412b58826e2d45592ce4d0781e.png)

Any ideas?

---

<div class="post-metadata">

### Author: ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)
#### Post date: [6 May 2022 11:19 UTC](https://discourse.nodered.org/t/function-node-for-starttime-in-time-range-node/62289/2 "2022-05-06T11:19:05Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![theotherd](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/theotherd/32/61182_2.png) [@theotherd](https://discourse.nodered.org/u/theotherd)
#### Post date: [6 May 2022 11:41 UTC](https://discourse.nodered.org/t/function-node-for-starttime-in-time-range-node/62289/3 "2022-05-06T11:41:34Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/d/6d8d52212da5437b8c4803038329699ef8f5ff10.png)

---

<div class="post-metadata">

### Author: ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)
#### Post date: [6 May 2022 12:13 UTC](https://discourse.nodered.org/t/function-node-for-starttime-in-time-range-node/62289/4 "2022-05-06T12:13:02Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![theotherd](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/theotherd/32/61182_2.png) [@theotherd](https://discourse.nodered.org/u/theotherd)
#### Post date: [6 May 2022 21:49 UTC](https://discourse.nodered.org/t/function-node-for-starttime-in-time-range-node/62289/5 "2022-05-06T21:49:45Z")

</div>

(post deleted by author)

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [6 May 2022 22:05 UTC](https://discourse.nodered.org/t/function-node-for-starttime-in-time-range-node/62289/8 "2022-05-06T22:05:47Z")

</div>

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 `````)

````auto
``` 
   code goes here 
```

````

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

See this post for more details - [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

---

<div class="post-metadata">

### Author: ![theotherd](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/theotherd/32/61182_2.png) [@theotherd](https://discourse.nodered.org/u/theotherd)
#### Post date: [6 May 2022 22:25 UTC](https://discourse.nodered.org/t/function-node-for-starttime-in-time-range-node/62289/9 "2022-05-06T22:25:51Z")

</div>

```auto
[{"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}]

```

---

<div class="post-metadata">

### Author: ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)
#### Post date: [7 May 2022 00:00 UTC](https://discourse.nodered.org/t/function-node-for-starttime-in-time-range-node/62289/10 "2022-05-07T00:00:21Z")

</div>

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](https://github.com/rdmtc/node-red-contrib-sun-position/wiki/within-time)

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

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [6 July 2022 00:01 UTC](https://discourse.nodered.org/t/function-node-for-starttime-in-time-range-node/62289/11 "2022-07-06T00:01:08Z")

</div>

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