How to return last result for custom template sensor

Here is my issue: Sensor returns correct value while shelly is working and available (Custom sensor does some Maths on Power Consumption using shelly sensor). However, once shelly goes offline this custom sensor also goes unavailble. I would like it instead of going unavailable to just display its last known value. Any help on this would be greatly appreciated, here is the custom sensor template:

power_consumption:
      friendly_name: House Power Consumption
      value_template: >-
        {{ ((states('sensor.solar_channel_2_power') | float + states('sensor.solar_channel_1_power') | float) | round) }}
      unit_of_measurement: "Watt"
power_consumption:
  friendly_name: House Power Consumption
  value_template: >
    {% if states('sensor.solar_channel_2_power')|is_number and states('sensor.solar_channel_1_power')|is_number %}
      {{ ( states('sensor.solar_channel_2_power')|float + states('sensor.solar_channel_1_power')|float )|round }}
    {% else %}
      {{ this.state }}
    {% endif %}
  unit_of_measurement: "W"
  device_class: power
1 Like

thank you very much! just heads up for anyone else space between end if in {%end if%} should be removed for this to work. Thank again!

1 Like

Thanks. I fixed it.

1 Like