Help request: Adding Home Assistant Values

Hi everyone,

I want to use a Shelly 3EM as an electricity meter. Since we have a photovoltaik plant and the shelly measures all three phases separatly (offtake and injection, so six values), I need to calculate the sum of those six values and then add this sum to the Total_Offtake_Value, if it's positive, or to the Total_Injection_Value, if it is negative.

Does someone has an idea, how to do it?

I tried today for many hours but I'm really stuck. Here is what I got so far:

[{"id":"97f3fba943e984d6","type":"tab","label":"Shelly3EM-Saldierend","disabled":false,"info":"","env":[]},{"id":"51bea5337f7ade47","type":"poll-state","z":"97f3fba943e984d6","name":"Energy L1","server":"6f93cefd.a282a","version":2,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"updateinterval":"60","updateIntervalType":"num","updateIntervalUnits":"seconds","outputinitially":false,"outputonchanged":false,"entity_id":"sensor.netzanschluss_energy_wh_l1","state_type":"num","halt_if":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"x":140,"y":60,"wires":[["55a7ba6fef5947c8"]]},{"id":"0d7bb5def0c551c6","type":"poll-state","z":"97f3fba943e984d6","name":"Energy L2","server":"6f93cefd.a282a","version":2,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"updateinterval":"60","updateIntervalType":"num","updateIntervalUnits":"seconds","outputinitially":false,"outputonchanged":false,"entity_id":"sensor.netzanschluss_energy_wh_l2","state_type":"num","halt_if":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"x":140,"y":120,"wires":[["85b50c657fd727aa"]]},{"id":"25add2b98f92aadd","type":"poll-state","z":"97f3fba943e984d6","name":"Energy L3","server":"6f93cefd.a282a","version":2,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"updateinterval":"60","updateIntervalType":"num","updateIntervalUnits":"seconds","outputinitially":false,"outputonchanged":false,"entity_id":"sensor.netzanschluss_energy_wh_l3","state_type":"num","halt_if":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"x":140,"y":180,"wires":[["6c7deb5161590270"]]},{"id":"55a7ba6fef5947c8","type":"change","z":"97f3fba943e984d6","name":"","rules":[{"t":"set","p":"val1","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":60,"wires":[["79ab0d9e3e0694ad"]]},{"id":"85b50c657fd727aa","type":"change","z":"97f3fba943e984d6","name":"","rules":[{"t":"set","p":"val2","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":120,"wires":[["79ab0d9e3e0694ad"]]},{"id":"6c7deb5161590270","type":"change","z":"97f3fba943e984d6","name":"","rules":[{"t":"set","p":"val3","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":180,"wires":[["79ab0d9e3e0694ad"]]},{"id":"79ab0d9e3e0694ad","type":"function","z":"97f3fba943e984d6","name":"Add","func":"var val1 = parseFloat(flow.get(\"val1\") || \"0\");\nvar val2 = parseFloat(flow.get(\"val2\") || \"0\");\nvar val3 = parseFloat(flow.get(\"val3\") || \"0\");\nvar val4 = parseFloat(flow.get(\"val4\") || \"0\");\nvar val5 = parseFloat(flow.get(\"val5\") || \"0\");\nvar val6 = parseFloat(flow.get(\"val6\") || \"0\");\nmsg.payload = val1 + val2 + val3 + val4 +  val5 + val6;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":630,"y":200,"wires":[["a6246f4fdf540a36"]]},{"id":"f039ce25dfaa94a9","type":"poll-state","z":"97f3fba943e984d6","name":"Energy Return L1","server":"6f93cefd.a282a","version":2,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"updateinterval":"60","updateIntervalType":"num","updateIntervalUnits":"seconds","outputinitially":false,"outputonchanged":false,"entity_id":"sensor.netzanschluss_energy_returned_wh_l1","state_type":"num","halt_if":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"x":160,"y":240,"wires":[["dc01b377799e8bd5"]]},{"id":"f930237ea5b7c5bb","type":"poll-state","z":"97f3fba943e984d6","name":"Energy Return L2","server":"6f93cefd.a282a","version":2,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"updateinterval":"60","updateIntervalType":"num","updateIntervalUnits":"seconds","outputinitially":false,"outputonchanged":false,"entity_id":"sensor.netzanschluss_energy_returned_wh_l2","state_type":"num","halt_if":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"x":160,"y":300,"wires":[["2ded69d63dbf27f0"]]},{"id":"6e5eb74e4bf8e2af","type":"poll-state","z":"97f3fba943e984d6","name":"Energy Return L3","server":"6f93cefd.a282a","version":2,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"updateinterval":"60","updateIntervalType":"num","updateIntervalUnits":"seconds","outputinitially":false,"outputonchanged":false,"entity_id":"sensor.netzanschluss_energy_returned_wh_l3","state_type":"num","halt_if":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"x":160,"y":360,"wires":[["0728d418a4a07c89"]]},{"id":"dc01b377799e8bd5","type":"change","z":"97f3fba943e984d6","name":"","rules":[{"t":"set","p":"val4","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":240,"wires":[["79ab0d9e3e0694ad"]]},{"id":"2ded69d63dbf27f0","type":"change","z":"97f3fba943e984d6","name":"","rules":[{"t":"set","p":"val5","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":300,"wires":[["79ab0d9e3e0694ad"]]},{"id":"0728d418a4a07c89","type":"change","z":"97f3fba943e984d6","name":"","rules":[{"t":"set","p":"val6","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":360,"wires":[["79ab0d9e3e0694ad"]]},{"id":"a6246f4fdf540a36","type":"switch","z":"97f3fba943e984d6","name":"","property":"payload","propertyType":"msg","rules":[{"t":"gt","v":"0","vt":"num"},{"t":"lt","v":"0","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":810,"y":200,"wires":[["17d8b9d0d427e3f8"],["845ff3a026bb6513"]]},{"id":"cde7f47a96ac6a7f","type":"poll-state","z":"97f3fba943e984d6","name":"Offtake","server":"6f93cefd.a282a","version":2,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"updateinterval":"60","updateIntervalType":"num","updateIntervalUnits":"seconds","outputinitially":false,"outputonchanged":false,"entity_id":"sensor.netzanschluss_netzbezug","state_type":"str","halt_if":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"x":110,"y":500,"wires":[["f1dc7bef7ec597a5"]]},{"id":"2c4f82f82ffe30f9","type":"poll-state","z":"97f3fba943e984d6","name":"Injection","server":"6f93cefd.a282a","version":2,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"updateinterval":"60","updateIntervalType":"num","updateIntervalUnits":"seconds","outputinitially":false,"outputonchanged":false,"entity_id":"sensor.netzanschluss_einspeisung","state_type":"str","halt_if":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"x":120,"y":620,"wires":[["d757b26d77b1c6d8"]]},{"id":"f1dc7bef7ec597a5","type":"change","z":"97f3fba943e984d6","name":"","rules":[{"t":"set","p":"val2","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":500,"wires":[["625e598d3fe4b6a1"]]},{"id":"625e598d3fe4b6a1","type":"function","z":"97f3fba943e984d6","name":"Add","func":"var val1 = parseFloat(flow.get(\"val1\") || \"0\");\nvar val2 = parseFloat(flow.get(\"val2\") || \"0\");\nmsg.payload = val1 + val2;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":630,"y":460,"wires":[[]]},{"id":"17d8b9d0d427e3f8","type":"change","z":"97f3fba943e984d6","name":"","rules":[{"t":"set","p":"val1","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":460,"wires":[["625e598d3fe4b6a1"]]},{"id":"845ff3a026bb6513","type":"change","z":"97f3fba943e984d6","name":"","rules":[{"t":"set","p":"val1","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":580,"wires":[["ebafce8f102aeb61"]]},{"id":"d757b26d77b1c6d8","type":"change","z":"97f3fba943e984d6","name":"","rules":[{"t":"set","p":"val2","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":620,"wires":[["ebafce8f102aeb61"]]},{"id":"ebafce8f102aeb61","type":"function","z":"97f3fba943e984d6","name":"Add","func":"var val1 = parseFloat(flow.get(\"val1\") || \"0\");\nvar val2 = parseFloat(flow.get(\"val2\") || \"0\");\nmsg.payload = val1 + val2;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":630,"y":580,"wires":[[]]},{"id":"6f93cefd.a282a","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30}]```

I have made the assumption that your incoming values should only be summed only when you get 6 new values.
This example may help you

[{"id":"faa37195.07b9f","type":"inject","z":"97f3fba943e984d6","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"$ceil($random()*5)-3","payloadType":"jsonata","x":110,"y":80,"wires":[["466b8197.48a738","f59dc5b2.bb1e4"]]},{"id":"466b8197.48a738","type":"change","z":"97f3fba943e984d6","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"val1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":270,"y":80,"wires":[["92bea493.7c7d8"]]},{"id":"f59dc5b2.bb1e4","type":"debug","z":"97f3fba943e984d6","name":"","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":580,"y":80,"wires":[]},{"id":"92bea493.7c7d8","type":"join","z":"97f3fba943e984d6","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"6","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":490,"y":200,"wires":[["79ab0d9e3e0694ad"]]},{"id":"22b5a7fe.215af8","type":"change","z":"97f3fba943e984d6","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"val2","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":270,"y":120,"wires":[["92bea493.7c7d8","f59dc5b2.bb1e4"]]},{"id":"24e088c.9468078","type":"inject","z":"97f3fba943e984d6","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"$ceil($random()*5)-3","payloadType":"jsonata","x":110,"y":180,"wires":[["eae63369.4b6c7","f59dc5b2.bb1e4"]]},{"id":"cedc72db.0ae72","type":"inject","z":"97f3fba943e984d6","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"$ceil($random()*5)-3","payloadType":"jsonata","x":110,"y":240,"wires":[["c6751f51.abf348","f59dc5b2.bb1e4"]]},{"id":"4dd8b8b.a8f60c8","type":"inject","z":"97f3fba943e984d6","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"$ceil($random()*5)-3","payloadType":"jsonata","x":110,"y":300,"wires":[["6753ebc.376d394","f59dc5b2.bb1e4"]]},{"id":"bb24f6b1.b8cea","type":"inject","z":"97f3fba943e984d6","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"$ceil($random()*5)-3","payloadType":"jsonata","x":110,"y":360,"wires":[["e69d46a7.4bd2b","f59dc5b2.bb1e4"]]},{"id":"79ab0d9e3e0694ad","type":"function","z":"97f3fba943e984d6","name":"Add","func":"msg.payload = Object.values(msg.payload).reduce((a,b) => a + parseFloat(b),0)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":630,"y":200,"wires":[["a6246f4fdf540a36","77ee47eb.23eed"]]},{"id":"eae63369.4b6c7","type":"change","z":"97f3fba943e984d6","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"val3","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":270,"y":180,"wires":[["92bea493.7c7d8"]]},{"id":"c6751f51.abf348","type":"change","z":"97f3fba943e984d6","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"val4","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":270,"y":240,"wires":[["92bea493.7c7d8"]]},{"id":"6753ebc.376d394","type":"change","z":"97f3fba943e984d6","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"val5","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":270,"y":300,"wires":[["92bea493.7c7d8"]]},{"id":"e69d46a7.4bd2b","type":"change","z":"97f3fba943e984d6","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"val6","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":270,"y":360,"wires":[["92bea493.7c7d8"]]},{"id":"ed2fecf5.661a2","type":"inject","z":"97f3fba943e984d6","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"$ceil($random()*5)-3","payloadType":"jsonata","x":110,"y":120,"wires":[["22b5a7fe.215af8"]]},{"id":"a6246f4fdf540a36","type":"switch","z":"97f3fba943e984d6","name":"","property":"payload","propertyType":"msg","rules":[{"t":"gt","v":"0","vt":"num"},{"t":"lt","v":"0","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":810,"y":200,"wires":[["17d8b9d0d427e3f8"],["57556454.8564fc"]]},{"id":"77ee47eb.23eed","type":"debug","z":"97f3fba943e984d6","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":790,"y":380,"wires":[]},{"id":"17d8b9d0d427e3f8","type":"change","z":"97f3fba943e984d6","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"val1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":460,"wires":[["80fc5465.20232"]]},{"id":"57556454.8564fc","type":"change","z":"97f3fba943e984d6","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"val1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":330,"y":580,"wires":[["f66eda95.ed315"]]},{"id":"4b14132c.831fa4","type":"change","z":"97f3fba943e984d6","name":"","rules":[{"t":"set","p":"positive","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":800,"y":460,"wires":[["77ee47eb.23eed"]]},{"id":"b55f92f1.0d3b9","type":"change","z":"97f3fba943e984d6","name":"","rules":[{"t":"set","p":"negative","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":800,"y":580,"wires":[["77ee47eb.23eed"]]},{"id":"80fc5465.20232","type":"join","z":"97f3fba943e984d6","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":510,"y":460,"wires":[["625e598d3fe4b6a1"]]},{"id":"f66eda95.ed315","type":"join","z":"97f3fba943e984d6","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":490,"y":580,"wires":[["ebafce8f102aeb61"]]},{"id":"625e598d3fe4b6a1","type":"function","z":"97f3fba943e984d6","name":"Add","func":"msg.payload =  Object.values(msg.payload).reduce((a,b) => a + (parseFloat(b)  || 0),0)\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":630,"y":460,"wires":[["4b14132c.831fa4"]]},{"id":"ebafce8f102aeb61","type":"function","z":"97f3fba943e984d6","name":"Add","func":"msg.payload = Object.values(msg.payload).reduce((a,b) => a + (parseFloat(b) || 0),0)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":630,"y":580,"wires":[["b55f92f1.0d3b9"]]},{"id":"f1dc7bef7ec597a5","type":"change","z":"97f3fba943e984d6","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"val2","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":500,"wires":[["80fc5465.20232"]]},{"id":"67eeb50.cd8934c","type":"change","z":"97f3fba943e984d6","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"val2","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":330,"y":620,"wires":[["f66eda95.ed315"]]},{"id":"51ec18fa.6fc71","type":"inject","z":"97f3fba943e984d6","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"positive","payloadType":"flow","x":120,"y":500,"wires":[["f1dc7bef7ec597a5"]]},{"id":"177b7044.48f1c8","type":"inject","z":"97f3fba943e984d6","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"negative","payloadType":"flow","x":140,"y":620,"wires":[["67eeb50.cd8934c"]]}]

I have used inject nodes as i do not have HA

Thanks for the quick help!!

I just integrated your input and the first part of the calculation is working now like a charm! :slight_smile:

But the calculation after the switch-node doesn't seem to work.

How can I add the calculated sum of the six values to the overall energy consumption (power offtake, injection)? Do I need a separate entity/variable for that?

[{"id":"affc05c7ebf69beb","type":"debug","z":"7f6ef726678ad590","name":"Add","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":990,"y":880,"wires":[]}]```

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