Hi, I am trying to develop a flow that will be trigger every few minutes to determine if a device has been ON too long.
I plan to use Inject Node to trigger the process every (5) minutes.
Then I wish to use Node type TEMPLATE or FUNCTION to calculate the difference between current time (timestamp) of Inject Node and the " {{payload.last_changed}}" of the device being checked.
Timestamp format is 1673297371916 and last.changed format is 023-01-09T18:54:23.054819+00:00.
Which formula should I use (in each node type ; template and function ) to the calculate the elapse time difference between the two.
You could also use the trigger node and set it to the time you want the maximum light on time. Configure it for “second msg on separate output” and “extend delay if new msg”.
Then let the light on state trigger the trigger node.
Now the second output of the trigger node will fire exactly the defined time after the light has last been turned on. Route this output to a current state node to check whether the light is still on.
This way you avoid constant polling and save a few microwatts of CPU power.
Thank you for your help.
Should I use this formula in node type Template or Change ?
In Template; the payload output is the exact text entered, not an number of milliseconds
In Change : both {}JASON and J: expression give me an error not properly configure.
I love your suggestion, very easy to implement and can set it with a different delay per device.
However, I would like to be able to use the first alternative as well, to help me to understand how to use template and function.
Thank you very much
The expression I gave in my first post is the javascript for a function node. The function node is the one I use most, whenever there is not a native node-red node available for the same task or when I need calculations or more complicated expressions.
There is no particular reason to not use a change node and somehow formulate that conversion in JsonATA instead (the “J”-expression in many nodes if you are able to figure out the correct JsonATA syntax, which is a whole different language in itself), it is just that for me personally it is much more convenient to just write it in JavaScript, but this is a matter of personal preference. I also occasionally use JsonATA when it helps me to avoid adding yet another node behind it for a simple conversion or attribute extraction. It depends. Very often I also use this to construct the data structure that is needed for call-service nodes. But I cannot tell from the top of my head now how to do this same date-time conversion in JsonATA (I am sure it is possible) without some googling myself.