Only consider positive values

Hi all,

I want to create a sensor from another sensor, where I only consider positive values.
How can I go about doing it.
My current sensor is
{{states(‘sensor.byd_remaining_power’) | float -2210}} but for my new sensor I want to have only positive values since the negative mean that the battery is charging so there is no point participating in the new sensor.

I read about the If statement but not sure how to formulate it.

Thank you in advance

So what should it be if it’s negative then?

If negative then it should be 1,
What I want to do is divide the remaining power with the power consumption from the battery.
But if the battery consumption is less than 0 (which means it is charging) then I can divide with 1 which will give me the remaining capacity of the battery.

{{ [states(‘sensor.byd_remaining_power’) | float -2210, 1] | max }} 

Thank you, I will give it a try and 'll get back to you.

You’ll need to replace the “smart quotes” copied in from your forum-text-formatted original post. Corrected:

{{ max(states('sensor.byd_remaining_power')|float(0) - 2210, 1) }}

Although I have positive consumption from the battery, which means that there is energy flowing out, I only get an indication of ON when I use the script
image
I would expect to get the 2.8 value that I am getting with my previous sensor and also from the Fronius app.

Apologies by the way for the script formatting

This is the script I use to calculate the remaining time. I want to calculate only if the power flow indication on the battery is positive.

{{ ( states('sensor.remaining_battery_without_20_percent') | float / states('sensor.solarnet_power_load') | float ) *-1 | round(0)}}

I want to replace the sensor.solarnet_power_load with sensor.solarnet_power_battery but only if the indication is positive.

That’s because you’re trying to set up a template binary sensor. You made the wrong choice one step before.

I should have selected “Template a Sensor”?

Yes. Binary sensor are “on” or “off” only.

No it doesnt work
Now the battery shows a flow of 34.23 but the sensor shows 1.

I will give an example just to be sure I am conveying the requirement correctly:

There is a BYD battery sensor “sensor.solarnet_power_battery”
This sensor can have negative values e.g. -730w in case the battery is being charged or positive values e.g. 500w in case the battery is being discharged.

Currently irrespective of negative or positive value the newly created sensor that uses the “sensor.solarnet_power_battery” shows always 1 as value.
Any ideas as to why?

We were going off your first post. 34.23 - 2210 is very negative, so the template returns 1 as requested and will do so until sensor.byd_remaining_power exceeds 2211.

I can see you’ve tried to explain what you want a few times, but I’m still not clear on the logic.

Please give some specific examples of the inputs you’re getting from your (three?) sensors and what you want the output to be.

You’ve listed these across your posts so far:

  • sensor.byd_remaining_power
  • sensor.solarnet_power_load
  • sensor.solarnet_power_battery

I don’t understand what you mean by this, for example:

First off all apologies for any confusion. Indeed initially I might have given the wrong request.

To answer your question:

  • sensor.byd_remaining_power → This represents the available energy of the battery. The reason I deduct 2210 is because this refers to the 20% of the battery that is never discharged. I have gone with a steady number instead of a percentage because I got this reading from Fronius. The value I expect here is e.g. 8.849,0 kWh

  • sensor.solarnet_power_load → This represents the instant consumption of the house. For example if I use the oven the value will be 2000w however this is not representative because the energy might be coming directly from the PV and not the battery this is way I want to change to the next sensor

  • sensor.solarnet_power_battery → this represents the instant either charge or discharge of the battery. The values can be e.g. -2500w if the battery is being charged or e.g. 3000w if the battery is being discharged.

What I want to do is calculate the remaining battery time by dividing sensor.byd_remaining_power / sensor.solarnet_power_battery
but only if the sensor.solarnet_power_battery has a positive value which means it is being discharged.

In general I want to understand how to syntax the sensor.solarnet_power_battery to give values only if positive and if negative then it should be equal to 1 which means that I would have the full power of the battery.
Hope this is more clear now.

Right, I think so. This should represent the wording you’ve just posted. Paste into the state template:

{% if states('sensor.solarnet_power_battery')|float(0) > 0 %}
  {{ states('sensor.byd_remaining_power')|float(0) /
     states('sensor.solarnet_power_battery')|float }}
{% else %}
  1
{% endif %}

That doesn’t involve the load sensor at all: is that what you’d expect? It should give you an idea of syntax anyway, even if you’ve mis-described or I’ve mis-interpreted.

Note that it’s good practice to provide a default value to filters like float, which is what the (0) is. If the sensor is unavailable or unknown, it’ll use 0 instead, which will cause the whole template to output 1.

I haven’t included a default for the battery sensor the second time, as that code should never be reached if the sensor is non-numeric; and a default of 0 would cause a divide-by-zero error there anyway.

This is great and covers what I want.
One last question, if instead of 1 I wanted to have the value N/A which of course is text. How would the syntax change ?

Just change the 1 into N/A. Note that this will generate an error if you have set a device class or unit of measurement on your sensor.

Better to set an availability template, but you must use YAML to do that:

template:
  - sensor:
      - name: "Only positive battery readings"
        state: >
          {{ states('sensor.byd_remaining_power')|float(0) /
             states('sensor.solarnet_power_battery')|float }}
        availability: "{{ states('sensor.solarnet_power_battery')|float(0) > 0 }}"

You are great.
Really appreciate the support.
I will try it
Thanks again!!!