Need help fixing a template sensor

Hi,

I have solar panels and using Envoy integration within HA to monitor production and other characteristics using Grafana and InfluxDB. The solar production is quiet variable depending on sunlight and weather condition so I wrote a template sensor (for each panel) as follows:

template:
 - sensor:
     - name: Solar Panel-01
       state_class: measurement
       icon: mdi:solar-panel
       unit_of_measurement: W
       device_class: power
       state: >
         {% set value = states('sensor.inverter_1') | int %}
         {% if value  <= 4 -%}
           0
         {% elif is_state('sun.sun','below_horizon') %}
           0
         {%- else -%}
           {{ value }}
         {%- endif %}

The above sets the value of production to “0” once the output from panel drops below “4” for any reasons, the UI looks really clean!

But I am also seeing following error in the logs:

TemplateError('ValueError: Template error: int got invalid input 'unavailable' when rendering template '{% set value = states('sensor.inverter_1') | int %} {% if value <= 4 -%} 0 {% elif is_state('sun.sun','below_horizon') %} 0 {%- else -%} {{ value }} {%- endif %}' but no default was specified') while processing template 'Template("{% set value = states('sensor.inverter_1') | int %} {% if value <= 4 -%} 0 {% elif is_state('sun.sun','below_horizon') %} 0 {%- else -%} {{ value }} {%- endif %}")' for attribute '_attr_native_value' in entity 'sensor.solar_panel_1'

Guess that there are times when the sensor is unavailable, likely due to Envoy dongle not connected to WiFi and the template sensor is unable to parse its value.

What will be the easiest or the most practical way to fix it?

It would depend a little on what you want the behaviour to be. If the underlying sensor is unavailable, do you still want a value from your sensor? If so, provide int with a default value (e.g. int(0)). Otherwise, provide an availability template for the sensor.

When sensor.inverter_1 is unavailable what value do you want the Template Sensor to report?

For example, if you add a default value to the int filter, you can influence the result to be zero.

         {% set value = states('sensor.inverter_1') | int(0) %}
         {% if value  <= 4 -%}
           0
         {% elif is_state('sun.sun','below_horizon') %}
           0
         {%- else -%}
           {{ value }}
         {%- endif %}

Setting default to “0” would mislead the Grafana charts as the sensor being “unavailable” is a legit scenario in the setup. So thought that I will put the existing template code in another if-else statement:

{% if states('sensor.inverter_1') in ("available") %}
  {% set value = states('sensor.inverter_1') | int %}
  {% if value  <= 4 -%}
    0
  {% elif is_state('sun.sun','below_horizon') %}
    0
  {%- else -%}
    {{ value }}
  {%- endif -%}
{% else %}
  unavailable
{%- endif -%}

But the above code always show the output as “unavailable”, while I can see that this inverter is actually working and can see its output in the following command.

{{states('sensor.inverter_1')}}

Also curious what is the difference between the two syntax below, one uses “-” and the other one does not but seems to produce same output from the template:

{% else %}

and

{%- else -%}

OK first -
if the state is NOT unavailable then it has a value - so it will never find the state “available” that is why it is always unavailable.

Second - using - in the if and else statements, removes newlines (you can play about in the template tool to see the difference.

Third - if the inverter state did report it was available, then you would want to use:

{% if states('sensor.inverter_1') in ["available"] %}

Anyway:

{% if states('sensor.inverter_1') not in ["unavailable","unknown"] %}
  {% set value = states('sensor.inverter_1') | int %}
  {% if value  <= 4 -%}
    0
  {% elif is_state('sun.sun','below_horizon') %}
    0
  {%- else -%}
    {{ value }}
  {%- endif -%}
{% else %}
  unavailable
{%- endif -%}

See how that works.

And if you don’t want to mess about remembering to use the hyphen on multiple lines to remove newlines on other sensors, change state: > to state: >- and it will do the same thing.

1 Like

Do what parautenbach suggested and use the Template Sensor’s availability option with a template like this:

{{ states('sensor.inverter_1') != 'unavailable' }}

Wow, it worked like a charm :sunglasses:

Thanks @123 @mobile.andrew.jones & @parautenbach :pray:

1 Like