Min und Max Wert aus Array

blacksun80

New member
Hallo zusammen,

ich stehe noch am Anfang mit Node-Red. Ich möchte aus einem Array 0..23 das Element mit dem niedrigstem und höchstem Wert von "total" finden.

Es ist nur Element 0 und 1 aufgeklappt in dem Beispiel. Array geht aber von 0..23 und jedes Element enthält total.

Wie macht man denn sowas in NodeRed?

Code:
31.10.2023, 15:31:36node: debug 3
msg.payload : Object
object
viewer: object
homes: array[1]
0: object
currentSubscription: object
priceInfo: object
current: object
today: array[24]
[0 … 9]
0: object
total: 0.2545
energy: 0.086
tax: 0.1685
startsAt: "2023-10-31T00:00:00.000+01:00"
1: object
total: 0.2463
energy: 0.0792
tax: 0.1671
startsAt: "2023-10-31T01:00:00.000+01:00"
2: object
3: object
4: object
5: object
6: object
7: object
8: object
9: object
[10 … 19]
[20 … 23]
 
Code:
var Preise = msg.payload.viewer.homes[0].currentSubscription.priceInfo.today;

var maxPreis = Preise
maxPreis.sort((a,b) => b.total - a.total);

msg.payload.maxPreis = maxPreis[0].total;
return msg
 
Bei erneutem lesen habe ich dann auch festgestellt, dass ich ziemlichen Blödsinn geschrieben habe (geht ja um "max" und nicht um "avg", von daher war der average-Link direkt für die Katz') 😅 Aber gut, dass Du die Lösung gepostet hast, werden andere sicherlich auch nützlich finden (y)🙂
 
Warum macht ihr es Euch eigentlich so schwer mit JSONATA habt ihr doch alles an Board - entweder willst Du das ganze Objekt zurück oder nur den Wert. Es wäre halt auch toll - wenn man die Daten hier in CodeTags einbindet dann kann man das besser übernehmen und den Wert über das Debug Fenster und Wert kopieren übernimmt. So habe ich mal den relevanten Teil der payload nachgebaut:

Code:
{
  "payload": {
    "today": [
      {
        "total": 0.2545,
        "energy": 0.086,
        "tax": 0.1685,
        "startsAt": "2023-10-31T00:00:00.000+01:00"
      },
      {
        "total": 0.2463,
        "energy": 0.0792,
        "tax": 0.1671,
        "startsAt": "2023-10-31T01:00:00.000+01:00"
      }
    ]
  }
}

Wenn man nur an dem Wert an sich interessiert ist dann in JSONATA einfach die Funktion eingeben:

$min($$.payload.today.total)

Wenn man das ganze Objekt haben will dann eben:

payload.today[total=$min($$.payload.today.total)]

Kann man hier auch testen : https://try.jsonata.org/Wy8mPw3Rp

Das Ganze im Flow ist dann nur eine Change Node.

1701953711234.png

Hier der Flow zum Import:


JSON:
[
    {
        "id": "8dce1c28d9f22d53",
        "type": "inject",
        "z": "517cd16e4d7e6ae4",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "{\"today\":[{\"total\":0.2545,\"energy\":0.086,\"tax\":0.1685,\"startsAt\":\"2023-10-31T00:00:00.000+01:00\"},{\"total\":0.2463,\"energy\":0.0792,\"tax\":0.1671,\"startsAt\":\"2023-10-31T01:00:00.000+01:00\"}]}",
        "payloadType": "json",
        "x": 230,
        "y": 760,
        "wires": [
            [
                "b975ed9065adce56",
                "5d90c40c9fe8c246"
            ]
        ]
    },
    {
        "id": "b975ed9065adce56",
        "type": "change",
        "z": "517cd16e4d7e6ae4",
        "name": "Objekt mit max",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "payload.today[total=$max($$.payload.today.total)]",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 420,
        "y": 740,
        "wires": [
            [
                "c5647bc325c76277"
            ]
        ]
    },
    {
        "id": "5d90c40c9fe8c246",
        "type": "change",
        "z": "517cd16e4d7e6ae4",
        "name": "Objekt mit min",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "payload.today[total=$min($$.payload.today.total)]",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 420,
        "y": 780,
        "wires": [
            [
                "1602224df8079768"
            ]
        ]
    },
    {
        "id": "c5647bc325c76277",
        "type": "debug",
        "z": "517cd16e4d7e6ae4",
        "name": "Objekt mit max",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 680,
        "y": 740,
        "wires": []
    },
    {
        "id": "1602224df8079768",
        "type": "debug",
        "z": "517cd16e4d7e6ae4",
        "name": "Objekt mit min",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 680,
        "y": 780,
        "wires": []
    }
]
 
Zuletzt bearbeitet von einem Moderator:

Zurzeit aktive Besucher

Letzte Anleitungen

Statistik des Forums

Themen
4.595
Beiträge
47.011
Mitglieder
4.238
Neuestes Mitglied
Funkenmariechen
Zurück
Oben