# Home usage, solarproduction, math and with if else then..? need some help

**URL:** <https://discourse.nodered.org/t/home-usage-solarproduction-math-and-with-if-else-then-need-some-help/58254>\
**Category:** General\
**Created:** [13 February 2022 10:43 UTC](https://discourse.nodered.org/t/home-usage-solarproduction-math-and-with-if-else-then-need-some-help/58254 "2022-02-13T10:43:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![syma33](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/syma33/32/30665_2.png) [@syma33](https://discourse.nodered.org/u/syma33)\
**Post date:** [13 February 2022 10:43 UTC](https://discourse.nodered.org/t/home-usage-solarproduction-math-and-with-if-else-then-need-some-help/58254/1 "2022-02-13T10:43:45Z")

</div>

I suceeded to create a visualisation of power usage at home, it looks like this....  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/4/0426f2240bfad52385867fcfeaf8bb278ad2fb8f.png)  
sql table tags includes 1 or 0, picked by the gui, where 1 enable color-arrows and 0 to hide them, the tags in table looks like this..  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/f/6faedee2f16eb74a438c3e18fbb517d14f29ff64.png)

Now.. i need node-red to calculate kw and send tags of 1 or 0 to sql-db but.. i feel my approach wont work as i think... any suggestion where to start to calculate? my brain in stuck in a loop..

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/5/c55b015e9701ca50ceb5e37ee606460af422c097.jpeg)

---

<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:** [13 February 2022 11:17 UTC](https://discourse.nodered.org/t/home-usage-solarproduction-math-and-with-if-else-then-need-some-help/58254/2 "2022-02-13T11:17:39Z")

</div>

I think I understand your diagram - you want the arrows visible or invisible depending on which way the power is flowing now?

But what does it have to do with the database?

Can you show us the db structure and at least a screenshot of your flow?

---

<div class="post-metadata">

**Author:** ![syma33](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/syma33/32/30665_2.png) [@syma33](https://discourse.nodered.org/u/syma33)\
**Post date:** [13 February 2022 14:38 UTC](https://discourse.nodered.org/t/home-usage-solarproduction-math-and-with-if-else-then-need-some-help/58254/3 "2022-02-13T14:38:18Z")

</div>

> you want the arrows visible or invisible depending on which way the power is flowing now?

yes

> But what does it have to do with the database?

its just temporary storage, the db structure is not finished, cause i cannot get the code correct.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/5/45f00de804719fbc7fad926e007e1a2bf16552d9.png)

```auto
[{"id":"54a9a5742e6589df","type":"inject","z":"0f9de81aaa0f53ef","name":"grid 10kw","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"grid","payload":"10","payloadType":"num","x":1060,"y":280,"wires":[["b0f91c6b7a6a9ebe"]]},{"id":"98de6e3932b870cb","type":"inject","z":"0f9de81aaa0f53ef","name":"grid 5kw","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"grid","payload":"5","payloadType":"num","x":1070,"y":320,"wires":[["b0f91c6b7a6a9ebe"]]},{"id":"d64e5e3cef802ac1","type":"inject","z":"0f9de81aaa0f53ef","name":"grid 0kw","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"grid","payload":"0","payloadType":"num","x":1070,"y":360,"wires":[["b0f91c6b7a6a9ebe"]]},{"id":"20ecf3913e027a6d","type":"inject","z":"0f9de81aaa0f53ef","name":"pv 10kw","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"pv","payload":"10","payloadType":"num","x":1180,"y":160,"wires":[["b0f91c6b7a6a9ebe"]]},{"id":"2eba78cae8d3539d","type":"inject","z":"0f9de81aaa0f53ef","name":"pv 5kw","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"pv","payload":"5","payloadType":"num","x":1180,"y":200,"wires":[["b0f91c6b7a6a9ebe"]]},{"id":"617c9b100fa7de22","type":"inject","z":"0f9de81aaa0f53ef","name":"pv 0kw","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"pv","payload":"0","payloadType":"num","x":1180,"y":240,"wires":[["b0f91c6b7a6a9ebe"]]},{"id":"568404e6d863285c","type":"inject","z":"0f9de81aaa0f53ef","name":"home 10kw","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"home","payload":"10","payloadType":"num","x":1310,"y":280,"wires":[["b0f91c6b7a6a9ebe"]]},{"id":"97da716273846630","type":"inject","z":"0f9de81aaa0f53ef","name":"home 5kw","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"home","payload":"5","payloadType":"num","x":1310,"y":320,"wires":[["b0f91c6b7a6a9ebe"]]},{"id":"c735f94eec9b7789","type":"inject","z":"0f9de81aaa0f53ef","name":"home 0kw","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"home","payload":"0","payloadType":"num","x":1310,"y":360,"wires":[["b0f91c6b7a6a9ebe"]]},{"id":"ed5e86035a25a4ad","type":"inject","z":"0f9de81aaa0f53ef","name":"bp 10kw","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"bp","payload":"10","payloadType":"num","x":1180,"y":400,"wires":[["b0f91c6b7a6a9ebe"]]},{"id":"84fb9bb7243ccdc3","type":"inject","z":"0f9de81aaa0f53ef","name":"bp 5kw","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"bp","payload":"5","payloadType":"num","x":1180,"y":440,"wires":[["b0f91c6b7a6a9ebe"]]},{"id":"f18a622f04ac9109","type":"inject","z":"0f9de81aaa0f53ef","name":"bp 0kw","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"bp","payload":"0","payloadType":"num","x":1180,"y":480,"wires":[["b0f91c6b7a6a9ebe"]]},{"id":"b0f91c6b7a6a9ebe","type":"join","z":"0f9de81aaa0f53ef","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"3","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":1490,"y":300,"wires":[["0f0a0d4929135d48","ee178b050bd410cc"]]},{"id":"0f0a0d4929135d48","type":"debug","z":"0f9de81aaa0f53ef","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1530,"y":220,"wires":[]},{"id":"ee178b050bd410cc","type":"function","z":"0f9de81aaa0f53ef","name":"pv,home,bp","func":"//var pv = payload.pv\n//var home = payload.home\n//var bp = payload.bp\n//var grid = \"0\"\n\n//math... \nvar plusorminus = (msg.payload.home - msg.payload.pv)\nmsg.payload = plusorminus;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1670,"y":300,"wires":[["62b7b58.d85804c"]]},{"id":"62b7b58.d85804c","type":"switch","z":"0f9de81aaa0f53ef","name":"","property":"$number(payload)","propertyType":"jsonata","rules":[{"t":"lt","v":"0","vt":"num"},{"t":"gt","v":"0","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":3,"x":1510,"y":440,"wires":[["b2f576f7.07073"],["8e0f836.ea41a8"],["4855cece.93e67"]]},{"id":"4855cece.93e67","type":"debug","z":"0f9de81aaa0f53ef","name":"0","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1690,"y":480,"wires":[]},{"id":"b2f576f7.07073","type":"debug","z":"0f9de81aaa0f53ef","name":"-1 (overproduction","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1730,"y":400,"wires":[]},{"id":"8e0f836.ea41a8","type":"debug","z":"0f9de81aaa0f53ef","name":"1 (from power source)","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1740,"y":440,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [13 February 2022 14:44 UTC](https://discourse.nodered.org/t/home-usage-solarproduction-math-and-with-if-else-then-need-some-help/58254/4 "2022-02-13T14:44:11Z")

</div>

When you are editing a thread these options should appear.  
 ![Screen Shot 02-13-22 at 02.41 PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/6/46e801dbb6f1987a48928669a93e570e8c08e352.png)

Paste you code between the back ticks.  
 ![Screen Shot 02-13-22 at 02.42 PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/2/423ac9dbbf848673249143646fc10d60cbbedb73.png)

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [13 February 2022 14:49 UTC](https://discourse.nodered.org/t/home-usage-solarproduction-math-and-with-if-else-then-need-some-help/58254/5 "2022-02-13T14:49:58Z")

</div>

Your code still doesn't look quite right (although it will probably import OK when someone clicks it).  
Did you click the 'compact' option when you copied it to the clipboard?  
 ![Screen Shot 02-13-22 at 02.48 PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/2/228c3062d7b8c96564d4ffb70d255657804ac99c.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:** [13 February 2022 16:31 UTC](https://discourse.nodered.org/t/home-usage-solarproduction-math-and-with-if-else-then-need-some-help/58254/6 "2022-02-13T16:31:53Z")

</div>

First off, none of the joins groups (pv, grid, home or pv) will work when you press the three injects because they are all (in each group) sending the same topic and the join is waiting for three different topics.

If you click on three injects (one from home, pv and bp) then you will see a result.  
If you click on three injects (one from home, pv and grid) then you will see a result.  
BUT if you don't send a msg from the 'home' and 'pv' groups, the `NAN` appears because the function you have:  
`var plusorminus = (msg.payload.home - msg.payload.pv)`  
and if msg.payload.home or msg.payload.pv is missing you get a `NAN`

---

<div class="post-metadata">

**Author:** ![syma33](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/syma33/32/30665_2.png) [@syma33](https://discourse.nodered.org/u/syma33)\
**Post date:** [14 February 2022 16:44 UTC](https://discourse.nodered.org/t/home-usage-solarproduction-math-and-with-if-else-then-need-some-help/58254/7 "2022-02-14T16:44:30Z")

</div>

i have no access to node-red for moment, this is what i wrote at the train without any error check with notepad... i think this is a good start..

i use grid meter to first check if theres a export or not, this is only the export-senario.

```auto
//debug option... remove when go production.

//grid meter
var gridA = 16,1 //debug power from grid
var gridB = 20,4 //debug power to grid
//home meter
var homeA = 27,8 //debug power usage
var pvA = 32,1 //debug power production
//battery pack
var bpA = 32,1 //debug power production
var bpB = 0 //debug power usage

var gridDIFF = gridA-gridB //we are exporting, but to what? result is 4,3
if(gridDIFF = positive)}
//export function, this is not finished, this need to be put into a own function block...
pvA-homeA = pvMINUShome // result is 4,3
if(pvMINUShome==gridDIFF){
	set pv2grid = 1
	set pv2home = 1
};

if(bpA>bpB){ //is production higher than usage?
								//it was higher (but what usage?)
	var compare = (pbA+bpA)-homeA /
	if(compare==gridDIFF){
		set bp2home = 1
		set bp2grid = 1
	}
}elseif{ //it was lower,batteries are charing, but from where?
	var compare = pvA-home
	var compare = compare - gridDIFF
	if(compare == gridDIFF){
		set pv2bp = 1
		set pv2grid = 1
		set pv2home = 1
	}
}
}
//export function ends here...
//import function starts here
elseif(gridDIFF = negative){
	
}

```

---

<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:** [15 April 2022 16:44 UTC](https://discourse.nodered.org/t/home-usage-solarproduction-math-and-with-if-else-then-need-some-help/58254/8 "2022-04-15T16:44:58Z")

</div>

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