Preserve last value of sensor before becoming unavailable

Hi,

there have been some threads about this topic already but I haven’t found a suitable solution with non MQTT values.

In order to preserve last value of a sensor before becoming unavailable I created a input_number helper called

input_number.river_pro_101859_battery_preserved

furthermore I created an automation which triggers at each state change but not if sensor is unavailable

- id: '*******************'
  alias: Preserve State - River Pro Battery Value
  description: ''
  trigger:
  - platform: state
    entity_id:
    - sensor.river_pro_101859_battery
  condition:
  - condition: not
    conditions:
    - condition: state
      entity_id: sensor.river_pro_101859_battery
      state: unavailable
  action:
  - service: input_number.set_value
    data:
      value: '{{ states(''sensor.river_pro_101859_battery'') }}'
    target:
      entity_id: input_number.river_pro_101859_battery_preserved
  mode: single

last but not least I created a sensor based on this input number to display it in lovelace in a nicer manner

- platform: template
  sensors:
    river_pro_101859_battery_preserved:
      friendly_name: River Pro 101859 Battery Preserved
      icon_template: mdi:battery-90
      unit_of_measurement: "%"
      value_template: >-
        {{ states('input_number.river_pro_101859_battery_preserved')|int(0) }}

This does work and first of all it may help some people here setting it up.

However, since I have many sensors, I’d like to preserve the last state before being unavailable in a more streamlined way.

i.e. is there a way to do it without an automation, directly in configuration.yaml? Maybe as a template sensor?

Best
Pete

yes, you can do that.

let’s say you have the real sensor value (your sensor.sensor.river_pro_101859_battery) you can create a template sensor to to retain the last good value (your sensor.river_pro_101859_battery_preserved).

- platform: template
  sensors:
    river_pro_101859_battery_preserved:
      friendly_name: River Pro 101859 Battery Preserved
      icon_template: mdi:battery-90
      unit_of_measurement: "%"
      value_template: >-
        {% if  states('sensor.river_pro_101859_battery') not in ['unknown', 'unavailable'] %}
          {{ states('input_number.river_pro_101859_battery') }}
        {% else %}
          {{ states('input_number.river_pro_101859_battery_preserved') }}
        {% endif %}
1 Like

do you consider this code as a replacement for the automation?

I think in your code the setting of input_number is missing. the value of

input_number.river_pro_101859_battery_preserved

needs to be defined before it can be written to

sensor.river_pro_101859_battery_preserved

or did I miss something here?

oops. I copy/pasted.

here is a better (correct) version:

- platform: template
  sensors:
    river_pro_101859_battery_preserved:
      friendly_name: River Pro 101859 Battery Preserved
      icon_template: mdi:battery-90
      unit_of_measurement: "%"
      value_template: >-
        {% if states('sensor.river_pro_101859_battery') not in ['unknown', 'unavailable'] %}
          {{ states('sensor.river_pro_101859_battery') }}
        {% else %}
          {{ states('sensor.river_pro_101859_battery_preserved') }}
        {% endif %}

Sorry about that

6 Likes

Hey finity,

great. that’s it.
I was under the impresssion that you can’t refer a sensor to itself within one template - no idea why.

Thank you

1 Like

Hi, thanks for the code

I have used the exact same code, but my template sensor value shows unknown once I reboot the HA.

Any workaround?

try the following:

- platform: template
  sensors:
    river_pro_101859_total_input_preserved:
      friendly_name: River Pro 101859 Total Input Preserved
      icon_template: mdi:flash
      unit_of_measurement: W
      value_template: >-
        {% if states('sensor.river_pro_101859_total_input') not in ['unknown', 'unavailable'] %}
          {{ states('sensor.river_pro_101859_total_input')|float(0)|round(0) }}
        {% else %}
          {{ ( states ('0.0')|float(0))|round(0) }}
        {% endif %}

Edit: that’s not exactly the same code but the trick is the |float(0) part.

Hi Pete, thanks for the super quick response. I actually tried your suggested code. And after testing, I found that when my device (solar inverter) is available and I restart HA, everything is okay. But the the solar inverter is offline and I restart HA, the entities again go back to unknown/unavailable.

I am sharing my code below. It would be great if you could suggest a solution to it.

  - platform: template
    sensors:
      mauli_solar_energy_total_preserved:
        friendly_name: Solar Production Total Preserved
        unit_of_measurement: "kWh"
        value_template: >-
          {% if states('sensor.mauli_solar_energy_total') not in ['unknown', 'unavailable'] %}
            {{ states('sensor.mauli_solar_energy_total')|float(0)|round(0) }}
          {% else %}
            {{ states('sensor.mauli_solar_energy_total_preserved') }}
          {% endif %}

Hi,

this sounds to me that the value of the sensor mauli_solar_energy_total_preserved is being reset at boot. And since the inverter is offline it can’t get a valid number from the if statement.

You could add | float(0) to the else statement as well to replace the unkown state by ‘0’ but I guess that’s not want you want, right?

By the way, did you create an input_number helper?