JSONata $toMillis() in change node will not convert an ISO complaint date string

I have another anomaly when using JSONata in a change node, and it is to do with time/date objects (of course).
I am using a node-red-contrib-sun-event-trigger to produce a sunrise trigger, and also capture the days other sun related events. on start up it produces an object with several key:value pairs for the days event, with the value being a date string like "2026-05-12T20:48:34.671Z" which is ISO compatible. But when I try to convert this to a date number with $toMillis() in a change node I get the error.

Invalid JSONata expression: Argument 1 of function "toMillis" does not match function signature

The Jsonata statement works in try.json.org when tested against a copy of the data fed to the node.

I can convert it the date string with some JS in a function node (val is the date string)

    else if (isValidDateString(val)) {
        // If it's a valid date string, parse directly
        millis = new Date(val.trim()).getTime();
    }
    else {
        // Try to coerce to string and parse
        let strVal = String(val).trim();
        if (isValidDateString(strVal)) {
            millis = new Date(strVal).getTime();
        }

Here is the simplified flow that exhibits the behaviour.

[
    {
        "id": "e75342e810b228f7",
        "type": "debug",
        "z": "fb3b8eb733c0c5e0",
        "name": "Discrete Event",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 860,
        "y": 860,
        "wires": []
    },
    {
        "id": "0683c88ce677ce7a",
        "type": "change",
        "z": "fb3b8eb733c0c5e0",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "sunrise",
                "pt": "msg",
                "to": "$toMillis(payload.sunrise)",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 640,
        "y": 860,
        "wires": [
            [
                "e75342e810b228f7"
            ]
        ]
    },
    {
        "id": "6627d82026f11d88",
        "type": "switch",
        "z": "fb3b8eb733c0c5e0",
        "name": "",
        "property": "payload.sunEvent",
        "propertyType": "msg",
        "rules": [
            {
                "t": "null"
            },
            {
                "t": "else"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 410,
        "y": 960,
        "wires": [
            [
                "3a8e0dd0848ee708",
                "f50672bec3e58cfa",
                "0683c88ce677ce7a",
                "e75342e810b228f7"
            ],
            [
                "8ed74b07effbab9b"
            ]
        ]
    },
    {
        "id": "bee2284c3912022b",
        "type": "junction",
        "z": "fb3b8eb733c0c5e0",
        "x": 300,
        "y": 960,
        "wires": [
            [
                "0a11d8da95898da4",
                "6627d82026f11d88"
            ]
        ]
    },
    {
        "id": "792599f715b4c8b7",
        "type": "sun-event-inject",
        "z": "fb3b8eb733c0c5e0",
        "name": "",
        "latitude": "-35.31120",
        "longitude": "149.68053",
        "event": "sunrise",
        "offset": "0",
        "injectEventTimesAfterStartup": true,
        "x": 210,
        "y": 960,
        "wires": [
            [
                "bee2284c3912022b"
            ]
        ]
    },
    {
        "id": "70b8f91ff26d4635",
        "type": "global-config",
        "env": [],
        "modules": {
            "node-red-contrib-sun-event-trigger": "1.1.1"
        }
    }
]

Although I have a workaround that suits me using the function node and converting all date strings to millis when received, it has taken me a while to develop a work around what is I believe is a bug in JSONata implementation... is there something in the node-red-contrib-sun-event-trigger node output I cannot see the cause.

ta

I've just fed the ISO date/time string you provided through a change node set to convert to millis and it worked fine, no errors.

[{"id":"6f334eaa7fed3055","type":"inject","z":"b2f18a716bd20f99","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"2026-05-12T20:48:34.671Z","payloadType":"str","x":190,"y":2020,"wires":[["e727dc4d59e1a0e6"]]},{"id":"fd63f2ac0b883f2b","type":"debug","z":"b2f18a716bd20f99","name":"debug 38","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":620,"y":2020,"wires":[]},{"id":"e727dc4d59e1a0e6","type":"change","z":"b2f18a716bd20f99","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$toMillis(payload)\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":420,"y":2020,"wires":[["fd63f2ac0b883f2b"]]}]

Did I miss something?

Incidentally, I use node-red-contrib-sun-position (node) - Node-RED and have a startup flow that writes sun and moon details to MQTT, and updates the sun and moon positions every minute for good measure.

Ah! Got it! :smiley:

You've unfortunately fallen into one of JavaScripts more confusing traps.

The value you shared is what happens to a JavaScript timestamp when it is serialised to a string - for example, when you dump it to the debug output.

The error you are getting is because your input is not, in fact an ISO timestamp string - but rather an actual JavaScript Date timestamp. $toMillis doesn't actually work with that - presumably because it is unnecessary, you already have the data in millis.