# Beginner questions on IF expressions and wildcard usage in function node

**URL:** <https://discourse.nodered.org/t/beginner-questions-on-if-expressions-and-wildcard-usage-in-function-node/80999>\
**Category:** General\
**Created:** [3 September 2023 11:43 UTC](https://discourse.nodered.org/t/beginner-questions-on-if-expressions-and-wildcard-usage-in-function-node/80999 "2023-09-03T11:43:38Z")\
**Posts on this page:** 20\
**Page:** 7

<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:** [6 September 2023 20:00 UTC](https://discourse.nodered.org/t/beginner-questions-on-if-expressions-and-wildcard-usage-in-function-node/80999/122 "2023-09-06T20:00:46Z")

</div>

> [@TotallyInformation](#):
>
> `((new Date('2023-09-06 11:30:00')) - (new Date('2023-09-06 10:30:00')))/60000`

Actually that gives a linting error "The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.(2362)" because it does not know that javascript can subtract dates. I believe that js just calls getTime() to convert them to ms then subtracts, so the expression is functionally identical to

`(new Date('2023-09-06 11:30:00').getTime() - new Date('2023-09-06 10:30:00').getTime())/60000`

> [@TotallyInformation](#):
>
> if you used getTime and the two dates cross the midnight boundary, you'd likely get the wrong answer anyway.

I don't think that is correct. getTime() returns the time since 1970 in ms.

---

<div class="post-metadata">

**Author:** ![AleXSR700](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/alexsr700/32/41161_2.png) [@AleXSR700](https://discourse.nodered.org/u/AleXSR700)\
**Post date:** [6 September 2023 20:04 UTC](https://discourse.nodered.org/t/beginner-questions-on-if-expressions-and-wildcard-usage-in-function-node/80999/123 "2023-09-06T20:04:50Z")

</div>

> [@AleXSR700](#):
>
> `msg.TimeDelta = ((new Date(msg.Time).getTime()) - (new Date(msg.topic + '_Time').getTime())) / 1000;`  
> but I am getting `TimeDelta: NaN`

@Colin: do you see an issue here then also that would explain the `NaN`?

Or does the function have a problem with the `T` in `"Time":"2023-09-06T21:40:52"` ?

---

<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:** [6 September 2023 20:07 UTC](https://discourse.nodered.org/t/beginner-questions-on-if-expressions-and-wildcard-usage-in-function-node/80999/124 "2023-09-06T20:07:49Z")

</div>

Feed the message you are feeding into the node into a debug node, set to Output Complete Message. and show us what you see.

---

<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 September 2023 20:10 UTC](https://discourse.nodered.org/t/beginner-questions-on-if-expressions-and-wildcard-usage-in-function-node/80999/125 "2023-09-06T20:10:18Z")

</div>

Another option is to break down your code so you can see where it goes wrong

```auto
const debugMe = {
    msgTime: msg.Time,
    msgTimeType: typeof msg.Time,
    msgTimeAsDate: new Date(msg.Time),
    msgTimeAsDateToTime: new Date(msg.Time).getTime(),
}
node.warn(debugMe)

const debugMe2 = {
    msgTopicPlusTime: msg.topic + '_Time',
    msgTopicPlusTimeAsDate: new Date(msg.topic + '_Time'),
    msgTopicPlusTimeAsDateToTime: new Date(msg.topic + '_Time').getTime()
}
node.warn(debugMe2)

mTime = 
msg.TimeDelta = ((new Date(msg.Time).getTime()) - (new Date(msg.topic + '_Time').getTime())) / 1000;

```

---

<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:** [6 September 2023 20:23 UTC](https://discourse.nodered.org/t/beginner-questions-on-if-expressions-and-wildcard-usage-in-function-node/80999/126 "2023-09-06T20:23:31Z")

</div>

Yes that is a good way. I would like to know what is in msg.topic that with '\_Time' on the end gives a valid input to `new Date()`

---

<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 September 2023 20:33 UTC](https://discourse.nodered.org/t/beginner-questions-on-if-expressions-and-wildcard-usage-in-function-node/80999/127 "2023-09-06T20:33:03Z")

</div>

> [@Colin](#):
>
> I would like to know what is in msg.topic that with '\_Time' on the end gives a

indeed & why i asked for:

> [@Steve-Mcl](#):
>
> ```auto
> const debugMe2 = {
> msgTopicPlusTime: msg.topic + '_Time',
> msgTopicPlusTimeAsDate: new Date(msg.topic + '_Time'),
> msgTopicPlusTimeAsDateToTime: new Date(msg.topic + '_Time').getTime()
> }
> node.warn(debugMe2)
> 
> ```

in particular.

---

<div class="post-metadata">

**Author:** ![AleXSR700](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/alexsr700/32/41161_2.png) [@AleXSR700](https://discourse.nodered.org/u/AleXSR700)\
**Post date:** [6 September 2023 20:37 UTC](https://discourse.nodered.org/t/beginner-questions-on-if-expressions-and-wildcard-usage-in-function-node/80999/128 "2023-09-06T20:37:14Z")

</div>

@Colin:  
`flow.set(msg.topic + '_Time', msg.Time)`  
It is at the end of the function so whenever the if condition is true the current time is written to that message property to create "previous time".  
Example  
`tele/xiaomi/ATC4e211d_Time "2023-09-06T22:17:40"`

@Steve-Mcl  
I will test as soon as my alarm clock forces me to get back out of bed 🙂

By the way, all my debug nodes are always complete message 😉  
I will post the input into the function tomorrow. But the previous time is not fed into the node. It is a flow context. Only msg.Time is fed into the function and is the same format.

I will post tomorrow morning 🙂

---

<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:** [6 September 2023 20:39 UTC](https://discourse.nodered.org/t/beginner-questions-on-if-expressions-and-wildcard-usage-in-function-node/80999/129 "2023-09-06T20:39:25Z")

</div>

Perhaps you meant  
`new Date(flow.get(msg.topic + '_Time'))`  
You currently have  
`new Date("tele/xiaomi/ATC4e211d_Time")`  
which is meaningless

---

<div class="post-metadata">

**Author:** ![AleXSR700](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/alexsr700/32/41161_2.png) [@AleXSR700](https://discourse.nodered.org/u/AleXSR700)\
**Post date:** [6 September 2023 20:42 UTC](https://discourse.nodered.org/t/beginner-questions-on-if-expressions-and-wildcard-usage-in-function-node/80999/130 "2023-09-06T20:42:43Z")

</div>

Oh my! Oh my oh my oh my....  
I think you might be right!

If my wife wasn't asleep next to me I would run to my computer to test!!!!!!!!  
I think I totally messed that one up and simply forgot to get to treat it correctly as a flow context 🤦‍♂️ 🤦‍♂️ 🤦‍♂️

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [6 September 2023 21:45 UTC](https://discourse.nodered.org/t/beginner-questions-on-if-expressions-and-wildcard-usage-in-function-node/80999/131 "2023-09-06T21:45:11Z")

</div>

> [@Colin](#):
>
> Actually that gives a linting error "The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.(2362)" because it does not know that javascript can subtract dates.

Yes, from MDN:

> Subtracting two subsequent `getTime()` calls on newly generated [`Date`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) objects, give the time span between these two calls.

Yes, there are some pretty bad errors in the linting in Monaco/VScode. Thankfully, not too many.

You can always use the `toValue` function if you want to avoid the warning.

> [@Colin](#):
>
> I believe that js just calls getTime() to convert them to ms then subtracts, so the expression is functionally identical to

Looks like you are correct. I think I read the wrong web answer. ☹

> [@AleXSR700](#):
>
> do you see an issue here then also that would explain the `NaN`?

See this:

> Returns `NaN` if the date is [invalid](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#the_epoch_timestamps_and_invalid_date).

---

<div class="post-metadata">

**Author:** ![AleXSR700](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/alexsr700/32/41161_2.png) [@AleXSR700](https://discourse.nodered.org/u/AleXSR700)\
**Post date:** [7 September 2023 04:44 UTC](https://discourse.nodered.org/t/beginner-questions-on-if-expressions-and-wildcard-usage-in-function-node/80999/132 "2023-09-07T04:44:33Z")

</div>

IT really was the forgotten `flow.get()` -.-

But here the promised additional info (numbered debugs seen here are of course just for this feedback)

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

raw\_data:  
`{"topic":"raw_data/tasmota_D2EE70/SENSOR","payload":{"Time":"2023-09-07T06:37:42","ATC8f1b4f":{"mac":"a4c1388f1b4f","Temperature":21.6,"Humidity":59.2,"DewPoint":13.2,"Btn":0,"Battery":69,"RSSI":-56},"ATC801437":{"mac":"a4c138801437","Temperature":22.9,"Humidity":76.2,"DewPoint":18.4,"Btn":0,"Battery":47,"RSSI":-79},"TempUnit":"C"},"qos":0,"retain":false,"_msgid":"c2e9ee8679d60286"}`

debug 332:  
`{"topic":"tele/xiaomi/ATC801437","payload":{"mac":"a4c138801437","Temperature":22.9,"Humidity":76.2,"DewPoint":18.4,"Btn":0,"Battery":47,"RSSI":-79},"Time":"2023-09-07T06:37:42","_msgid":"c2e9ee8679d60286"}`

debug 333:  
`{"topic":"tele/xiaomi/ATC801437","payload":{"mac":"a4c138801437","Temperature":22.9,"Humidity":76.2,"DewPoint":18.4,"Btn":0,"Battery":47,"RSSI":-79},"Time":"2023-09-07T06:37:42","_msgid":"c2e9ee8679d60286","Temperature":22.9}`

m32 ATC\*Temperature:  
`{"topic":"tele/xiaomi/ATC801437","payload":{"mac":"a4c138801437","Temperature":23.5,"Humidity":60.8,"DewPoint":15.5,"Btn":0,"Battery":47,"RSSI":-83},"Time":"2023-09-07T06:38:32","_msgid":"8024b9f1fea9f525","Temperature":23.5,"PowerDelta":0.3999999999999986,"TimeDelta":40}`

In case you are wondering:  
I am preserving the original message in the payload so I can publish it once I am done with my processing. The added msg properties inbetween are either for better handling or debugging or just out of curiosity ☺

---

<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:** [7 September 2023 07:29 UTC](https://discourse.nodered.org/t/beginner-questions-on-if-expressions-and-wildcard-usage-in-function-node/80999/133 "2023-09-07T07:29:37Z")

</div>

> [@TotallyInformation](#):
>
> You can always use the `toValue` function if you want to avoid the warning.

I think that should be `valueOf()`. It seems that the spec for valueOf() is identical to getTime().

From mdn web docs:

The **`valueOf()`** method of [`Date`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) instances returns the number of milliseconds for this date since the [epoch](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#the_epoch_timestamps_and_invalid_date), which is defined as the midnight at the beginning of January 1, 1970, UTC.

The **`getTime()`** method of [`Date`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) instances returns the number of milliseconds for this date since the [epoch](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#the_epoch_timestamps_and_invalid_date), which is defined as the midnight at the beginning of January 1, 1970, UTC.

---

<div class="post-metadata">

**Author:** ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)\
**Post date:** [7 September 2023 10:10 UTC](https://discourse.nodered.org/t/beginner-questions-on-if-expressions-and-wildcard-usage-in-function-node/80999/134 "2023-09-07T10:10:52Z")

</div>

As Colin says

```auto
new Date(msg.topic + '_Time')

```

is never going to work because you are trying to get a Date object from a non date string

---

<div class="post-metadata">

**Author:** ![AleXSR700](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/alexsr700/32/41161_2.png) [@AleXSR700](https://discourse.nodered.org/u/AleXSR700)\
**Post date:** [7 September 2023 10:22 UTC](https://discourse.nodered.org/t/beginner-questions-on-if-expressions-and-wildcard-usage-in-function-node/80999/135 "2023-09-07T10:22:38Z")

</div>

See above. I forgot to get the context. It was never sipposed to be a message property but a context named after a message property 😉

---

<div class="post-metadata">

**Author:** ![AleXSR700](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/alexsr700/32/41161_2.png) [@AleXSR700](https://discourse.nodered.org/u/AleXSR700)\
**Post date:** [7 September 2023 10:29 UTC](https://discourse.nodered.org/t/beginner-questions-on-if-expressions-and-wildcard-usage-in-function-node/80999/136 "2023-09-07T10:29:58Z")

</div>

According to a stackoverflow post, getTime() was inteoduced first. Since it is less versatile, valueOf() was introduced but getTime() never removed. Maybe for backwards compatibility, maybe because it feels more elegant 🤷

> This makes sense if we consider that getTime got introduced in JavaScript 1.0 and valueOf was introduced in JavaScript 1.1. My guess is that getTime was kept for backwards compability.

-Attila Kun

[Mar 14, 2012 at 21:03](https://stackoverflow.com/questions/9710136/in-javascript-why-do-date-objects-have-both-valueof-and-gettime-methods-if-they#comment12343181_9710152)

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [7 September 2023 16:49 UTC](https://discourse.nodered.org/t/beginner-questions-on-if-expressions-and-wildcard-usage-in-function-node/80999/137 "2023-09-07T16:49:34Z")

</div>

> [@Colin](#):
>
> I think that should be `valueOf()`. It seems that the spec for valueOf() is identical to getTime().

Doh! Shows how tired I was yesterday since I'd only just looked it up!

---

<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:** [7 September 2023 17:02 UTC](https://discourse.nodered.org/t/beginner-questions-on-if-expressions-and-wildcard-usage-in-function-node/80999/138 "2023-09-07T17:02:08Z")

</div>

Personally I think that getTicks() or getms() would have been better. Too late now.

---

<div class="post-metadata">

**Author:** ![AleXSR700](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/alexsr700/32/41161_2.png) [@AleXSR700](https://discourse.nodered.org/u/AleXSR700)\
**Post date:** [7 September 2023 20:16 UTC](https://discourse.nodered.org/t/beginner-questions-on-if-expressions-and-wildcard-usage-in-function-node/80999/139 "2023-09-07T20:16:28Z")

</div>

I am still struggling a bit with the details of the matchingKeys.forEach part.  
I would like to add the msg.payload.Time to the payload of each element.

My first approach is

```auto
const matchingKeys = Object.keys(msg.payload).filter((element) => element.startsWith("ATC"))
matchingKeys.forEach((element) => node.send({
    topic: "tele/xiaomi/" + element,
    payload[element].Time: msg.payload.Time, //error message: No value exists in scope for the shorthand property 'payload'. Either declare one or provide an initializer.(18004)
    payload: msg.payload[element],
}))

```

I was thinking that I added the sub property to the element's payload and then made the node out payload the extended payload.  
I thought that maybe I first have to initialize the message property msg.payload[element].Time because it is not known to the system yet.  
So I tried

```auto
const matchingKeys = Object.keys(msg.payload).filter((element) => element.startsWith("ATC"))
matchingKeys.forEach((element) => node.send({
    topic: "tele/xiaomi/" + element,
    msg.payload[element].Time = msg.payload.Time,
    payload: msg.payload[element],
}))

```

This should create a property "Time" in each payload of each element. Well, at least that is what I thought 😃  
What am I doing wrong? Or where is my mental mistake? I am not altering the original payload, only the element specific one. That should be fine in my books 😉

I keep seeing a yellow lightbulb or a red triangle but those seem to only say "something is wrong" and are giving absolutely no indication of what. Seems somehow useless to me.  
Sometimes I can hover with the mouse over each part of the respective line of code but this time I don't even see anything there.  
Obviously the system dislikes something but is not sharing with me what it is 🤔

---

<div class="post-metadata">

**Author:** ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)\
**Post date:** [7 September 2023 20:28 UTC](https://discourse.nodered.org/t/beginner-questions-on-if-expressions-and-wildcard-usage-in-function-node/80999/140 "2023-09-07T20:28:20Z")

</div>

You have an assignment as a property of the Object

```auto
const matchingKeys = Object.keys(msg.payload).filter((element) => element.startsWith("ATC"))
matchingKeys.forEach((element) => node.send({
    topic: "tele/xiaomi/" + element,
    msg.payload[element].Time = msg.payload.Time, // try msg.payload[element].Time: msg.payload.Time,
    payload: msg.payload[element],           
}))

```

---

<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:** [7 September 2023 20:41 UTC](https://discourse.nodered.org/t/beginner-questions-on-if-expressions-and-wildcard-usage-in-function-node/80999/141 "2023-09-07T20:41:00Z")

</div>

Currently msg.payload is just a value (msg.payload[element]). If you want to have a property msg.payload.time then you must make the payload be an object and move the existing value into another property, so something like

```auto
matchingKeys.forEach((element) => node.send({
    topic: "tele/xiaomi/" + element,
    payload: {value: msg.payload[element], time: msg.payload.time}
}))

```

Where you can change `value` to whatever name you want.  
So the line  
`payload: {value: msg.payload[element], time: msg.payload.time}`  
says that payload is an object with two properties, value and time, and sets them to those values.

[Previous page](https://discourse.nodered.org/t/beginner-questions-on-if-expressions-and-wildcard-usage-in-function-node/80999.md?page=6)

[Next page](https://discourse.nodered.org/t/beginner-questions-on-if-expressions-and-wildcard-usage-in-function-node/80999.md?page=8)
