Number in Jsonata

Hi, I just want to have the actual hour as a number.
So I´ve build:

[
    {
        "id": "9c8c4170df54250b",
        "type": "inject",
        "z": "7b74e2db318add18",
        "name": "Zeitstempel",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "60",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 870,
        "y": 1380,
        "wires": [
            [
                "84bf06adf76d82e3"
            ]
        ]
    },
    {
        "id": "84bf06adf76d82e3",
        "type": "change",
        "z": "7b74e2db318add18",
        "name": "Uhrzeit in h",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "$moment(payload.date).locale(\"de\").tz('Europe/Berlin').format('HH')",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1090,
        "y": 1380,
        "wires": [
            [
                "4101478478a454f1"
            ]
        ]
    },
    {
        "id": "4101478478a454f1",
        "type": "function",
        "z": "7b74e2db318add18",
        "name": "Uhr",
        "func": "msg.payload = Number(msg.payload);\nmsg.topic = \"Uhr\";\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1110,
        "y": 1340,
        "wires": [
            [
                "fb91e1a51ca592be"
            ]
        ]
    }
]

Question: Is it possible to convert in Jsonata expression? so that the function can be deleted?

JSONata has the $number() function for converting a string to a number.

yes I saw, but there comes always error:
$moment(payload.date).locale("de").tz('Europe/Berlin').format('HH')
$number(msg.payload)

Have you tried:

$number($moment(payload.date).locale("de").tz('Europe/Berlin').format('HH'))
1 Like

thank you,
just tried

$number($$moment(payload.date).locale("de").tz('Europe/Berlin').format('HH'))

:roll_eyes:

Jasonata is incomprehensible to me :crazy_face:

In that case I advise against using it. Use javascript instead. It is a little more efficient that way too.

ok, show me please ?

msg.payload = new Date().getHours()
// Or
// msg.payload = new Date(msg.payload.date).getHours()
return msg
1 Like

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