# Comparing Strings to numbers in Wait node?

**URL:** <https://discourse.nodered.org/t/comparing-strings-to-numbers-in-wait-node/93511>\
**Category:** General\
**Created:** [27 November 2024 11:37 UTC](https://discourse.nodered.org/t/comparing-strings-to-numbers-in-wait-node/93511 "2024-11-27T11:37:30Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![vaderag](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/vaderag/32/95219_2.png) [@vaderag](https://discourse.nodered.org/u/vaderag)\
**Post date:** [27 November 2024 11:37 UTC](https://discourse.nodered.org/t/comparing-strings-to-numbers-in-wait-node/93511/1 "2024-11-27T11:37:30Z")

</div>

I wonder if someone can help me here - I have a temperature value which for some reason, is a string. In some nodes I can force it to act as a number (and I have done so in the flow below to be able to add 2 to it)  
Essentially I want my towel rail to go off when the temperature is increased by two

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

The problem seems to be that the wait node never triggers, presumably because my now number is not equal to the string it's being compared to.

I'm not clear on how I can either revert the stored flow variable to be a string, or use the string as a number in the wait node... can anyone advise?  
Maybe there is a better way to do this than the wait node that I'm missing!?

Any help appreciated 🙂

```auto
[{"id":"795593744ea3239b","type":"trigger-state","z":"9dddcfaf9cc3bc8a","g":"cfcae76508526628","name":"Bathroom Towel Rail Turns On","server":"3aa13fed.bf745","version":5,"inputs":0,"outputs":2,"exposeAsEntityConfig":"","entities":{"entity":["switch.shelly1_f4cfa276b8a6"],"substring":[],"regex":[]},"debugEnabled":false,"constraints":[{"targetType":"this_entity","targetValue":"","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"on"}],"customOutputs":[],"outputInitially":false,"stateType":"str","enableInput":false,"x":1040,"y":660,"wires":[["d40a4d86259c5273"],["b648b7279de430d9"]]},{"id":"d40a4d86259c5273","type":"api-current-state","z":"9dddcfaf9cc3bc8a","g":"cfcae76508526628","name":"UFH Thermostat Temp","server":"3aa13fed.bf745","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"sensor.ufh_true_temperature","state_type":"num","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":1320,"y":640,"wires":[["543c5e039b066342"]]},{"id":"543c5e039b066342","type":"change","z":"9dddcfaf9cc3bc8a","g":"cfcae76508526628","name":"flow.thermostat-temp","rules":[{"t":"set","p":"themostat-temp","pt":"flow","to":"payload","tot":"msg"},{"t":"set","p":"target-thermostat","pt":"flow","to":"$sum([msg.payload,2])","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1560,"y":640,"wires":[["9c694a0dc0f7605f","9f4faefa777dafc5"]]},{"id":"b648b7279de430d9","type":"change","z":"9dddcfaf9cc3bc8a","g":"cfcae76508526628","name":"reset","rules":[{"t":"set","p":"payload","pt":"msg","to":"reset","tot":"str"},{"t":"set","p":"reset","pt":"msg","to":"reset","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1150,"y":720,"wires":[["9f4faefa777dafc5"]]},{"id":"9f4faefa777dafc5","type":"ha-wait-until","z":"9dddcfaf9cc3bc8a","g":"cfcae76508526628","name":"UFH Thermo hits 2deg more","server":"3aa13fed.bf745","version":3,"outputs":1,"entities":{"entity":["sensor.ufh_true_temperature"],"substring":[],"regex":[]},"property":"state","comparator":"is","value":"target-thermostat","valueType":"flow","timeout":"0","timeoutType":"num","timeoutUnits":"seconds","checkCurrentState":true,"blockInputOverrides":true,"outputProperties":[],"x":1520,"y":700,"wires":[["836ce44f4b9584f2"]]},{"id":"836ce44f4b9584f2","type":"api-call-service","z":"9dddcfaf9cc3bc8a","g":"cfcae76508526628","name":"Turn off Bathroom Towel Rail","server":"3aa13fed.bf745","version":7,"debugenabled":false,"action":"switch.turn_off","floorId":[],"areaId":[],"deviceId":[],"entityId":["switch.shelly1_f4cfa276b8a6"],"labelId":[],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","blockInputOverrides":false,"domain":"switch","service":"turn_off","x":1800,"y":700,"wires":[[]]},{"id":"9c694a0dc0f7605f","type":"change","z":"9dddcfaf9cc3bc8a","name":"Target","rules":[{"t":"set","p":"payload","pt":"msg","to":"target-thermostat","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":1690,"y":580,"wires":[["b531620e410bb22c"]]},{"id":"b531620e410bb22c","type":"debug","z":"9dddcfaf9cc3bc8a","name":"debug 105","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1830,"y":580,"wires":[]},{"id":"d5fd4a8b5c9dfe6f","type":"inject","z":"9dddcfaf9cc3bc8a","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1320,"y":500,"wires":[["1418a9f00a77793e"]]},{"id":"1418a9f00a77793e","type":"api-current-state","z":"9dddcfaf9cc3bc8a","name":"","server":"3aa13fed.bf745","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"sensor.ufh_true_temperature","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":1590,"y":500,"wires":[[]]},{"id":"3aa13fed.bf745","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}]

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [27 November 2024 12:08 UTC](https://discourse.nodered.org/t/comparing-strings-to-numbers-in-wait-node/93511/2 "2024-11-27T12:08:39Z")

</div>

> [@vaderag](#):
>
> I'm not clear on how I can either revert the stored flow variable to be a string,

Before you save it to the flow context, insert a Function node containing

```auto
msg.payload = Number(msg.payload)
return msg

```

Which will convert it to a number.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [27 November 2024 13:06 UTC](https://discourse.nodered.org/t/comparing-strings-to-numbers-in-wait-node/93511/3 "2024-11-27T13:06:22Z")

</div>

It's not clear from your post why you want to revert from a number to a string. Is it a Home Assistant thing that numbers are usually held as strings?

I would think the best approach is to make it a number at the earliest opportunity and keep it that way.

If the node "UFH Thermo hits 2deg more" requires a string, use a filter node instead.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [27 November 2024 13:19 UTC](https://discourse.nodered.org/t/comparing-strings-to-numbers-in-wait-node/93511/4 "2024-11-27T13:19:00Z")

</div>

I think that @vaderag misstyped. I think he/she wants it be a number always.

---

<div class="post-metadata">

**Author:** ![vaderag](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/vaderag/32/95219_2.png) [@vaderag](https://discourse.nodered.org/u/vaderag)\
**Post date:** [27 November 2024 13:31 UTC](https://discourse.nodered.org/t/comparing-strings-to-numbers-in-wait-node/93511/5 "2024-11-27T13:31:28Z")

</div>

Looks like I need to be a bit clearer... yeah, realistically NodeRed always seeing it as a number would be best for my usage  
The only reason I was considering getting it back to a string is for the compare to work

Currently this fails, and my assumption is because state is string and flow.target-thermostat is number

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

I'm not certain that there is any way for the wait node to use UFH True Temperature state as a number - I need it linked to that entity and not the payload so changing it to a number before isn't going to work

Other nodes have an option to treat it differently

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/9/b989056fad9e2fb8e0109e94066644164ccebb36.png)  
which is what I have done to be able to calculate the value +2 in the flow variable

> [@Colin](#):
>
> Before you save it to the flow context, insert a Function node containing
> 
> ```auto
> msg.payload = Number(msg.payload)
> return msg
> 
> ```
> 
> Which will convert it to a number.

This is actually the opposite of what is needed, but similarly wont work because the payload needs to be a number for my calculation to work

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/7/275ad0a38c194903176f413430990e9cd710d3be.png)

I thought that would work with flow.target-thermostat as a String as well, but seems not as the function node doesn't like it ☹

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [27 November 2024 13:57 UTC](https://discourse.nodered.org/t/comparing-strings-to-numbers-in-wait-node/93511/6 "2024-11-27T13:57:16Z")

</div>

I can't comment on how to use the HA nodes as I don't use HA (few here do).

However, I have already shown how to convert it to a Number in a function node (that will also work if it is already a number). Any time you want to convert it to a string (if not already one) then you can use

```auto
// convert payload to string if not already one
if (typeof msg.payload != "string") {
  msg.payload = msg.payload.toString()
}
return msg

```

**[Edit]** Personally I would make it always a number and use a core node-red node for doing the comparison. The Switch node for example.

---

<div class="post-metadata">

**Author:** ![vaderag](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/vaderag/32/95219_2.png) [@vaderag](https://discourse.nodered.org/u/vaderag)\
**Post date:** [27 November 2024 14:06 UTC](https://discourse.nodered.org/t/comparing-strings-to-numbers-in-wait-node/93511/7 "2024-11-27T14:06:22Z")

</div>

Thanks. I agree - I'm not really sure why these numeric values come through as strings in the first place but given there doesn't seem to be a way to alter it at the point of compare I don't think I really have a choice but to use a string...  
I've now got it implemented with conversion to number and then flipping it back to string - seems to work (will give it a genuine test shortly)

---

<div class="post-metadata">

**Author:** ![mikefila](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mikefila/32/95223_2.png) [@mikefila](https://discourse.nodered.org/u/mikefila)\
**Post date:** [30 November 2024 17:08 UTC](https://discourse.nodered.org/t/comparing-strings-to-numbers-in-wait-node/93511/8 "2024-11-30T17:08:52Z")

</div>

Instead of recalling the flow variable directly, you can use jsonata.

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

Also worth mentioning context storage is set to memory only by default. I suggest [changing it to file](https://nodered.org/docs/user-guide/context#saving-context-data-to-the-file-system) to avoid losing values if you haven't already.

---

<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:** [28 February 2025 17:09 UTC](https://discourse.nodered.org/t/comparing-strings-to-numbers-in-wait-node/93511/9 "2025-02-28T17:09:17Z")

</div>

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