Sensor: don't want a "-" but a 0 as minumum

Hi,

I have a sensor calculating my solar energy consumption. It works very well.

BUT, it goes below zero if I deliver back too much, but I want that to be minimum 0.

Is that possible?


	  power_consumption_solar:
        value_template: "{{ (states('sensor.growatt_total_output_power')|float - (states('sensor.power_production')|float *1000)|round(2) }}"
        unit_of_measurement: 'W'
        friendly_name: Home usage Solar Energy

example of what I do not want too see:
image
this should be minimum 0.

Yep, use the max function.

value_template: "{{ [ (states('sensor.growatt_total_output_power')|float - (states('sensor.power_production')|float *1000)|round(2), 0 ]|max }}"

This selects the maximum from a list containing your sensor calculation and zero. So you will only get 0 or positive values (0 is bigger than all negative numbers).

Configuration invalid

Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: unexpected ‘]’, expected ‘)’) for dictionary value @ data[‘sensors’][‘power_consumption_solar’][‘value_template’]. Got “{{ [ (states(‘sensor.growatt_total_output_power’)|float - (states(‘sensor.power_production’)|float *1000)|round(2), 0 ]|max }}”. (See ?, line ?).

You’re missing a ) somewhere. Not sure how that happened as I just copied your template and put it in a list. Try this:

value_template: "{{ [ ( states('sensor.growatt_total_output_power')|float - ( states('sensor.power_production')|float *1000 ) )|round(2), 0 ]|max }}"

still

Configuration invalid

                      Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: unexpected char '‘' at 15) for dictionary value @ data['sensors']['power_consumption_solar']['value_template']. Got '“{{ [ ( states(‘sensor.growatt_total_output_power’)|float - ( states(‘sensor.power_production’)|float *1000 ) )|round(2), 0 ]|max }}”'. (See ?, line ?).

Maybe tell me “how” to put that “0 ]|max” where is it described?

It looks like when you copied it over, the quotations got messed up. I just did a straight copy/paste from the code above and it worked. Try coping the above code again or replace ‘ with ’ in what you currently have

You have silly smart quotes (66 / 99) within your statement. Are you transferring via MS Word?

I dont use word… Use direct browser-browser. now I think it took it:

I cant do much more than showing you the exact template above, but it works like this:

value_template: "{{ [ your_template, 0 ]|max }}"

separate your lines so it’s easy to read.

value_template: >
  {% set total = states('sensor.growatt_total_output_power') | float %}
  {% set production = states('sensor.power_production') | float * 1000 %}
  {{ [ total - production, 0 ] | max | round(2) }}