Sensor offline, show latested data

I have an ESP that reads the data from my inverter, and when the inverter turns off, the ESP also turns off. In HA, I then see ‘Unavailable’ for the sensors. For at least two sensors, I want the last value to be displayed. Can I solve this in ESPHome, or do I need to build something for this in HA?

Sample

  - platform: template
    name: "Energy Total"
    id: sensor_energy_total
    unit_of_measurement: kWh
    accuracy_decimals: 0
    state_class: total_increasing
    icon: mdi:solar-power
    device_class: energy
    filters:
      - filter_out: nan

Does this not do what you want?

It should. Maybe you need this:

    filters:
      - filter_out: nan
      - heartbeat: 60s

That will ensure the last good value keeps being sent every 60s.

You likely want to make template sensor on HA to present last value from esp.

Unfortunately this will not work (or with more than 60), offline = offline :slight_smile:

  - platform: template
    name: "Energy Total"
    id: sensor_energy_total
    unit_of_measurement: kWh
    accuracy_decimals: 0
    state_class: total_increasing
    icon: mdi:solar-power
    device_class: energy
    filters:
      - filter_out: nan
      - heartbeat: 60s




If I create a second (template) sensor, will the data also be logged twice? (I’m trying to prevent data pollution.)

template
  - sensor:
      - name: "Energy total (last value)"
        state: >
          {% if states('sensor.esp32_serial_logger_energy_total') not in ['unknown', 'unavailable'] %}
            {{ states('sensor.esp32_serial_logger_energy_total') }}
          {% else %}
            {{ this.state }}
          {% endif %}

I don’t believe you will be able to solve this with esphome (with the current behavior, I would power the esp independently and the you can solve it with esphome).

Yes, why? (how is this pollution? It is more data than the absolute minimum necessary.)

You have a set of “requirements” that are likely contradictory. This is what it sounds like to me.

  1. I want every detail saved
  2. I don’t want extra data
  3. The thing that generates the data I want is not available sometimes
  4. When the data is not available I want the last value that was available

There was an HA system that operated that way that I used a decade ago (OpenHAB). I hated it, since I could never tell (for sure) when a sensor went offline.

You need to be clear about the relative priority of your conflicting requirements. Most HA data is incredibly boring, but there are nuggets of value/interest in it. For my system, I am more than happy to have many GB of boring (semi-duplicated) data as long as it allows me to find the moderately interesting thing I want.

I use this in the configuration.yaml:

 - trigger:
      - trigger: state
        entity_id: sensor.wt32_eth01_pool_temperature
        not_to:
          - unknown
          - unavailable
    sensor:
      - name: Pool Temperature_2
        state: "{{ trigger.to_state.state }}"
        device_class: temperature
        unit_of_measurement: "°C"

replying because my issue is kind of related, opposite of what OP wants.
I’m using power monitor smart switch with openbeken flashed.
the smart switch doesn’t have backup power. homeassistant server has backup power.
it shows the last available value when the smart switch looses powere/goes offline. and continues to put that same value in the graph until power comes back.
I don’t want that. I want it to say offline/unavailable when it is offline. and enter null data or something in the graph.
It used to be like that, but I updated home assistant a few days ago from 2025.4.x to 2026.x it started doing that after the update.

sorry again for hijackiing OP’s thread, posted here just because it is very relavent.

Thanks.

What exactly are you trying to say, and how are you helping?

I used to ‘run’ my home on a Raspberry Pi 2 using Bash and Python, and I wrote my code in the Arduino IDE. Until I got pushed into using Home Assistant ;-). Only later did I realize how easy ESPHome actually is. That said, with the rules built into Home Assistant, it sometimes feels less logical to me than Python and Bash. I still get stuck on that from time to time and end up asking for help here.

Thanks, what would be the advantage/disadvantage of these different solutions in the configuration.yaml?

yours:

template
 - trigger:
      - trigger: state
        entity_id: sensor.wt32_eth01_pool_temperature
        not_to:
          - unknown
          - unavailable
    sensor:
      - name: Pool Temperature_2
        state: "{{ trigger.to_state.state }}"
        device_class: temperature
        unit_of_measurement: "°C"

mine:

template
  - sensor:
      - name: "Energy total (last value)"
        state: >
          {% if states('sensor.esp32_serial_logger_energy_total') not in ['unknown', 'unavailable'] %}
            {{ states('sensor.esp32_serial_logger_energy_total') }}
          {% else %}
            {{ this.state }}
          {% endif %}

Mine is working :slight_smile:

haha, mine also :wink:

This is what I said:

You asked if it was possible to solve with esphome or of you needed to use HA. I said I didn’t believe it was possible to solve with just esphome (since the device loses power).

I then asked clarifying questions, because your statements didn’t make sense to me.

Sorry you didn’t find them helpful.

1 Like