# Script with conditions

**URL:** <https://discourse.nodered.org/t/script-with-conditions/75863>\
**Category:** General\
**Created:** [28 February 2023 07:58 UTC](https://discourse.nodered.org/t/script-with-conditions/75863 "2023-02-28T07:58:51Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![einfach112](https://avatars.discourse-cdn.com/v4/letter/e/3da27b/32.png) [@einfach112](https://discourse.nodered.org/u/einfach112)\
**Post date:** [28 February 2023 07:58 UTC](https://discourse.nodered.org/t/script-with-conditions/75863/1 "2023-02-28T07:58:52Z")

</div>

Hi,  
i have the follow function to start my power socket:

```auto
var thr = flow.get("Threshold");
msg.threshold = thr;

var durchschnitt = msg.payload;
msg.durchschnitt = durchschnitt;

if(durchschnitt <= thr)
{
    // AN
    msg.payload = 'on';
    flow.set("Threshold", -50);
}
else
{
    // AUS
    msg.payload = 'off';
    flow.set("Threshold", -1050);
}
return msg;

```

the payload with the data comes every Second.  
If the payload-data say : -1050 my power socket start.  
if the payload-data say \< -50 my power socket stop.

My biggest problem is:  
if the payload-data -1050 = send signal on; one second later: payload-data -1051 =send signal on.... one second later payload-data -750 send signal on......  
You can see that every second is sending a command. The controll every second is fine! But not send every second a command.  
Can somewone tell my, how i can send a command only the state have to change?  
Sorry for my spelling

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [28 February 2023 08:14 UTC](https://discourse.nodered.org/t/script-with-conditions/75863/2 "2023-02-28T08:14:57Z")

</div>

Why not save the current state? Read it at the start of the function and before the return test it. For example

```auto
var thr = flow.get("Threshold");
let state = flow.get("state") || "unknown";
msg.threshold = thr;

var durchschnitt = msg.payload;
msg.durchschnitt = durchschnitt;

if (durchschnitt <= thr) {
    // AN
    msg.payload = 'on';
    flow.set("Threshold", -50);
    flow.set("state", "on")
}
else {
    // AUS
    msg.payload = 'off';
    flow.set("Threshold", -1050);
    flow.set("state", "off")
}
if (state == msg.payload) return null;

return msg;

```

(Untested)

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [28 February 2023 09:56 UTC](https://discourse.nodered.org/t/script-with-conditions/75863/3 "2023-02-28T09:56:45Z")

</div>

You can also use a filter node to block duplicate on/off messages.

---

<div class="post-metadata">

**Author:** ![einfach112](https://avatars.discourse-cdn.com/v4/letter/e/3da27b/32.png) [@einfach112](https://discourse.nodered.org/u/einfach112)\
**Post date:** [28 February 2023 13:52 UTC](https://discourse.nodered.org/t/script-with-conditions/75863/4 "2023-02-28T13:52:47Z")

</div>

Hello zenofmud ! ! !  
Thank you ! ! ! ! ! ! sorry i´m only a systemintegratorm i´m not a Programmer!  
yeha it work fine!  
Sorry for my copy & paste code

Yeha ... Happiness  
Thank you again!

---

<div class="post-metadata">

**Author:** ![einfach112](https://avatars.discourse-cdn.com/v4/letter/e/3da27b/32.png) [@einfach112](https://discourse.nodered.org/u/einfach112)\
**Post date:** [13 March 2023 15:40 UTC](https://discourse.nodered.org/t/script-with-conditions/75863/5 "2023-03-13T15:40:35Z")

</div>

Hi Zenofmud!  
Can you help me again?  
Now i have a second System with more Power.....

I would switch the second device after the first...BUT  
The first one have 1050 W Power  
The second one have 1750 W Power

If the first one start at -1050 W the power injection go down to -70 and always is fine  
That's exactly what we have.

But now i need a second XXX System.  
I don't know how can i make a discription......  
But i think in need one like....  
If System 1 Run (1050) & Power ist more than -700 then System 1 go off and system 2 start.  
If the power is \> -50 stop system 2, and then again if power -1050 then start system 1  
i hope you can understand what i want.  
Maybe it's easier to understand if the whole project is briefly described. I have a solar system on the roof. I have an IR read head on my electricity meter. This gives me the current feed. So if 1050 watts are fed in, then socket 1 to which a heater is connected should start. When a total of 1750 watts (System1 + an additional 700 watts) are available, heating 1 should switch off and heating 2 start.....

Thanks in advance. Greetings from Germany

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [13 March 2023 16:00 UTC](https://discourse.nodered.org/t/script-with-conditions/75863/6 "2023-03-13T16:00:41Z")

</div>

I read what you wrote but don't understand. (this be because I think I'm coming down with a head cold)

Draw a block diagram of what you have and what you want to do and upload it. Who knows, just drawing it out might allow you to figure this out yourself 🤧

---

<div class="post-metadata">

**Author:** ![einfach112](https://avatars.discourse-cdn.com/v4/letter/e/3da27b/32.png) [@einfach112](https://discourse.nodered.org/u/einfach112)\
**Post date:** [13 March 2023 17:56 UTC](https://discourse.nodered.org/t/script-with-conditions/75863/7 "2023-03-13T17:56:04Z")

</div>

![funktionsweise](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/2/c2e7295a7935e7be4b98638c714ac06655101586.png)

is it better?

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [14 March 2023 16:08 UTC](https://discourse.nodered.org/t/script-with-conditions/75863/8 "2023-03-14T16:08:55Z")

</div>

Please export your current flow and attach it to a reply that way I can see what you are doing.

---

<div class="post-metadata">

**Author:** ![einfach112](https://avatars.discourse-cdn.com/v4/letter/e/3da27b/32.png) [@einfach112](https://discourse.nodered.org/u/einfach112)\
**Post date:** [15 March 2023 08:03 UTC](https://discourse.nodered.org/t/script-with-conditions/75863/9 "2023-03-15T08:03:41Z")

</div>

First Flow i get the Power....

```auto
[
    {
        "id": "ddd17256ad3d16a3",
        "type": "tab",
        "label": "Einspeisung - Zählerstände",
        "disabled": false,
        "info": "",
        "env": [
            {
                "name": "Threshold",
                "value": "-1050",
                "type": "num"
            }
        ]
    },
    {
        "id": "3f1db29997650257",
        "type": "smartmeter",
        "z": "ddd17256ad3d16a3",
        "name": "VZahlerstandZ1",
        "datasource": "0fb0b4f0cce9c7a9",
        "protocol": "SmlProtocol",
        "transport": "SerialResponseTransport",
        "requestInterval": "1",
        "d0WakeupCharacters": "",
        "d0SignOnMessage": "",
        "d0BaudrateChangeoverOverwrite": "",
        "protocolSmlIgnoreInvalidCRC": false,
        "debugging": true,
        "x": 100,
        "y": 80,
        "wires": [
            [
                "204c4d6691f0f95b",
                "5dc24479545f7393",
                "9b11b3dde8e4456b",
                "f393b7a51e8505c4"
            ]
        ]
    },
    {
        "id": "5dc24479545f7393",
        "type": "debug",
        "z": "ddd17256ad3d16a3",
        "name": "Ausgelesene Daten",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 130,
        "y": 140,
        "wires": []
    },
    {
        "id": "204c4d6691f0f95b",
        "type": "function",
        "z": "ddd17256ad3d16a3",
        "name": "Auslesen der Wattzahl",
        "func": "var StromZ2A = msg.payload[\"1-0:16.7.0*255\"].values[0].value;\n\nreturn [\n { payload: StromZ2A },\n]",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 320,
        "y": 80,
        "wires": [
            [
                "bc59e3835caff765",
                "c6974c6e6932abdb",
                "1021df5268aa3460"
            ]
        ]
    },
    {
        "id": "bc59e3835caff765",
        "type": "debug",
        "z": "ddd17256ad3d16a3",
        "name": "Watt Einspeisung / Verbrauch Live pro Sekunde",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 640,
        "y": 20,
        "wires": []
    },
    {
        "id": "c6974c6e6932abdb",
        "type": "smooth",
        "z": "ddd17256ad3d16a3",
        "name": "Durchschnitt berechnen",
        "property": "payload",
        "action": "mean",
        "count": "3",
        "round": "0",
        "mult": "multi",
        "reduce": true,
        "x": 570,
        "y": 80,
        "wires": [
            [
                "f277c1d6d773992d",
                "e71926ad6b59b7f5"
            ]
        ]
    },
    {
        "id": "9b11b3dde8e4456b",
        "type": "function",
        "z": "ddd17256ad3d16a3",
        "name": "Zählerstand Einkauf",
        "func": "var StromZ1A = msg.payload[\"1-0:1.8.0*255\"].values[0].value;\n\nreturn [\n { payload: StromZ1A },\n]",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 140,
        "y": 220,
        "wires": [
            [
                "d7022a8e40019a6f",
                "8305383cc5422ac6"
            ]
        ]
    },
    {
        "id": "d7022a8e40019a6f",
        "type": "debug",
        "z": "ddd17256ad3d16a3",
        "name": "Zählerstand Einkauf",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 140,
        "y": 280,
        "wires": []
    },
    {
        "id": "f393b7a51e8505c4",
        "type": "function",
        "z": "ddd17256ad3d16a3",
        "name": "Zählerstand Verkauf",
        "func": "var StromZ3A = msg.payload[\"1-0:2.8.0*255\"].values[0].value;\n\nreturn [\n { payload: StromZ3A },\n]",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 140,
        "y": 340,
        "wires": [
            [
                "62a501f2b9e43e4a",
                "02aade26ac4759a8"
            ]
        ]
    },
    {
        "id": "62a501f2b9e43e4a",
        "type": "debug",
        "z": "ddd17256ad3d16a3",
        "name": "Zählerstand Verkauf",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 140,
        "y": 400,
        "wires": []
    },
    {
        "id": "02aade26ac4759a8",
        "type": "change",
        "z": "ddd17256ad3d16a3",
        "name": "übergebe Zählerstand Verkauf zum Template",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "$round(payload, 0)\t",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 450,
        "y": 340,
        "wires": [
            [
                "e534c9db3955e7f5"
            ]
        ]
    },
    {
        "id": "8305383cc5422ac6",
        "type": "change",
        "z": "ddd17256ad3d16a3",
        "name": "übergebe Zählerstand Einkauf zum Template",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "$round(payload, 0)\t",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 450,
        "y": 220,
        "wires": [
            [
                "4ae9d2d26bec3f2e"
            ]
        ]
    },
    {
        "id": "1021df5268aa3460",
        "type": "ui_gauge",
        "z": "ddd17256ad3d16a3",
        "name": "Stromverbauch live Gauge",
        "group": "3063edc21d564397",
        "order": 1,
        "width": 0,
        "height": 0,
        "gtype": "gage",
        "title": "STROM",
        "label": "units",
        "format": "{{value}}",
        "min": "-3800",
        "max": "6000",
        "colors": [
            "#00b500",
            "#e6e600",
            "#ca3838"
        ],
        "seg1": "",
        "seg2": "",
        "className": "",
        "x": 580,
        "y": 140,
        "wires": []
    },
    {
        "id": "4ae9d2d26bec3f2e",
        "type": "ui_template",
        "z": "ddd17256ad3d16a3",
        "group": "941b98732acb116e",
        "name": "Ausgabe Einkauf",
        "order": 0,
        "width": 0,
        "height": 0,
        "format": "EINKAUF\n<div ng-bind-html=\"msg.payload\"></div>",
        "storeOutMessages": true,
        "fwdInMessages": true,
        "resendOnRefresh": true,
        "templateScope": "local",
        "className": "",
        "x": 750,
        "y": 220,
        "wires": [
            []
        ]
    },
    {
        "id": "e534c9db3955e7f5",
        "type": "ui_template",
        "z": "ddd17256ad3d16a3",
        "group": "941b98732acb116e",
        "name": "Ausgabe Verkauf",
        "order": 1,
        "width": 0,
        "height": 0,
        "format": "Verkauf\n<div ng-bind-html=\"msg.payload\"></div>",
        "storeOutMessages": true,
        "fwdInMessages": true,
        "resendOnRefresh": true,
        "templateScope": "local",
        "className": "",
        "x": 750,
        "y": 340,
        "wires": [
            []
        ]
    },
    {
        "id": "f277c1d6d773992d",
        "type": "debug",
        "z": "ddd17256ad3d16a3",
        "name": "Durchschnitt ausgabe",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 820,
        "y": 120,
        "wires": []
    },
    {
        "id": "e71926ad6b59b7f5",
        "type": "link out",
        "z": "ddd17256ad3d16a3",
        "name": "durschnitt ausgabe",
        "mode": "link",
        "links": [
            "9182d91383ad9eb7",
            "3b6f81c71fc4d5fe"
        ],
        "x": 735,
        "y": 80,
        "wires": []
    },
    {
        "id": "0fb0b4f0cce9c7a9",
        "type": "smartmeter-connection",
        "sourcetype": "serial",
        "serialport": "/dev/ttyUSB0",
        "serialbaud": "9600",
        "databits": "8",
        "parity": "none",
        "stopbits": "1",
        "httphost": "localhost",
        "httpport": "80",
        "tcphost": "localhost",
        "tcpport": "502",
        "filepath": "/dev/null"
    },
    {
        "id": "3063edc21d564397",
        "type": "ui_group",
        "name": "Aktuelle Einspeisung Verbrauch",
        "tab": "fad73bed835f5485",
        "order": 1,
        "disp": true,
        "width": "6",
        "collapse": false,
        "className": ""
    },
    {
        "id": "941b98732acb116e",
        "type": "ui_group",
        "name": "Zählerstände",
        "tab": "fad73bed835f5485",
        "order": 2,
        "disp": true,
        "width": "6",
        "collapse": false,
        "className": ""
    },
    {
        "id": "fad73bed835f5485",
        "type": "ui_tab",
        "name": "Übersicht",
        "icon": "dashboard",
        "order": 1,
        "disabled": false,
        "hidden": false
    }
]

```

next flow i want to switch the power plugs

```auto
[
    {
        "id": "af5613b75f0b562a",
        "type": "tab",
        "label": "E-Heizungen",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "3b6f81c71fc4d5fe",
        "type": "link in",
        "z": "af5613b75f0b562a",
        "name": "link in 1",
        "links": [
            "e71926ad6b59b7f5"
        ],
        "x": 35,
        "y": 80,
        "wires": [
            [
                "4a11a0d4d5ac42db",
                "286491866f8a0c91"
            ]
        ]
    },
    {
        "id": "4a11a0d4d5ac42db",
        "type": "function",
        "z": "af5613b75f0b562a",
        "name": "Befehl An/Aus erstellen",
        "func": "var thr = flow.get(\"Threshold\");\nlet state = flow.get(\"state\") || \"unknown\";\nmsg.threshold = thr;\n\nvar durchschnitt = msg.payload;\nmsg.durchschnitt = durchschnitt;\n\nif (durchschnitt <= thr) {\n // AN\n msg.payload = 'on';\n flow.set(\"Threshold\", -50);\n flow.set(\"state\", \"on\");\n}\nelse \n{\n // AUS\n msg.payload = 'off';\n flow.set(\"Threshold\", -1050);\n flow.set(\"state\", \"off\");\n}\nif (state == msg.payload) return null ;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 210,
        "y": 100,
        "wires": [
            [
                "6e6f640bec6d1f8c",
                "de6844fd24344d17"
            ]
        ],
        "outputLabels": [
            "on"
        ]
    },
    {
        "id": "286491866f8a0c91",
        "type": "debug",
        "z": "af5613b75f0b562a",
        "name": "Ø Verbrauch / Einspeisung",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 220,
        "y": 40,
        "wires": []
    },
    {
        "id": "5d3f8a151369d559",
        "type": "alexa-remote-routine",
        "z": "af5613b75f0b562a",
        "name": "Heizung 1050W einschalten",
        "account": "f8eeb77e2edc13dc",
        "routineNode": {
            "type": "routine",
            "payload": {
                "routine": {
                    "type": "str",
                    "value": "amzn1.alexa.automation.xxx"
                },
                "device": {
                    "type": "str",
                    "value": "G091JJ1xxxx"
                }
            }
        },
        "x": 720,
        "y": 60,
        "wires": [
            []
        ]
    },
    {
        "id": "b647244a04e0ccf7",
        "type": "alexa-remote-routine",
        "z": "af5613b75f0b562a",
        "name": "Heizung 1050W ausschalten",
        "account": "f8eeb77xxxxxxxxx",
        "routineNode": {
            "type": "routine",
            "payload": {
                "routine": {
                    "type": "str",
                    "value": "amzn1.alexa.automation.133913dd-1f40xxxxxxxxxxxxxx"
                },
                "device": {
                    "type": "str",
                    "value": "G091JJxxxxxx"
                }
            }
        },
        "x": 720,
        "y": 120,
        "wires": [
            []
        ]
    },
    {
        "id": "6e6f640bexxxxxxx",
        "type": "switch",
        "z": "af5613b75f0b562a",
        "name": "",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "on",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "off",
                "vt": "str"
            }
        ],
        "checkall": "false",
        "repair": false,
        "outputs": 2,
        "x": 430,
        "y": 100,
        "wires": [
            [
                "5d3f8a15136xxxxx",
                "e6f7361e153axxxxxx"
            ],
            [
                "b647244a04exxxxxx",
                "e6f7361e153xxxxxx"
            ]
        ]
    },
    {
        "id": "de6844fd24344d17",
        "type": "debug",
        "z": "af5613b75f0b562a",
        "name": "aktueller status ... wenn leer dann off",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 210,
        "y": 160,
        "wires": []
    },
    {
        "id": "e6f7361e15xxxxx",
        "type": "ui_text",
        "z": "af5613b75f0b562a",
        "group": "c6f8431344dcd050",
        "order": 1,
        "width": 0,
        "height": 0,
        "name": "Status Heizung 1050W",
        "label": "",
        "format": "{{msg.payload}}",
        "layout": "col-center",
        "className": "",
        "x": 700,
        "y": 180,
        "wires": []
    },
    {
        "id": "a4cbe16f94941f40",
        "type": "ui_text",
        "z": "af5613b75f0b562a",
        "group": "7d7fb540b925c2c9",
        "order": 1,
        "width": 0,
        "height": 0,
        "name": "Status Heizung 1750",
        "label": "",
        "format": "{{msg.payload}}",
        "layout": "col-center",
        "className": "",
        "x": 700,
        "y": 360,
        "wires": []
    },
    {
        "id": "b67c2e959bf532f3",
        "type": "alexa-remote-routine",
        "z": "af5613b75f0b562a",
        "name": "Heizung 1750W einschalten",
        "account": "f8eeb77e2edc13dc",
        "routineNode": {
            "type": "routine",
            "payload": {
                "routine": {
                    "type": "str",
                    "value": "amzn1.alexa.automation.ae7b8474-4xxxxxxxxxxxxxxxxx"
                },
                "device": {
                    "type": "str",
                    "value": "G091JJ1421xxxxxxxxxxxx"
                }
            }
        },
        "x": 720,
        "y": 240,
        "wires": [
            []
        ]
    },
    {
        "id": "f2bc9f193c11ff9a",
        "type": "alexa-remote-routine",
        "z": "af5613b75f0b562a",
        "name": "Heizung 1750W ausschalten",
        "account": "f8eeb7xxxxxxxxx",
        "routineNode": {
            "type": "routine",
            "payload": {
                "routine": {
                    "type": "str",
                    "value": "amzn1.alexa.automation.040f7996-fefe-4d7xxxxxxxxxxxx"
                },
                "device": {
                    "type": "str",
                    "value": "G091JJ14xxxxxxx"
                }
            }
        },
        "x": 720,
        "y": 300,
        "wires": [
            []
        ]
    },
    {
        "id": "f8eeb77e2edc13dc",
        "type": "alexa-remote-account",
        "name": "my-account",
        "authMethod": "proxy",
        "proxyOwnIp": "192.168.0.xxx",
        "proxyPort": "3456",
        "cookieFile": "/home/pi/alexa.txt",
        "refreshInterval": "3",
        "alexaServiceHost": "layla.amazon.de",
        "amazonPage": "amazon.de",
        "acceptLanguage": "de-DE",
        "userAgent": "",
        "useWsMqtt": "on",
        "autoInit": "on"
    },
    {
        "id": "c6f8431344dcd050",
        "type": "ui_group",
        "name": "E-Heizung 1 - 1000 Watt",
        "tab": "fad73bed835f5485",
        "order": 4,
        "disp": true,
        "width": "6",
        "collapse": false,
        "className": ""
    },
    {
        "id": "7d7fb540b925c2c9",
        "type": "ui_group",
        "name": "E-Heizung 2 - 1750 Watt",
        "tab": "fad73bed835f5485",
        "order": 5,
        "disp": true,
        "width": "6",
        "collapse": false,
        "className": ""
    },
    {
        "id": "fad73bed835f5485",
        "type": "ui_tab",
        "name": "Übersicht",
        "icon": "dashboard",
        "order": 1,
        "disabled": false,
        "hidden": false
    }
]

```

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [15 March 2023 08:26 UTC](https://discourse.nodered.org/t/script-with-conditions/75863/10 "2023-03-15T08:26:36Z")

</div>

Please activate the Ausgelesene Daten debug node and expand the msg payload in the debug sidebar. Then copy a few of the results and paste them into a reply so I will have some data to play with.

---

<div class="post-metadata">

**Author:** ![einfach112](https://avatars.discourse-cdn.com/v4/letter/e/3da27b/32.png) [@einfach112](https://discourse.nodered.org/u/einfach112)\
**Post date:** [15 March 2023 08:29 UTC](https://discourse.nodered.org/t/script-with-conditions/75863/11 "2023-03-15T08:29:10Z")

</div>

goog morning.... thank you, for your fast reply.....

```auto
{"129-129:199.130.3*255":{"medium":129,"channel":129,"measurement":199,"measureType":130,"tariffRate":3,"previousMeasurement":255,"values":[{"value":"EMH","unit":""}]},"1-0:0.0.9*255":{"medium":1,"channel":0,"measurement":0,"measureType":0,"tariffRate":9,"previousMeasurement":255,"values":[{"value":"0901454d4800004737f4","unit":""}]},"1-0:1.8.0*255":{"medium":1,"channel":0,"measurement":1,"measureType":8,"tariffRate":0,"previousMeasurement":255,"values":[{"value":31209.5955,"unit":"kWh"}]},"1-0:2.8.0*255":{"medium":1,"channel":0,"measurement":2,"measureType":8,"tariffRate":0,"previousMeasurement":255,"values":[{"value":17672.8215,"unit":"kWh"}]},"1-0:1.8.1*255":{"medium":1,"channel":0,"measurement":1,"measureType":8,"tariffRate":1,"previousMeasurement":255,"values":[{"value":31209.5955,"unit":"kWh"}]},"1-0:2.8.1*255":{"medium":1,"channel":0,"measurement":2,"measureType":8,"tariffRate":1,"previousMeasurement":255,"values":[{"value":17672.8215,"unit":"kWh"}]},"1-0:1.8.2*255":{"medium":1,"channel":0,"measurement":1,"measureType":8,"tariffRate":2,"previousMeasurement":255,"values":[{"value":0,"unit":"kWh"}]},"1-0:2.8.2*255":{"medium":1,"channel":0,"measurement":2,"measureType":8,"tariffRate":2,"previousMeasurement":255,"values":[{"value":0,"unit":"kWh"}]},"1-0:16.7.0*255":{"medium":1,"channel":0,"measurement":16,"measureType":7,"tariffRate":0,"previousMeasurement":255,"values":[{"value":-331.3,"unit":"W"}]},"129-129:199.130.5*255":{"medium":129,"channel":129,"measurement":199,"measureType":130,"tariffRate":5,"previousMeasurement":255,"values":[{"value":"c0cd74b35ab9015061300dd599907222b4c92a4d0b039849c8331be0040d61379e4b93633faaa1acc7f368d74167829d","unit":""}]}}

```

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [15 March 2023 09:21 UTC](https://discourse.nodered.org/t/script-with-conditions/75863/12 "2023-03-15T09:21:19Z")

</div>

When I said to create a block diagram, I should have said create a flowchart that shows what will happen. Something like this

 ![Screenshot 2023-03-15 at 5.16.36 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/d/4d542ae8e0c27c9f6a479915ded1117be668aa2b.png)  
You need to build a complete flow chart of what you need to do because I don't understand your writeup.

You can draw it on paper or use the [site I used to produce this above flowchart](https://app.diagrams.net)

Doing this will help you determine how your system will work.

---

<div class="post-metadata">

**Author:** ![einfach112](https://avatars.discourse-cdn.com/v4/letter/e/3da27b/32.png) [@einfach112](https://discourse.nodered.org/u/einfach112)\
**Post date:** [15 March 2023 09:51 UTC](https://discourse.nodered.org/t/script-with-conditions/75863/13 "2023-03-15T09:51:52Z")

</div>

WOW thats a cool Website to create a flowchart.  
But you can see ..... its not my passion.....but i hope that you now can see what i need  
 ![OMG](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/5/253a0cd9777c2cf9ad943f155d765a1b5a469d19.png)

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [15 March 2023 10:39 UTC](https://discourse.nodered.org/t/script-with-conditions/75863/14 "2023-03-15T10:39:12Z")

</div>

Ok in flowcharting there are some standards

1. diamonds represent 'decisions' - make sure to label the outputs showing the result of the decision
2. rectangles represent actions to take

Let's look at the first column in your flow chart.

1. smart meter reading comes in and passed to a decision (diamond).  
-- If it is less than -1500W (NO) you do nothing (rectangle)  
-- If it is more than -1500W (YES) it goes to...
2. You have a decision block (diamond) when this should be a process (rectangle)
3. you then have a circle but. you actually are making a decision and doing a process. So this should be a decision (diamond) asking the question, connected to a rectangle (process) showing the action to take (stopping heater 1)

But when you look at that logic, you previously tested to see if the power was greater that -1050W (anything from -1049 to positive infinity)  
Now you want to test if heater 1 is on but you know it is because you just started it. So you are really checking if the power is less than -50 and if true, you stop heater 1

It seems to me, you should start by checking if heater 1 is on or off.  
if it is on and the power is less than -50, you turn it off and are done.  
if it is off and the power is greater than -1050, you turn it on.

it seems to me that the first thing you need to ask is "Is heater 1 on?" and then "is heater 2 on?" and work from there.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [29 March 2023 10:40 UTC](https://discourse.nodered.org/t/script-with-conditions/75863/15 "2023-03-29T10:40:10Z")

</div>

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