Trying to work with a power figure (kW) that can be negative or positive because of solar export

Hi. I’m new here although I’ve been working with HA for a while now. I recently started to get data from my smart meter via MQTT and the Glow Hildebrand integration. I’m displaying it using the energy panel.

So, my question is about using the instantaneous power reading for electricity. I’ve implemented the Riemann averaging, and get data, but I’d rather do something else as well. The power reading I receive can be either positive or negative, depending on whether I’m importing or exporting. So from the one data source I want to generate two template sensors, import and export. My thought is to do a calculation by multiplying the kW data by -1 so that I have a version of the kW that is positive for export. I can see how to do this.

BUT I also want for each of the available numbers only to be zero or above. That is when importing, the export number is zero, when exporting import is zero. I can see in the numbers template that I can set max and min, but doesn’t seem I can use this with a sensor template. So do I need to use a numbers template to do the calculation and limiting, and then feed that to a sensor template?

Sorry if it’s a dumb question but I’m quite new to HA in this sense.

Have you looked at utility meter?

It can handle net metering, hourly, daily, weekly, monthly and yearly metering. It can handle peak and off peak metering. It can be calibrated. It resets at the end of each period.

Thanks, no but I will do. At a glance it’s not going to solve the problem directly but I’ll have a read. At present I’m using the built in HA energy monitoring,

However, you have given me another idea. I think the HA energy panel has solar export, so maybe if I take my kW figure, multiply it by -1 and feed it as solar it will ignore the negative inputs when I’m importing, much as the consumption metering appears to ignore the export from my kW figures.