# Strange NaN reply

**URL:** <https://discourse.nodered.org/t/strange-nan-reply/25534>\
**Category:** General\
**Created:** [26 April 2020 09:47 UTC](https://discourse.nodered.org/t/strange-nan-reply/25534 "2020-04-26T09:47:06Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mrfox](https://avatars.discourse-cdn.com/v4/letter/m/f19dbf/32.png) [@Mrfox](https://discourse.nodered.org/u/Mrfox)\
**Post date:** [26 April 2020 09:47 UTC](https://discourse.nodered.org/t/strange-nan-reply/25534/1 "2020-04-26T09:47:06Z")

</div>

I am getting a bit confused here, I am trying to do an hourly calculation of my energy consumption:

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

The problem is that I still get the NaN from the last Function node (Hourcons). There are two messages, consisting of numbers, going in to that node for subtraction. Why does it reply Not a Number!?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [26 April 2020 09:58 UTC](https://discourse.nodered.org/t/strange-nan-reply/25534/2 "2020-04-26T09:58:10Z")

</div>

Without seeing the actual code in that function it's impossible to say.

The usual cause will be some maths operation using `undefined` rather than a number value.

You could add some `node.warn(value)` statements in the function to check your values are what you expect and to see where the NaN value is being generated.

---

<div class="post-metadata">

**Author:** ![Mrfox](https://avatars.discourse-cdn.com/v4/letter/m/f19dbf/32.png) [@Mrfox](https://discourse.nodered.org/u/Mrfox)\
**Post date:** [26 April 2020 10:15 UTC](https://discourse.nodered.org/t/strange-nan-reply/25534/3 "2020-04-26T10:15:03Z")

</div>

Her is the code for the last Node:

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

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [26 April 2020 10:21 UTC](https://discourse.nodered.org/t/strange-nan-reply/25534/4 "2020-04-26T10:21:26Z")

</div>

Copy and pasting the function text is much more helpful than a screenshot as it saves me having to type out any suggestions.

1. Don't use `-=` - use `-`
2. Have you checked that `thishourread` and `lasthourread` are always defined?

---

<div class="post-metadata">

**Author:** ![Mrfox](https://avatars.discourse-cdn.com/v4/letter/m/f19dbf/32.png) [@Mrfox](https://discourse.nodered.org/u/Mrfox)\
**Post date:** [26 April 2020 12:07 UTC](https://discourse.nodered.org/t/strange-nan-reply/25534/5 "2020-04-26T12:07:08Z")

</div>

Ok, I have changed the -= to just -.

I think that variables `thishourread` and `lasthourread` are defined as they returns in the debug as blue figures when the inject node triggers.

This is what I get:

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

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [26 April 2020 13:14 UTC](https://discourse.nodered.org/t/strange-nan-reply/25534/6 "2020-04-26T13:14:37Z")

</div>

There is an extra `,` at the end of the key:value.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [26 April 2020 13:27 UTC](https://discourse.nodered.org/t/strange-nan-reply/25534/7 "2020-04-26T13:27:34Z")

</div>

The debug shows you have thishourread and lasthourread in separate messages.

If you want to perform a calculation using both values you need to get them into the same message. Otherwise, when the function node receives the messages it only has one or the other value.

You can use the Join node to combine the messages into one.

---

<div class="post-metadata">

**Author:** ![Mrfox](https://avatars.discourse-cdn.com/v4/letter/m/f19dbf/32.png) [@Mrfox](https://discourse.nodered.org/u/Mrfox)\
**Post date:** [26 April 2020 21:36 UTC](https://discourse.nodered.org/t/strange-nan-reply/25534/8 "2020-04-26T21:36:10Z")

</div>

This is how this ended up, it is now working!

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

Thank you very much!

---

<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:** [25 June 2020 21:36 UTC](https://discourse.nodered.org/t/strange-nan-reply/25534/9 "2020-06-25T21:36:12Z")

</div>

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