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.
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.
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
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.