Creating a template line sensor with IF THEN ELSE

Could someone help me code this template sensor?

  - platform: template
    sensors:
solar_consumption:
if sensor.solaredge_current_power=0 then solar_consumption=0
else
if sensor.solaredge_current_power>sensor.energy_usage then
solar_consumption=sensor.energy_usage
else
solar_consumption=sensor.solaredge_current_power
end if
end if

I tried but nothing worked.
Thanks

Oh also how would I code this (with a subtraction):

if sensor.solaredge_current_power>sensor.energy_usage then
grid_feed_in=sensor.solaredge_current_power - sensor.energy_usage
- platform: template
    sensors:
      solar_consumption:
        value_template: >
          {% if states('sensor.solaredge_current_power') | float == 0 %}
            0
          {% elif states('sensor.solaredge_current_power') | float > states('sensor.energy_usage') | float %}
            {{ states('sensor.energy_usage') }}
          {% else %}
            {{ states('sensor.solaredge_current_power') }}
          {% endif %}
2 Likes

@finity You made me smile.
Thanks man.

“elif” is not a typo right? :wink:

:slightly_smiling_face:

nope.

that’s the way it’s written for another “if” in the chain.

it’s just a contraction of “else if”.

And one last question… how do you do the subtraction in the second part of my question? :grimacing:

oops, forgot about that part.

{% if states('sensor.solaredge_current_power') | float > states('sensor.energy_usage') | float %}
  {{ states('sensor.solaredge_current_power') |float - states('sensor.energy_usage') }}
{% endif %}
1 Like

You can use variables to streamline the template (and supply the float filter with a default value because it will become mandatory in December’s release):

        value_template: >
          {% set scp = states('sensor.solaredge_current_power') | float(0) %}
          {% set eu = states('sensor.energy_usage') | float(0) %}
          {{ 0 if scp == 0 else eu if scp > eu else scp }}
2 Likes

Thanks.

Do I need a float after - states(‘sensor.energy_usage’) | float }}

OOPS again.

yes

Thanks this does look neater in a strange way… so the last line starts with a 0 to say if scp=0 then value is 0 else move on… right?

Less need for multiple float filters if you use variables.

{% set scp = states('sensor.solaredge_current_power') | float(0) %}
{% set eu = states('sensor.energy_usage') | float(0) %}
{% if scp > eu %}
  {{ scp - eu }}
{% endif %}
1 Like

And so in this new more modern way, how do I do the subtraction part?

0 if scp==0 else scp-eu if…

It’s an inline if statement. If you don’t use variables, it can become a chore to read an inline-if because it becomes very long.

Yes, just use scp - eu if that’s what you need.

        value_template: >
          {% set scp = states('sensor.solaredge_current_power') | float(0) %}
          {% set eu = states('sensor.energy_usage') | float(0) %}
          {{ 0 if scp == 0 else scp - eu if scp > eu else scp }}

Whichever style you prefer to use, don’t overlook to supply the float filter with a default value (I used zero in the example).

2 Likes

So would this

    - platform: template
    sensors:
      grid_feed_in:
        friendly_name: Grid Feed
        unit_of_measurement: 'W'
        value_template: >
          {% if states('sensor.solaredge_current_power') | float == 0 %}
            0
          {% elif states('sensor.solaredge_current_power') | float > states('sensor.energy_usage') | float %}
            {{ states('sensor.solaredge_current_power') |float - states('sensor.energy_usage') | float}}
          {% else %}
            0
          {% endif %}

Be the same as this:

    - platform: template
    sensors:
      grid_feed_in:
        friendly_name: Grid Feed
        unit_of_measurement: 'W'
        value_template: >
          {% set scp = states('sensor.solaredge_current_power') | float(0) %}
          {% set eu = states('sensor.energy_usage') | float(0) %}
          {{ 0 if scp == 0 else scp-eu if scp > eu else 0 }}

K so the above is correct appart from the spaces scp - eu

I thought it would only be “mandatory” if there were errors generated by templates that couldn’t be rendered properly. Otherwise not.

??

The addition of spaces isn’t critical; the Jinja2 interpreter understands how to interpret it correctly with or without the spaces.

Depends on how you want to interpret ‘mandatory’. If you omit the default and the filter encounters a situation where it needed it, the entire template fails with an error.

Currently, it only gives you a warning and the filter supplies whatever default value it traditionally provided (which can mask some terrible nonsense and I say that based on having seen some terrible nonsense posted in this forum).

OK, that’s what I thought.

from your statement I thought I missed something else.

If you thought Check Configuration would flag missing default values, I didn’t get the impression that will happen (but we’ll know for sure in a month or so).