Hi all,
i’m trying to create two template sensor for imported and exported from grid.
I can’t use a shelly em or similar on the cables so i have to calculate this two values
I have total consumption and panel production values, now im’ using this templates
The problem is, i don’t want negative values, i need negatives becomes 0.
If i try the code on “models” in “developer tools” seems all ok, if i have a negative number i get back 0, but if i use the “prelievo” sensor on energy dashboard it ignores it and save the negative value.
It looks like there is a misconception going on here about how to calculate the imported and exported amount of energy.
You cannot just subtract the total_increasing values of the solar produced energy and total consumed energy to calculate these.
To calculate the respectively imported and exported energy in kWh you have to subtract the instantaneous power values in W or kW, and integrate the results to get the energy in kWh.
The integration can for instance be done with the Integration integration.
The resultant imported and exported energy values in kWh can then be used to feed the energy dashboard.
Do you also have sensors that give the instantaneous power consumption and produced solar power in W or kW?
Start with exchanging these power senors with the current energy sensors in your templates, and see what the result is.
You should get two values for both the imported and exported instantanious power.
I just have a sensor template that calculate inport/export power, but is for “power” not “energy” so i can’t use it on Energy Dashboard because is instantaneous value.
When you have these two total_increasing energy sensors in kWh, one for imported and one for exported energy, you can directly feed those two in the energy dashboard.
To summarize:
You have the instantaneous solar panel production power S in [W] or [kW].
You have the instantaneous consumed power C in [W or [kW].
You can subtract these two values to get the either imported or exported instantaneous power G in [W] or [kW].
So G = C – S in [W] or [kW].
If G is positive it is instantaneous imported power, so if you Integrate this value you get the total_increasing amount of imported energy in [Wh] or [kWh].
And if G is negative it is instantaneous exported power, so the absolute value can be used with the Integration to get the total_increasing amount of exported energy in [Wh] or [kWh].
It will take some time for the Integration sensors to be populated because it is an integration over time.
If you show your yaml code for the sensors and the resultant graph we can have a look at it.
I understand that, but for Energy Dashboard i need two different entities (one for inported and one for exported) and it accept only positive values and not negative.
Doing this method i have only one entities with positive and negative and can’t use it on Energy Dashboard
Sorry, i’m a little bit stupid but i want to understand
You have to “split” the “G” Grid power sensor from my example, using template sensors similar to what you did with your prelievo and immissione sensor code in you first post.
Only in this case these are not energy but power sensors, but that does not make a difference for the method.
When the value is negative you can take the absolute value to convert it to a positive value.
So the result is two instantaneous power sensors with positive values (one import and one export) that can then be integrated to get the energy sensors with total increasing positive values.
Good for you: I agree that it is always more satisfying when you really understand something instead of just coping code from someone else.
That’s the first trouble, i’ve not found a working code to set to zero the negative values.
I always receive positive and negative values.
The actual codes are this: