Last valid state value

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

Replace sensor.actual_toner_capacity with the name of your sensor (that reports residual toner capacity).

- platform: template
  sensors:
    toner_capacity:
      value_template: >-
        {% set tc = states('sensor.actual_toner_capacity') %}
        {{ tc if tc != 'unknown' else states('sensor.toner_capacity') }}
  • 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.
6 Likes

SOLVED! Thank You Taras
Alessandro

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.