# Strange Behaviour on Applying Mathematics on Variable

**URL:** <https://discourse.nodered.org/t/strange-behaviour-on-applying-mathematics-on-variable/81986>\
**Category:** General\
**Tags:** home-assistant\
**Created:** [12 October 2023 23:28 UTC](https://discourse.nodered.org/t/strange-behaviour-on-applying-mathematics-on-variable/81986 "2023-10-12T23:28:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kalvintan](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kalvintan/32/83702_2.png) [@Kalvintan](https://discourse.nodered.org/u/Kalvintan)\
**Post date:** [12 October 2023 23:28 UTC](https://discourse.nodered.org/t/strange-behaviour-on-applying-mathematics-on-variable/81986/1 "2023-10-12T23:28:29Z")

</div>

Hello everyone,

I've recently been attempting to perform mathematical operations on a variable in my Home Assistant setup. The variable I'm working with is obtained from Home Assistant and is named `comfort_temperature`. Here's an example of what I'm doing and the unexpected results I'm getting:

```auto
var comfort_temperature = global.get('homeassistant.homeAssistant.states["input_number.bedroom_1_comfort_temperature"].state')

```

- node.status({fill: 'green', shape: 'dot', text: "Temp: " + comfort\_temperature + 2 }) **Result: 16.02**
- node.status({fill: 'green', shape: 'dot', text: "Temp: " + comfort\_temperature / 2 }) **Result: 8**
- node.status({fill: 'green', shape: 'dot', text: "Temp: " + comfort\_temperature \* 2 }) **Result: 32**

I'm puzzled by these unexpected results and would appreciate any insights or assistance in understanding what's happening.

Thanks  
Kalvin

---

<div class="post-metadata">

**Author:** ![Kalvintan](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kalvintan/32/83702_2.png) [@Kalvintan](https://discourse.nodered.org/u/Kalvintan)\
**Post date:** [13 October 2023 02:47 UTC](https://discourse.nodered.org/t/strange-behaviour-on-applying-mathematics-on-variable/81986/2 "2023-10-13T02:47:00Z")

</div>

Have successfully got this solved.

`var comfort_temperature = parseFloat(global.get('homeassistant.homeAssistant.states["input_number.bedroom_1_comfort_temperature"].state'));`

---

<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:** [13 October 2023 06:43 UTC](https://discourse.nodered.org/t/strange-behaviour-on-applying-mathematics-on-variable/81986/3 "2023-10-13T06:43:35Z")

</div>

In case you did not realise, the problem was presumably that the value you were getting was a String not a Number. Calling parseFloat converts the string to a number.

---

<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:** [27 October 2023 06:43 UTC](https://discourse.nodered.org/t/strange-behaviour-on-applying-mathematics-on-variable/81986/4 "2023-10-27T06:43:39Z")

</div>

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