Template sensor - Overcome unavailable or NaN

Hi, just need some confirmation.help.

I have sensor I use in a template sensor (for energy - strictly increasing :slight_smile: ). But it is not always increasing due to some “flaws” .

The original sensor sometimes is “unavailable”:
image
(envoy_today_gen)

This end up like:
image
(solar_today_gen)

When I use this template:

      solar_today_gen:
        unit_of_measurement: 'kWh'
        friendly_name: Total Solar Today
        device_class: energy
        value_template: >
            {% set growattodaykwh = states('sensor.growattmodbus_today_gen') | float(0) %}
            {% set envoytodaykwh = states('sensor.envoy_today_gen') | float(0) / 1000 %}
            {{ [ growattodaykwh + envoytodaykwh, 0 ] | max | round(2) }}

I think I need something like this:

envoy_today_gen_preserved:
      value_template: >-
        {% if states('sensor.envoy_today_gen') not in ['unknown', 'unavailable'] %}
          {{ states('sensor.envoy_today_gen') }}
        {% else %}
          {{ states('sensor.envoy_today_gen_preserved') }}
        {% endif %}

But I don’t know how to “combine” them (syntax wise)…

you should use the “availability” option for your template:

solar_today_gen:
        unit_of_measurement: 'kWh'
        friendly_name: Total Solar Today
        device_class: energy
        value_template: >
            {% set growattodaykwh = states('sensor.growattmodbus_today_gen') | float(0) %}
            {% set envoytodaykwh = states('sensor.envoy_today_gen') | float(0) / 1000 %}
            {{ [ growattodaykwh + envoytodaykwh, 0 ] | max | round(2) }}
        availability: >
          {{ states('sensor.NAME') not in ['unknown', 'unavailable', 'None'] }}

The problem might be, that you need to cover BOTH sensors in your availability check…
I am not sure, if this can be covered…

Other than that:
You could try to solve that with existing UI options…

You might need to check, if you need to change the unit of measurement for the envoy_today_gen within the settings of the sensor

then, you could create a new sensor group where you add both → envoy_today and growattmodbus and use Type=> Sum

the other option would be, to create sensor templates with the correct unit of measurements based on your current “variables”
And then use the sensor group to create the sum.
This should overcome the “availability” issue - if you use the availability on both sensor templates directly

Use the template editor in developer tools to test templates. But along the lines of this for a double sensor availability check (with some spacing so it’s easier to see what it does):

{{ 
    (states('sensor.NAME') not in ['unknown', 'unavailable', 'None']) 
       and  
    (states('sensor.NAME2') not in ['unknown', 'unavailable', 'None']) 
}}

Never knew this option! Done and will check! Thank you!

Not sure on how to use this…

Something like this? would that take out spike when one of the tow sensors is “not sending a value”?

sensor:
  - platform: template
    sensors:
      solar_today_gen:
        unit_of_measurement: 'kWh'
        friendly_name: Total Solar Today
        device_class: energy
        value_template: >
            {% set growattodaykwh = states('sensor.growattmodbus_today_gen') | float(0) %}
            {% set envoytodaykwh = states('sensor.envoy_today_s_energy_production') | float(0) %}
            {{ [ growattodaykwh + envoytodaykwh, 0 ] | max | round(2) }}
		availability_template:
			{{ 
				 (states('sensor.sensor.growattmodbus_today_gen') not in ['unknown', 'unavailable', 'None']) 
					and  
				 (states('sensor.envoy_today_s_energy_production') not in ['unknown', 'unavailable', 'None']) 
			 }}