Sensor: If "not available" occurs keep the previous value

Hello Folks.

I use an sensor entity coming from external website and sometimes I got back the “Not available” value. There is no way to solve this issue with the support of this external service.

So what I’m looking for it’s a way to configure the entity in this way: If “not available” occurs keep the previous value. Any idea? Maybe a template sensor?

Thanks a lot

Yes, this can be done. Post your existing code, formatted properly (</> button).

Thank you @Troon
At moment I don’t have a code because I’m getting the sensor from an integration “OpenUV”: sensor.current_uv_index

But maybe the code to template it could start with:

# Filtered OpenUV sensor
  - platform: template
    sensors:
      sensor.current_uv_index_filtered:
        friendly_name: "Open UV sensor filtered"
        value_template: >-
          {% if is_state('sensor.current_uv_index', 'unknown') %} ???????????????

Basically turning your topic subject into code:

# Filtered OpenUV sensor
  - platform: template
    sensors:
      current_uv_index_filtered:
        friendly_name: "Open UV sensor filtered"
        value_template: >-
          {% if is_state('sensor.current_uv_index', 'unknown') %}
            {{ states('sensor.current_uv_index_filtered') }}
          {% else %}
            {{ states('sensor.current_uv_index') }}
          {% endif %}

This will retain the prior value for a potentially-unlimited amount of time if your source sensor goes down, so it might be worth setting up an automation to alert you if it’s unknown for too long.

4 Likes

Thank you!