Node "moment", output format "calendar", different results

Hi,

I am using node moment to humanize time. I've tried the Output Format calendar. It generally works great but for some 20% cases it gives me wrong time. E.g. from the future.

When it works ok, the message looks like
image

but sometimes it gives me
image
(at the bottom is oryginal format from input)

Does anybody else has this problem?

@QbaF Welcome to the forum! Can you show us the settings you have in the Moment node?

Actually, if it is a small flow, the export the flow and attach it to a reply AFTER reading this thread

Is it the moment (humanizer) node you are using or the moment library in your own code?

Also, can you share an example of both the input data that works and one that does not?

You will also need to share the settings in the humanizer node.

It is moment node.

In my first message you have examples of both cases - good and wrong (at this same flow settings). In both you can see:
message time
message title
Output Time from moment node
Input Time for moment node

The full flow:

[{"id":"152d0e5f.0d3222","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"40c17b7d.b30e24","type":"server-state-changed","z":"152d0e5f.0d3222","name":"Ewa Ping change state","server":"f0c730a9.6c722","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"device_tracker.ewa_ping","entityidfiltertype":"exact","outputinitially":true,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"x":120,"y":200,"wires":[["16c9b768.8960d9"]]},{"id":"b62ec91c.96bb78","type":"api-call-service","z":"152d0e5f.0d3222","name":"Wyślij wiadomość Qbie","server":"f0c730a9.6c722","version":1,"debugenabled":false,"service_domain":"notify","service":"mobile_app_sm_g970f","entityId":"","data":"{\"title\":\"{{topic}} {{payload}}\",\"message\":\"Od {{ostatnio_zmieniono}} \\n {{data.new_state.last_changed}}\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":850,"y":200,"wires":[[]]},{"id":"133739bc.8d6206","type":"moment","z":"152d0e5f.0d3222","name":"","topic":"","input":"data.new_state.last_changed","inputType":"msg","inTz":"ETC/GMT","adjAmount":0,"adjType":"days","adjDir":"add","format":"calendar","locale":"pl_PL","output":"ostatnio_zmieniono","outputType":"msg","outTz":"Europe/Warsaw","x":560,"y":200,"wires":[["b62ec91c.96bb78"]]},{"id":"16c9b768.8960d9","type":"change","z":"152d0e5f.0d3222","name":"Ewa","rules":[{"t":"set","p":"topic","pt":"msg","to":"Ewa","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":330,"y":200,"wires":[["133739bc.8d6206"]]},{"id":"f0c730a9.6c722","type":"server","z":"","name":"Home Assistant","legacy":true,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

Sorry, that flow doesn't really help since I don't have all of the nodes it requires. That's why I asked for the data & settings.

The other thing to try would be to run a nodejs app with moment to see if the issue is in the momentjs code or whether it is an artifact of the moment node.

Of course @TotallyInformation

And the input data examples? Just the data, you can capture that by putting a debug node attached to the output from your initial node. Then in the debug sidebar there is a small icon that lets you copy the data.

Input data strings are bottom lines of first post screenshots:
2020-03-04T07:20:12.598450+00:00
2020-03-04T12:14:57.501133+00:00

Or do you ask for the complete input object?

Stick a debug node on the output of the moment node so display when the format changes or is it possibel when you send the data to the api-call-service node something is happening to the date after that??

For correct formating is enough or it have to be for wrong one? As I did try to make it by hand and as for now all the answers was ok.

Sorry I don’t understand what you mean

OK, we got it.

This is what is getting into the moment (with marked most important data):
image

This is what is going out of the moment:
image

This is what we have on call-service node:
image

When you post a screenshot, we cannot copy it into a flow. You need to post as JSON output.

However, I can now finally see what the issue is. There is a bug in MomentJS that I've raised previously and they have refused to fix.

If you specify an input with more than 3 decimal places on the seconds value, MomentJS will misinterpret the timestamp.

Please feel free to raise this with the authors of Moment as it is a pain. I've put a work-around into the moment node but it doesn't seem to handle the format you've provided. If you remove the timezone offset from the end, it should work.

Here is the workaround code. If you are good with Regular Expressions, perhaps you can extend it to cover your case and I will be happy to publish a new version.

/** Catch & correct input strings in ISO format with seconds >3dp
 * since parseFormat has an unfixed bug (Issue #24)
 * @param {string} inp Input date/time string
 */
function catchDp(inp) {
    // If ISO string w/ seconds >3dp, then process else exit
    if ( inp.match(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{4,}/) ) {
        inp = inp.replace(/^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3})\d*(.*)/, '$1$2')
    }

    return inp
}

Otherwise, you will need to tweak the input values before sending them to the moment node so that the decimal seconds have no more than 3dp.

I've actually reproduced the issue and will take a deeper look in a bit but my lap has been taken over by my three year old grand daughter. :smiling_face_with_three_hearts:

1 Like

@QbaF Do you have any control the format of the date coming in to the moment node?

@zenofmud
There is always possibility to control incoming data by changing it. That is what for I used moment node;) But what I receive from state node it is, as I know, standard "last changed" data format.

@TotallyInformation
I am not sure is it problem of "more than 3 decimal places" as in both cases (good and wrong) there are more than 3 decimal places.
Formating values to send them to format in moment does seems as a double work.
Anyway thanks.

57.501133 seconds is not standard and most likely completely ridiculous. 0.000003 seconds is three microseconds. Few systems outside specialist high-performance scientific and industrial systems will ever be able to measure to this accuracy. :grinning:

You would need to look up the issue I raised, I was quite a while back now and I can't remember the details.

Indeed, a pain.

I recommend at least trying the data with the shortened seconds to see if that is the problem.

The format of the date/time being passed to the moment node is causing the issue. What you could do is to add another rule to the change node to truncate the data after the period (.)

Add another 'set' and set msg.data.new_state.last_changed and use this jsonata expression
$substringBefore(data.new_state.last_changed, '.') & '+00:00'
See if that works for you

@TotallyInformation

57.501133 seconds is not standard and most likely completely ridiculous.

I am not sure that all nodes, even most has this format for last changed / last updated but at least a lot does. Isn't moment repairable?
But can not disagree about the "ridiculous"

Could You please send link to the issue mentioned. I can see only two issues connected with moment but no one is yours.

@zenofmud
Thank you. I will get it a try.
But maybe for future use need to find and try better working node.

I assume you ment this for me. When I said I reproduced the issue I ment what I said. I caused it to happen in my node-red. Somethng about the incoming date is causing an issue with the moment node. Since you don't seem to be able to control the format of the date being sent to your flow, I created a 'get arround' for you. I have not opened an issue in the moment node because the date format is not what I would call standard.

UPDATE: you may want to go read this post about the moment node