Issues with sensor template offset when data unavailable

I successfully managed to create sensor that adds 7 to the value. The issue is that the Telldus API loses connection several times a day and reports ‘unavailable’. The template then adds 0+7 = 7. This makes the graphs screw up.

Do you have an idea how to fix this problem?

- platform: template
  sensors:
    krypgrund_humidity_offset:
      value_template: '{{ (states.sensor.krypgrund_humidity.state | float + 7) | round(0) }}'
      unit_of_measurement: '%'

Try this instead:

value_template: "{{ (states('sensor.krypgrund_humidity') | float + 7) | round(0) }}"

Capture

Thank you! I have set up a sensor based on your suggestion. Now wait for an API-timeout.

It did not seem to work :frowning: The issue persists:

Capture

try this for the template:

value_template: >
  {% if states.sensor.krypgrund_humidity.state != 'unavailable' %}
    {{ (states.sensor.krypgrund_humidity.state | float + 7) | round(0) }}
  {% else %}
    {{ states.sensor.krypgrund_humidity_offset.state }}  
  {% endif %}

It almost worked. The state probably was something else than unavailable, possibly unknown, null or similar.

This modification did the trick:

  value_template: >-
    {% if states('sensor.krypgrund_humidity') | float > 40 %}
      {{ (states('sensor.krypgrund_humidity') | float + 7) | round(0) }}
    {% else %}
      {{ states('sensor.krypgrund_humidity_offset') }}
    {% endif %}

Thank you!