# Calculate time difference - hass.io

**URL:** <https://discourse.nodered.org/t/calculate-time-difference-hass-io/8356>\
**Category:** General\
**Created:** [25 February 2019 09:29 UTC](https://discourse.nodered.org/t/calculate-time-difference-hass-io/8356 "2019-02-25T09:29:22Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![folrewef](https://avatars.discourse-cdn.com/v4/letter/f/e47774/32.png) [@folrewef](https://discourse.nodered.org/u/folrewef)\
**Post date:** [25 February 2019 09:29 UTC](https://discourse.nodered.org/t/calculate-time-difference-hass-io/8356/1 "2019-02-25T09:29:22Z")

</div>

Hi,

I am really new to node-red and json etc so sorry if my question seems trivial. This is my first try with a function node!

I am currently setting up a notification that will tell me if an entity has been unavailable more than one minute. In order to not to complicate things I look for a state change and verify that the previous state was unavailable and that the new state is not unavailable.

I can the look at the message which gives me information about when the state change to unavailable (old\_state) and when it changed to anything that is not unavailable (new\_state). I would now like to calculate the time difference.

The format of the time when the state changed is as follows:  
old\_state: "2019-02-25T08:57:24.387431+00:00"  
new\_state: "2019-02-25T08:57:26.326909+00:00"

I would like to calculate the time difference in seconds. If the time difference is more than 60 seconds I want to pass on a message with the payload that includes the entity\_id and the time difference). I will then base a notification on this information. My intention is to use a function node. My attempt so far looks like this:

```auto
var timediff = 0
timediff = (msg.payload.event.new_state.last_changed) - (msg.payload.event.old_state.last_changed);
if (msg1 > 60){
    return (msg.payload.entity_id, timediff)
}

```

I guess this is hopelessly wrong and therefore I need your help.

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [25 February 2019 09:40 UTC](https://discourse.nodered.org/t/calculate-time-difference-hass-io/8356/2 "2019-02-25T09:40:05Z")

</div>

First thing is that you should alway return an object.

i.e. `return msg;` which is why it is autofilled in when you open a function node 🙂

---

<div class="post-metadata">

**Author:** ![folrewef](https://avatars.discourse-cdn.com/v4/letter/f/e47774/32.png) [@folrewef](https://discourse.nodered.org/u/folrewef)\
**Post date:** [25 February 2019 09:43 UTC](https://discourse.nodered.org/t/calculate-time-difference-hass-io/8356/3 "2019-02-25T09:43:42Z")

</div>

Ok - so an else and then return an object that I can then use to prevent the next step in the flow.

---

<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:** [25 February 2019 09:44 UTC](https://discourse.nodered.org/t/calculate-time-difference-hass-io/8356/4 "2019-02-25T09:44:19Z")

</div>

Also I imagine you wanted `timediff > 60` and note the timestamps in javascript are in milliseconds.  
If you want to prevent the next step in the flow then don't return anything and it will not pass on a message.

---

<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:** [25 February 2019 09:46 UTC](https://discourse.nodered.org/t/calculate-time-difference-hass-io/8356/5 "2019-02-25T09:46:02Z")

</div>

It is not the fact that you didn't return anything that it is complaining about, it is the fact that you _did_ return something, but it wasn't an object.

---

<div class="post-metadata">

**Author:** ![folrewef](https://avatars.discourse-cdn.com/v4/letter/f/e47774/32.png) [@folrewef](https://discourse.nodered.org/u/folrewef)\
**Post date:** [25 February 2019 09:49 UTC](https://discourse.nodered.org/t/calculate-time-difference-hass-io/8356/6 "2019-02-25T09:49:18Z")

</div>

So it should be  
timediff \> 60000

The "it wasn't an object" is not clear to me (yes I am really new at this).

---

<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:** [25 February 2019 09:54 UTC](https://discourse.nodered.org/t/calculate-time-difference-hass-io/8356/7 "2019-02-25T09:54:57Z")

</div>

> [@folrewef](#):
>
> The "it wasn't an object" is not clear to me

You have  
`return(msg.payload.entity_id, timediff)`  
I don't know what you are trying to do here, but if you return anything then it must be a javascript object. If you want to pass on the id and timediff in a message then maybe you want something like

```auto
msg.payload = {id: msg.payload.entity_id, timediff: timediff}
return msg

```

---

<div class="post-metadata">

**Author:** ![folrewef](https://avatars.discourse-cdn.com/v4/letter/f/e47774/32.png) [@folrewef](https://discourse.nodered.org/u/folrewef)\
**Post date:** [25 February 2019 10:01 UTC](https://discourse.nodered.org/t/calculate-time-difference-hass-io/8356/8 "2019-02-25T10:01:31Z")

</div>

So now I have:

```auto
var timediff = 0
timediff = (msg.payload.event.new_state.last_changed) - (msg.payload.event.old_state.last_changed);
if (timediff > 60000){
    msg.payload = {id: msg.payload.entity_id, timediff: timediff}
    return msg
}

```

Does that make more sense?

I get no error message but I get no message to the debug node.

---

<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:** [25 February 2019 10:06 UTC](https://discourse.nodered.org/t/calculate-time-difference-hass-io/8356/9 "2019-02-25T10:06:31Z")

</div>

Insert this line after the timediff calculation  
`node.warn("timediff is: " + timediff)`  
That will show that output in the debug window when that line is run.

---

<div class="post-metadata">

**Author:** ![folrewef](https://avatars.discourse-cdn.com/v4/letter/f/e47774/32.png) [@folrewef](https://discourse.nodered.org/u/folrewef)\
**Post date:** [25 February 2019 10:07 UTC](https://discourse.nodered.org/t/calculate-time-difference-hass-io/8356/10 "2019-02-25T10:07:29Z")

</div>

I removed the if statement to make sure a message was returned.  
I got timediff: NaN  
I guess the format of msg.payload.event.new\_state.last\_changed is a string and not a number.

---

<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:** [25 February 2019 10:10 UTC](https://discourse.nodered.org/t/calculate-time-difference-hass-io/8356/11 "2019-02-25T10:10:08Z")

</div>

> [@folrewef](#):
>
> I guess the format of msg.payload.event.new\_state.last\_changed is a string and not a number.

Yes, I think you are right. Where are the timestamps coming from? If you cannot get them as numbers then you can probably do

```auto
var lastTime = new Date(msg.payload.event.new_state.last_changed)

```

to convert it into a timestamp.

---

<div class="post-metadata">

**Author:** ![folrewef](https://avatars.discourse-cdn.com/v4/letter/f/e47774/32.png) [@folrewef](https://discourse.nodered.org/u/folrewef)\
**Post date:** [25 February 2019 10:11 UTC](https://discourse.nodered.org/t/calculate-time-difference-hass-io/8356/12 "2019-02-25T10:11:14Z")

</div>

They come from home assistant. I will try the conversion you suggest.

---

<div class="post-metadata">

**Author:** ![folrewef](https://avatars.discourse-cdn.com/v4/letter/f/e47774/32.png) [@folrewef](https://discourse.nodered.org/u/folrewef)\
**Post date:** [25 February 2019 10:16 UTC](https://discourse.nodered.org/t/calculate-time-difference-hass-io/8356/13 "2019-02-25T10:16:25Z")

</div>

That seems to work fine. Now I will only reinsert the if statement.

---

<div class="post-metadata">

**Author:** ![folrewef](https://avatars.discourse-cdn.com/v4/letter/f/e47774/32.png) [@folrewef](https://discourse.nodered.org/u/folrewef)\
**Post date:** [25 February 2019 10:23 UTC](https://discourse.nodered.org/t/calculate-time-difference-hass-io/8356/14 "2019-02-25T10:23:31Z")

</div>

Thanks for all help - works lika a charm. Lots of new knowledge for me!

---

<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:** [25 February 2019 11:31 UTC](https://discourse.nodered.org/t/calculate-time-difference-hass-io/8356/15 "2019-02-25T11:31:35Z")

</div>

Excellent. Glad to be of help.

---

<div class="post-metadata">

**Author:** ![SciTii](https://avatars.discourse-cdn.com/v4/letter/s/a5b964/32.png) [@SciTii](https://discourse.nodered.org/u/SciTii)\
**Post date:** [8 October 2020 11:43 UTC](https://discourse.nodered.org/t/calculate-time-difference-hass-io/8356/16 "2020-10-08T11:43:14Z")

</div>

Can you share how it looks? send a flow please
