If - Else Templating help

Hi all and I hope someone may be able to help with this topic.

I have this template inside a Markup Card and it works really well but I would love to be able to extend it a little and I’m really not sure how to go about it.

Here is my template:

{% if is_state("sun.sun", "above_horizon") -%}
#### Generating Solar Power: {{ (states( 'sensor.total_solar_array_power' ))|float |round (0) }} Watts 
  
#### Feed In (-) / Purchased (+): {{ (states( 'sensor.grid_power_watts' ))|float |round (0) }} Watts 

#### Purchased Energy: {{ (states( 'sensor.gosungrow_virtual_1273924_p83102' )) }} kWh

#### Feed In Energy: {{ (states( 'sensor.gosungrow_virtual_1273924_p83072' ))|float |round (0) }} kWh

 #### Battery Discharging (-) / Charging: (+): {{ (states( 'sensor.battery_charge_and_discharge_watts' ))|float |round (0) }} Watts

#### Battery Storage: {{ (states( 'sensor.gosungrow_getpsdetail_1273924_storage_inverter_data_1273924_14_1_1_p13141' )|float |round (0)) }} %

#### Battery Energy Today: {{ (states( 'sensor.gosungrow_virtual_1273924_14_1_1_p13028' ))|float |round (0) }} kWh

#### Battery Current: {{ (states( 'sensor.gosungrow_virtual_1273924_43_2_1_p58602' )) }} Amps

{%- else -%}

#### Battery Discharging (-) / Charging: (+): {{ (states( 'sensor.battery_charge_and_discharge_watts' ))|float |round (0) }} Watts

#### Battery Current: {{ (states( 'sensor.gosungrow_virtual_1273924_43_2_1_p58602' )) }} Amps

### Battery Storage Level: {{ (states( 'sensor.gosungrow_getpsdetail_1273924_storage_inverter_data_1273924_14_1_1_p13141' )) }} % 

#### Battery Energy Today: {{ (states( 'sensor.gosungrow_virtual_1273924_14_1_1_p13028' ))|float |round (0) }} kWh


{%- endif %}

That displays this text in the day:

Energy Status:

Generating Solar Power: 19 Watts

Feed In (-) / Purchased (+): 0 Watts

Purchased Energy: 2 kWh

Feed In Energy: 19 kWh

Battery Discharging (-) / Charging: (+): -1273 Watts

Battery Storage: 93 %

Battery Energy Today: 14 kWh

Battery Current: 3.4 Amps

Once the sun goes down it of course displays the nightime information specified in the template.

What I am looking to extend is if below line the value of Solar Power sensor was zero, then I would like to display the text “Not Generating Solar Power” or the like.

Generating Solar Power: 22 Watts

I am sure this should be possible as I can display night and day using an if - else statement I’m just not sure how to impliment what I am looking to achieve here?

Thanks in advance for any tips!

{% if is_state("sun.sun", "above_horizon") -%}
{% set pwr = states('sensor.total_solar_array_power' ) | float(0) | round(0) -%}
#### Generating Solar Power: {{ pwr ~ ' Watts' if pwr > 0 else 'Not generating solar power' }}
  
1 Like

Thanks very much Taras!

I got caught up in doing what I wanted as a block
Didn’t think to do it in line by line

{% set gridpwr = states('sensor.grid_power_watts' ) | float(0) | round(0) -%}
#### Purchasing Grid Power: {{ gridpwr ~ ' Watts' if gridpwr > 0 else 'Not Feeding in solar power' }}

Regards Steve

1 Like