Keep last known rest sensor value

Apologies, I know this has been asked before, but I can’t find any good examples.
I have the following rest template sensor, which works fine, except when the disk goes to sleep (The rest sensor gives me the output from a S.M.A.R.T test). When this happens, I check the output from the rest call, and change the sensor values, usually to something like zero, but I’d like to keep the last known value instead, how might I go about this ?

The current sensor is below:-

- resource: http://someurl/output.txt
  scan_interval: 300 
  sensor:
    - name: main_hdd_mode_restful
      value_template: >
        {% if "Device is in" in value %}
        {{ (value|regex_findall("Device is in .*"))[0].split()[3] }}
        {%else%}
        Active
        {%endif%}
    - name: main_hdd_temperature_restful
      value_template: >
        {% if "Temperature_Celsius" in value %}
        {{ (value|regex_findall("Temperature_Celsius.*"))[0].split()[8] }}
        {%else%}
        0
        {%endif%}
      unit_of_measurement: "°C"
    - name: main_hdd_power_on_hours_restful
      value_template: >
        {% if "Power_On_Hours" in value %}
        {{ (value|regex_findall("Power_On_Hours.*"))[0].split()[8] }}
        {%else%}
        0
        {%endif%}
      unit_of_measurement: "hours"
    - name: main_hdd_health_restful
      value_template: >
        {% if "SMART overall-health self-assessment test result:" in value %}
        {{ (value|regex_findall("SMART overall-health self-assessment test result:.*"))[0].split(":")[1] }}
        {%else%}
        Unknown
        {%endif%}

Use {{ this.state }} in your else statement.

EDIT: This only applies to template sensors. See posts below.

I’ve actually now tried this and changed the template sensor to this:-

- resource: http://192.168.0.202:8000/DriveD.txt
  scan_interval: 300 #checks every 5 minutes, but the file is actually only updated every 40 minutes on the host machine.
  sensor:
    - name: main_hdd_mode_restful
      value_template: >
        {% if "Device is in" in value %}
        {{ (value|regex_findall("Device is in .*"))[0].split()[3] }}
        {%else%}
        Active
        {%endif%}
    - name: main_hdd_temperature_restful
      value_template: >
        {% if "Temperature_Celsius" in value %}
        {{ (value|regex_findall("Temperature_Celsius.*"))[0].split()[8] }}
        {%else%}
        {{ this.state }}
        {%endif%}
      unit_of_measurement: "°C"

But the entity is now showing the value ‘unknown’ and this is in the logs:-

Template variable error: 'this' is undefined when rendering '{% if "Temperature_Celsius" in value %} {{ (value|regex_findall("Temperature_Celsius.*"))[0].split()[8] }} {%else%} {{ this.state }} {%endif%}'

It will until the first value comes in.

If you’re trying this in the template editor, it will fail — that’s expected behaviour.

Thanks Troon, but looking at the entities state history, there was a value prior to it going unknown:-

image

Paste the config for that particular sensor please.

The sensors all now look like this:-
(The one I posted the screenshot from is at the bottom)

- resource: http://someurl/output.txt
  scan_interval: 300 
  sensor:
    - name: main_hdd_mode_restful
      value_template: >
        {% if "Device is in" in value %}
        {{ (value|regex_findall("Device is in .*"))[0].split()[3] }}
        {%else%}
        Active
        {%endif%}
    - name: main_hdd_temperature_restful
      value_template: >
        {% if "Temperature_Celsius" in value %}
        {{ (value|regex_findall("Temperature_Celsius.*"))[0].split()[8] }}
        {%else%}
        {{ this.state }}
        {%endif%}
      unit_of_measurement: "°C"
    - name: main_hdd_power_on_hours_restful
      value_template: >
        {% if "Power_On_Hours" in value %}
        {{ (value|regex_findall("Power_On_Hours.*"))[0].split()[8] }}
        {%else%}
        {{ this.state }}
        {%endif%}
      unit_of_measurement: "hours"
    - name: main_hdd_health_restful
      value_template: >
        {% if "SMART overall-health self-assessment test result:" in value %}
        {{ (value|regex_findall("SMART overall-health self-assessment test result:.*"))[0].split(":")[1] }}
        {%else%}
        {{ this.state }}
        {%endif%}

But all of them are now doing this, where I have replaced a default value with {{this.state}}, I’m now getting unknown, even after a valid state has been ‘seen’.

My apologies: it appears as though the this variable is only available in template entities.

So you could do this:

- resource: http://someurl/output.txt
  scan_interval: 300 
  sensor:
    - name: main_hdd_temperature_restful
      value_template: >
        {% if "Temperature_Celsius" in value %}
          {{ (value|regex_findall("Temperature_Celsius.*"))[0].split()[8] }}
        {%else%}
          retain
        {%endif%}

then:

template:
  - sensor:
      - name: Main HDD temperature
        state: "{{ states('sensor.main_hdd_temperature_restful')|int(this.state) }}"
        unit_of_measurement: '°C'
        device_class: temperature

Annoying to have to have two entities, but I think that’s your only solution.

The template sensor state tries to convert the restful sensor to an integer, and uses this.state if the conversion fails.

Thanks @Troon for all your help. That looks like a reasonable compromise to get what I need.