Moment "fromNow" functionality

I'm trying to show time like "fromNow" feature from "moment", so "a few seconds ago", "2 minutes ago" and such.

Ideally, it would be nice to use it in template nodes as well, is there anything like it?

The only way I can think of Is using an injector to pass an "stored" date through moment nodes with "fromNow" format in it, but I don't think that this is feasible for most scenarios.

Are there any examples of how to achieve this functionality?

Thank you all!

the latest release (v1.1.2) now has $moment in the Jsonata function in the change node so you can use .fromNow() if you like :slight_smile:

[{"id":"ea9e8347.01a58","type":"inject","z":"a8a9908c.bbfce","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":130,"y":680,"wires":[["f3beefb1.49a92"]]},{"id":"f3beefb1.49a92","type":"change","z":"a8a9908c.bbfce","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$moment(payload-3600000).fromNow()","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":680,"wires":[["abe3bd65.dbd6b"]]},{"id":"abe3bd65.dbd6b","type":"debug","z":"a8a9908c.bbfce","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":550,"y":680,"wires":[]}]
1 Like

Cool! Just updated to v1.1.2

But I don't know if I didn't use it correctly, because the time is still "static" to me, I mean, I tested it with an Inject node, and waited on the dashboard for it to update the value naturally from "a few seconds ago" to "1 minute" and such, but it's stuck on "a few seconds ago" even if I reload the dashboard.

Is there a way for it to update dynamically on the dashboard?

I'm trying to use it like this flow below

[{"id":"7d7e1d25.45ec64","type":"inject","z":"d7bc9070.c779f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":270,"y":260,"wires":[["b579fa07.432718"]]},{"id":"cb7bec9e.7efa","type":"ui_text","z":"d7bc9070.c779f","group":"6d7c6599.59221c","order":2,"width":"8","height":"2","name":"","label":"Time test","format":"{{msg.payload}}","layout":"row-spread","x":700,"y":260,"wires":[]},{"id":"b579fa07.432718","type":"change","z":"d7bc9070.c779f","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$moment(msg.payload).fromNow()","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":470,"y":260,"wires":[["cb7bec9e.7efa"]]},{"id":"6d7c6599.59221c","type":"ui_group","z":"","name":"Controls","tab":"b8ee80de.1ffdf","order":1,"disp":true,"width":8,"collapse":true},{"id":"b8ee80de.1ffdf","type":"ui_tab","z":"","name":"Home","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

Almost like "how much time passed since the last I clicked on the Inject node"

The use case is to show how much time from the last time one node was updated

Like this: (but in this case, if the last time this text/template was updated like 2 hours ago, it will still show "a few seconds ago", and as I'm not querying the database for the data, I only have the actual update event that triggered this flow
image

well no - it only calculates it based on the time you give it - so you would need to keep updating the display widget - but using the same old time... maybe a trigger node set to keep sending the data - but if you have loads of them updating too often it could make updating the screen not perform great.

Great!

The trigger node solved for me (in conjunction with the updated change node)

In my case, my sensors send data every minute, so with the trigger node resending every 50 seconds, solved for me, I will quickly see if a data point is older than one minute.

Thank you so much for your help!

Here's my approach (with 10 seconds for quicker feedback):

[{"id":"7d7e1d25.45ec64","type":"inject","z":"d7bc9070.c779f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":280,"y":260,"wires":[["33331b78.0ed184"]]},{"id":"e94e35ee.2b0ce8","type":"debug","z":"d7bc9070.c779f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":810,"y":260,"wires":[]},{"id":"33331b78.0ed184","type":"trigger","z":"d7bc9070.c779f","name":"","op1":"","op2":"0","op1type":"pay","op2type":"str","duration":"-10","extend":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":450,"y":260,"wires":[["315256d9.5c88ba"]]},{"id":"315256d9.5c88ba","type":"change","z":"d7bc9070.c779f","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$moment(msg.payload).fromNow()","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":640,"y":260,"wires":[["e94e35ee.2b0ce8"]]}]

image

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.