Energy Meter Flow for Node Red for Shelly Pro3em

hello I need help.
I want to build a flow for node red that calculate and send through mqtt energy consumption of my house measured in my shelly pro 3 em.
anyone can help me?

ok i make this… it works well but anytime I reboot node… it resets
any help?

[
    {
        "id": "d207741a429718f0",
        "type": "debug",
        "z": "9bfd23d692c499bb",
        "name": "debug 5",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 820,
        "y": 420,
        "wires": []
    },
    {
        "id": "fce69fa3d3263b3d",
        "type": "server-state-changed",
        "z": "9bfd23d692c499bb",
        "name": "Consumo Bocchio",
        "server": "e8e7fd0.247eb",
        "version": 4,
        "exposeToHomeAssistant": false,
        "haConfig": [
            {
                "property": "name",
                "value": ""
            },
            {
                "property": "icon",
                "value": ""
            }
        ],
        "entityidfilter": "sensor.consumo_bocchio_total_active_energy",
        "entityidfiltertype": "exact",
        "outputinitially": false,
        "state_type": "str",
        "haltifstate": "",
        "halt_if_type": "str",
        "halt_if_compare": "is",
        "outputs": 1,
        "output_only_on_state_change": true,
        "for": "0",
        "forType": "num",
        "forUnits": "seconds",
        "ignorePrevStateNull": false,
        "ignorePrevStateUnknown": false,
        "ignorePrevStateUnavailable": false,
        "ignoreCurrentStateUnknown": false,
        "ignoreCurrentStateUnavailable": false,
        "outputProperties": [
            {
                "property": "payload",
                "propertyType": "msg",
                "value": "",
                "valueType": "entityState"
            },
            {
                "property": "data",
                "propertyType": "msg",
                "value": "",
                "valueType": "eventData"
            },
            {
                "property": "topic",
                "propertyType": "msg",
                "value": "",
                "valueType": "triggerId"
            }
        ],
        "x": 130,
        "y": 300,
        "wires": [
            [
                "42c09cf1dd9c7fb3"
            ]
        ]
    },
    {
        "id": "42c09cf1dd9c7fb3",
        "type": "change",
        "z": "9bfd23d692c499bb",
        "name": "w",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "payload.values[0].value_scaled",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 310,
        "y": 300,
        "wires": [
            [
                "d51a1863a0f12473",
                "152feed774c6238f",
                "998a795ca7707563",
                "822f6e5d67b41022"
            ]
        ]
    },
    {
        "id": "822f6e5d67b41022",
        "type": "change",
        "z": "9bfd23d692c499bb",
        "name": "set current",
        "rules": [
            {
                "t": "set",
                "p": "energy",
                "pt": "flow",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 450,
        "y": 220,
        "wires": [
            []
        ]
    },
    {
        "id": "e7f6f16d64d9c2b8",
        "type": "inject",
        "z": "9bfd23d692c499bb",
        "name": "newday",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "00 00 * * *",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 100,
        "y": 380,
        "wires": [
            [
                "fe3228d4b16feb32"
            ]
        ]
    },
    {
        "id": "fe3228d4b16feb32",
        "type": "change",
        "z": "9bfd23d692c499bb",
        "name": "set day start",
        "rules": [
            {
                "t": "set",
                "p": "day_start_energy",
                "pt": "flow",
                "to": "energy",
                "tot": "flow"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 260,
        "y": 380,
        "wires": [
            [
                "d51a1863a0f12473"
            ]
        ]
    },
    {
        "id": "d51a1863a0f12473",
        "type": "function",
        "z": "9bfd23d692c499bb",
        "name": "calc consumption of today",
        "func": "if (!flow.get(\"day_start_energy\"))\n    flow.set(\"day_start_energy\", flow.get(\"energy\"));\n    \nmsg.payload = flow.get(\"energy\") - flow.get(\"day_start_energy\");\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 500,
        "y": 380,
        "wires": [
            [
                "0c6d421057b64e20",
                "9de67151a098a892",
                "d207741a429718f0"
            ]
        ]
    },
    {
        "id": "0c6d421057b64e20",
        "type": "ui_text",
        "z": "9bfd23d692c499bb",
        "group": "d15c50bb.6b0f38",
        "order": 2,
        "width": 0,
        "height": 0,
        "name": "Consumo Giornaliero",
        "label": "Consumo Giornaliero",
        "format": "{{msg.payload | number:3}} w",
        "layout": "row-spread",
        "className": "",
        "x": 780,
        "y": 380,
        "wires": []
    },
    {
        "id": "9de67151a098a892",
        "type": "function",
        "z": "9bfd23d692c499bb",
        "name": "consumption to cost",
        "func": "let Z = 0.93; // Fattore di Conversione\nlet H = 11.3; //Valore kWh / Nm³\nlet c = 14.32; // Cent / kWh\nmsg.payload = msg.payload * Z * H * c / 100;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 480,
        "y": 460,
        "wires": [
            [
                "f2abe5e1e9897481"
            ]
        ]
    },
    {
        "id": "f2abe5e1e9897481",
        "type": "ui_text",
        "z": "9bfd23d692c499bb",
        "group": "d15c50bb.6b0f38",
        "order": 3,
        "width": 0,
        "height": 0,
        "name": "Costo Giornaliero",
        "label": "Costo Giornaliero",
        "format": "{{msg.payload | number:2}} €",
        "layout": "row-spread",
        "className": "",
        "x": 710,
        "y": 460,
        "wires": []
    },
    {
        "id": "152feed774c6238f",
        "type": "ui_text",
        "z": "9bfd23d692c499bb",
        "group": "d15c50bb.6b0f38",
        "order": 1,
        "width": 0,
        "height": 0,
        "name": "Meter",
        "label": "Consumo Totale",
        "format": "{{msg.payload | number:3}} w",
        "layout": "row-spread",
        "className": "",
        "x": 670,
        "y": 300,
        "wires": []
    },
    {
        "id": "0bfb070013fe1234",
        "type": "debug",
        "z": "9bfd23d692c499bb",
        "name": "debug 6",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 820,
        "y": 560,
        "wires": []
    },
    {
        "id": "e5bf56c4febdc08d",
        "type": "inject",
        "z": "9bfd23d692c499bb",
        "name": "newweek",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "00 00 * * 1",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 90,
        "y": 520,
        "wires": [
            [
                "7dedcff47a14e1f5"
            ]
        ]
    },
    {
        "id": "7dedcff47a14e1f5",
        "type": "change",
        "z": "9bfd23d692c499bb",
        "name": "set week start",
        "rules": [
            {
                "t": "set",
                "p": "week_start_energy",
                "pt": "flow",
                "to": "energy",
                "tot": "flow"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 270,
        "y": 520,
        "wires": [
            [
                "998a795ca7707563"
            ]
        ]
    },
    {
        "id": "998a795ca7707563",
        "type": "function",
        "z": "9bfd23d692c499bb",
        "name": "calc consumption weekly",
        "func": "if (!flow.get(\"week_start_energy\"))\n    flow.set(\"week_start_energy\", flow.get(\"energy\"));\n    \nmsg.payload = flow.get(\"energy\") - flow.get(\"week_start_energy\");\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 490,
        "y": 520,
        "wires": [
            [
                "a9106fef21778d3d",
                "b24596d515d33f35",
                "0bfb070013fe1234"
            ]
        ]
    },
    {
        "id": "a9106fef21778d3d",
        "type": "ui_text",
        "z": "9bfd23d692c499bb",
        "group": "d15c50bb.6b0f38",
        "order": 4,
        "width": 0,
        "height": 0,
        "name": "Consumo Settimanale",
        "label": "Consumo Settimanale",
        "format": "{{msg.payload | number:3}} w",
        "layout": "row-spread",
        "className": "",
        "x": 780,
        "y": 520,
        "wires": []
    },
    {
        "id": "b24596d515d33f35",
        "type": "function",
        "z": "9bfd23d692c499bb",
        "name": "consumption to cost",
        "func": "let Z = 0.93; // Fattore di Conversione\nlet H = 11.3; //Valore kWh / Nm³\nlet c = 14.32; // Cent / kWh\nmsg.payload = msg.payload * Z * H * c / 100;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 480,
        "y": 600,
        "wires": [
            [
                "d586ada138db2e61"
            ]
        ]
    },
    {
        "id": "d586ada138db2e61",
        "type": "ui_text",
        "z": "9bfd23d692c499bb",
        "group": "d15c50bb.6b0f38",
        "order": 5,
        "width": 0,
        "height": 0,
        "name": "Costo Settimanale",
        "label": "Costo Settimanale",
        "format": "{{msg.payload | number:2}} €",
        "layout": "row-spread",
        "className": "",
        "x": 710,
        "y": 600,
        "wires": []
    },
    {
        "id": "e8e7fd0.247eb",
        "type": "server",
        "name": "Home Assistant",
        "addon": true,
        "rejectUnauthorizedCerts": true,
        "ha_boolean": "",
        "connectionDelay": false,
        "cacheJson": false,
        "heartbeat": false,
        "heartbeatInterval": "",
        "statusSeparator": "",
        "enableGlobalContextStore": false
    },
    {
        "id": "d15c50bb.6b0f38",
        "type": "ui_group",
        "name": "Consumo Bocchio",
        "tab": "3c9b4e17.5d49ea",
        "order": 7,
        "disp": true,
        "width": "6",
        "collapse": false,
        "className": ""
    },
    {
        "id": "3c9b4e17.5d49ea",
        "type": "ui_tab",
        "name": "Consumi",
        "icon": "dashboard",
        "order": 1,
        "disabled": false,
        "hidden": false
    }
]

noone can help me?

You’ll want to save the context to the file system.

https://nodered.org/docs/user-guide/context#saving-context-data-to-the-file-system

@Kermit
I applied your suggestions and it wrote a file… on path… /config/node-red/context/9bfd23d692c499bb/flow.json

then I have a function based on previous flow infos… that is:

if (!flow.get("day_start_energy"))
    flow.set("day_start_energy", flow.get("energy"));
    
msg.payload = flow.get("energy") - flow.get("day_start_energy");
return msg;

how to change it to read this file? because it stops working…

https://nodered.org/docs/user-guide/writing-functions#multiple-context-stores

You can also use a HA numeric helper to read / store a value.
This persists reboots.