Calculate time difference -



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:

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.


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 :slight_smile:


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


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.


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.


So it should be
timediff > 60000

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


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

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

So now I have:

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.


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.


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.


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

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

to convert it into a timestamp.


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


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


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

1 Like

Excellent. Glad to be of help.