@Alessandro_Bacioccol (Alessandro Bacioccola) posted a new topic, subject "Last valid state value". It read:

   Hello,  
    I am struggling getting the last valid value from a sensor. What I mean is:
    
      * I've a sensor giving me the residual capacity of my printer toner
      * The sensor return: the value when printer is ON, unknown when is OFF
      * Then I would like to keep the last value got from the sensor when the printer is OFF
    
    
    
    Do you have any suggestion?
    
    Thanks in advance  
    Alessandro

After which, @123 (Taras) added

    Replace `sensor.actual_toner_capacity` with the name of your sensor (that reports residual toner capacity).
    [code] 
        - platform: template
          sensors:
            toner_capacity:
              value_template: >-
                {% set tc = states('sensor.actual_toner_capacity') %}
                {{ tc if tc != 'unknown' else states('sensor.toner_capacity') }}
        
    [/code]
    
      * If the sensor reports a value that is _not_ `unknown`, it becomes the new value for `sensor.toner_capacity`.
      * If the sensor reports a value that is `unknown`, then `sensor.toner_capacity` retains its existing value.

Then @Alessandro_Bacioccol (Alessandro Bacioccola) said

    SOLVED! Thank You Taras  
    Alessandro

@123 (Taras) replied

    Alessandro_Bacioccol:
    
    > SOLVED! Thank You Taras
    
    You're welcome!
    
    Please consider marking my post with the Solution tag. It will automatically place a check-mark next to the topic's title which signals to other users that this topic has an accepted solution. Effectively, it indicates your question is resolved and helps users find answers to similar questions.


{'source': 'discourse', 'url': 'https://community.home-assistant.io/t/last-valid-state-value/280683', 'title': 'Last valid state value', 'views': 862, 'has_accepted_answer': False, 'word_count': 204, 'like_count': 4, 'reply_count': 1, 'participant_count': 2, 'posts': 4, 'tags': 'templates', 'author': '@Alessandro_Bacioccol (Alessandro Bacioccola)', 'participants': '@Alessandro_Bacioccol (Alessandro Bacioccola), @123 (Taras)', 'image_url': [None], 'created_at': '2021-02-14T14:42:21.308Z', 'last_posted_at': '2021-02-14T21:44:31.152Z'}