@mickym said in JSONata loop - $moment > Invalid date?:
Spaß macht es auf jeden Fall. Ich stoße beim tüfteln nur oft an meine Grenzen. JSONata ist für mich noch Neuland.
Nochmal vielen lieben Dank, dass du dich da so reingefuchst hast.
@mickym
Mein fertiger Flow für Feiertage und Schulferien zusammen:
[
{
"id": "2428c1679199ce79",
"type": "function",
"z": "2a5d5250.f178be",
"g": "d7ab92de65468601",
"name": "Global:Feiertag",
"func": "msg.payload = global.get(\"Frei\");\nreturn msg;",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 1740,
"y": 280,
"wires": [
[
"16744919a414ee37"
]
]
},
{
"id": "16744919a414ee37",
"type": "debug",
"z": "2a5d5250.f178be",
"g": "d7ab92de65468601",
"name": "Info",
"active": true,
"tosidebar": false,
"console": false,
"tostatus": true,
"complete": "payload",
"targetType": "msg",
"statusVal": "payload",
"statusType": "auto",
"x": 1910,
"y": 280,
"wires": []
},
{
"id": "27a3db497b5ec5d0",
"type": "inject",
"z": "2a5d5250.f178be",
"g": "d7ab92de65468601",
"name": "Abfrage",
"props": [
{
"p": "payload"
}
],
"repeat": "",
"crontab": "06 00 * * *",
"once": true,
"onceDelay": "0.5",
"topic": "",
"payload": "",
"payloadType": "date",
"x": 1560,
"y": 280,
"wires": [
[
"2428c1679199ce79"
]
]
},
{
"id": "2873966a84096291",
"type": "http request",
"z": "2a5d5250.f178be",
"g": "d7ab92de65468601",
"name": "https request",
"method": "GET",
"ret": "obj",
"paytoqs": "ignore",
"url": "",
"tls": "498c6e5f.1070f",
"persist": false,
"proxy": "",
"insecureHTTPParser": false,
"authType": "basic",
"senderr": false,
"headers": [],
"x": 1920,
"y": 120,
"wires": [
[
"7a7a0f91d75faecf"
]
]
},
{
"id": "98eaa50401fd8651",
"type": "change",
"z": "2a5d5250.f178be",
"g": "d7ab92de65468601",
"name": "",
"rules": [
{
"t": "set",
"p": "url",
"pt": "msg",
"to": "payload",
"tot": "msg"
},
{
"t": "set",
"p": "url",
"pt": "msg",
"to": "$.url",
"tot": "jsonata"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 1890,
"y": 60,
"wires": [
[
"2873966a84096291"
]
]
},
{
"id": "2b49ef7e9032ba65",
"type": "function",
"z": "2a5d5250.f178be",
"g": "d7ab92de65468601",
"name": "Url",
"func": "msg.payload = `https://openholidaysapi.org/SchoolHolidays?countryIsoCode=DE&validFrom=${msg.payload}-01-01&validTo=${msg.payload}-12-31&languageIsoCode=DE&subdivisionCode=DE-BY`;\nreturn msg;\n\n",
"outputs": 1,
"timeout": "",
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 1710,
"y": 60,
"wires": [
[
"98eaa50401fd8651"
]
]
},
{
"id": "668cd4c46954209e",
"type": "change",
"z": "2a5d5250.f178be",
"g": "d7ab92de65468601",
"name": "Jahr",
"rules": [
{
"t": "set",
"p": "topic",
"pt": "msg",
"to": "Jahr",
"tot": "str"
},
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "$moment().year() ",
"tot": "jsonata"
},
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "$string(payload)",
"tot": "jsonata"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 1710,
"y": 120,
"wires": [
[
"2b49ef7e9032ba65"
]
]
},
{
"id": "081d12e755b9ae21",
"type": "inject",
"z": "2a5d5250.f178be",
"g": "d7ab92de65468601",
"name": "Abfrage",
"props": [
{
"p": "payload"
}
],
"repeat": "",
"crontab": "05 00 * * *",
"once": true,
"onceDelay": "0.5",
"topic": "",
"payload": "",
"payloadType": "date",
"x": 1560,
"y": 140,
"wires": [
[
"668cd4c46954209e",
"ab40ce0d2f51afd7"
]
]
},
{
"id": "def42661118b4dd8",
"type": "change",
"z": "2a5d5250.f178be",
"g": "d7ab92de65468601",
"name": "",
"rules": [
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "payload.payload.isSame",
"tot": "jsonata"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 2310,
"y": 120,
"wires": [
[
"175999b28a5905ca"
]
]
},
{
"id": "1524c9b76d833a59",
"type": "http request",
"z": "2a5d5250.f178be",
"g": "d7ab92de65468601",
"name": "https request",
"method": "GET",
"ret": "obj",
"paytoqs": "ignore",
"url": "",
"tls": "498c6e5f.1070f",
"persist": false,
"proxy": "",
"insecureHTTPParser": false,
"authType": "basic",
"senderr": false,
"headers": [],
"x": 1920,
"y": 160,
"wires": [
[
"ecf91cc75ee58cec"
]
]
},
{
"id": "ab40ce0d2f51afd7",
"type": "change",
"z": "2a5d5250.f178be",
"g": "d7ab92de65468601",
"name": "Jahr",
"rules": [
{
"t": "set",
"p": "topic",
"pt": "msg",
"to": "Jahr",
"tot": "str"
},
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "$moment().year() ",
"tot": "jsonata"
},
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "$string(payload)",
"tot": "jsonata"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 1710,
"y": 160,
"wires": [
[
"12625be60bc8538d"
]
]
},
{
"id": "12625be60bc8538d",
"type": "function",
"z": "2a5d5250.f178be",
"g": "d7ab92de65468601",
"name": "Url",
"func": "msg.payload = `https://openholidaysapi.org/PublicHolidays?countryIsoCode=DE&validFrom=${msg.payload}-01-01&validTo=${msg.payload}-12-31&languageIsoCode=DE&subdivisionCode=DE-BY`;\nreturn msg;",
"outputs": 1,
"timeout": "",
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 1710,
"y": 220,
"wires": [
[
"1ffa03fb20ce426e"
]
]
},
{
"id": "1ffa03fb20ce426e",
"type": "change",
"z": "2a5d5250.f178be",
"g": "d7ab92de65468601",
"name": "",
"rules": [
{
"t": "set",
"p": "url",
"pt": "msg",
"to": "payload",
"tot": "msg"
},
{
"t": "set",
"p": "url",
"pt": "msg",
"to": "$.url",
"tot": "jsonata"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 1890,
"y": 220,
"wires": [
[
"1524c9b76d833a59"
]
]
},
{
"id": "ecf91cc75ee58cec",
"type": "change",
"z": "2a5d5250.f178be",
"g": "d7ab92de65468601",
"name": "Feiertage",
"rules": [
{
"t": "delete",
"p": "headers",
"pt": "msg"
},
{
"t": "delete",
"p": "statusCode",
"pt": "msg"
},
{
"t": "delete",
"p": "url",
"pt": "msg"
},
{
"t": "delete",
"p": "responseUrl",
"pt": "msg"
},
{
"t": "delete",
"p": "retry",
"pt": "msg"
},
{
"t": "delete",
"p": "redirectList",
"pt": "msg"
},
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "payload.{\t \"Feiertag\": $.name[language=\"DE\"].text,\t \"payload\": (\t $date := $moment($.startDate,'YYYY-MM-DD');\t $today := $moment();\t $date.isSame($today)\t )\t }",
"tot": "jsonata"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 2100,
"y": 160,
"wires": [
[
"3da676eba96a5168"
]
]
},
{
"id": "8a5a46b1a1839d84",
"type": "debug",
"z": "2a5d5250.f178be",
"g": "d7ab92de65468601",
"name": "Info",
"active": true,
"tosidebar": false,
"console": false,
"tostatus": true,
"complete": "payload",
"targetType": "msg",
"statusVal": "payload",
"statusType": "auto",
"x": 2990,
"y": 140,
"wires": []
},
{
"id": "83e59a86c8412805",
"type": "function",
"z": "2a5d5250.f178be",
"g": "d7ab92de65468601",
"name": "includes(true)",
"func": "const Frei = msg.payload[0] && msg.payload[1];\nmsg.payload = Frei.includes(true);\nreturn msg;",
"outputs": 1,
"timeout": "",
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 2680,
"y": 140,
"wires": [
[
"ad9391d7ef4f1de7"
]
]
},
{
"id": "3da676eba96a5168",
"type": "change",
"z": "2a5d5250.f178be",
"g": "d7ab92de65468601",
"name": "",
"rules": [
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "payload.payload",
"tot": "jsonata"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 2310,
"y": 160,
"wires": [
[
"175999b28a5905ca"
]
]
},
{
"id": "175999b28a5905ca",
"type": "join",
"z": "2a5d5250.f178be",
"g": "d7ab92de65468601",
"name": "",
"mode": "custom",
"build": "array",
"property": "payload",
"propertyType": "msg",
"key": "topic",
"joiner": "\\n",
"joinerType": "str",
"accumulate": false,
"timeout": "",
"count": "2",
"reduceRight": false,
"reduceExp": "",
"reduceInit": "",
"reduceInitType": "",
"reduceFixup": "",
"x": 2510,
"y": 140,
"wires": [
[
"83e59a86c8412805"
]
]
},
{
"id": "7a7a0f91d75faecf",
"type": "change",
"z": "2a5d5250.f178be",
"g": "d7ab92de65468601",
"name": "Schulferien",
"rules": [
{
"t": "delete",
"p": "headers",
"pt": "msg"
},
{
"t": "delete",
"p": "statusCode",
"pt": "msg"
},
{
"t": "delete",
"p": "url",
"pt": "msg"
},
{
"t": "delete",
"p": "responseUrl",
"pt": "msg"
},
{
"t": "delete",
"p": "retry",
"pt": "msg"
},
{
"t": "delete",
"p": "redirectList",
"pt": "msg"
},
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "payload.{\t \"Schulferien\": $.name[language=\"DE\"].text,\t \"payload\": (\t $start := $moment($moment($.startDate,'YYYY-MM-DD').format('DD.MM.YYYY'),['DD.MM.YYYY','x'],'de');\t $end := $moment($moment($.endDate,'YYYY-MM-DD').format('DD.MM.YYYY'),['DD.MM.YYYY','x'],'de');\t $diff := $end.diff($start, 'days');\t ([0..$diff])@$day.{\t \"date\": $moment($.startDate,'YYYY-MM-DD').add($day,'days').format('DD.MM.YYYY'),\t \"today\": $moment().format('DD.MM.YYYY'),\t \"isSame\": $moment($.startDate,'YYYY-MM-DD').add($day,'days').isSame($moment(),'days')\t } \t )\t }",
"tot": "jsonata"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 2110,
"y": 120,
"wires": [
[
"def42661118b4dd8"
]
]
},
{
"id": "ad9391d7ef4f1de7",
"type": "change",
"z": "2a5d5250.f178be",
"g": "d7ab92de65468601",
"name": "Frei",
"rules": [
{
"t": "set",
"p": "topic",
"pt": "msg",
"to": "Frei",
"tot": "str"
},
{
"t": "set",
"p": "Frei",
"pt": "global",
"to": "payload",
"tot": "msg"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 2850,
"y": 140,
"wires": [
[
"8a5a46b1a1839d84"
]
]
},
{
"id": "498c6e5f.1070f",
"type": "tls-config",
"name": "",
"cert": "",
"key": "",
"ca": "",
"certname": "",
"keyname": "",
"caname": "",
"servername": "",
"verifyservercert": false,
"alpnprotocol": ""
}
]
Nebenbei. Ist absolut erstaunlich wie enorm man einen Flow verschlanken kann.
- Flow für die Feiertage:
- Flow für die Feiertage:
Und der absolute Wahnsinn war das Abrufen auf einmal.
Dadurch redziert sich alleine bei den Feiertagen das JSONata von 106 auf 8 Zeilen in der change-node.