Script with conditions

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

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

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

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)

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

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!

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

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 :sneezing_face:

is it better?

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

First Flow i get the Power....

[
    {
        "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

[
    {
        "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
    }
]

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.

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

{"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":""}]}}

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


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

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

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

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.

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