ESPHome help with coding for display

I am trying to create code for an Ultrasonic Sensor in ESPHome to change my display once a sensor reads the same reading for 5 minutes.

So my ultrasonic sensor will trigger once it reads my car pulling and and change my display as I have written below. Once my car is parked, the sensor should read the same. I would like to once default back to the time display once the car is safely parked to save the display burnout.

Can anyone assist?

Here is my code so far. I have been reading around with conditions and states, but it is hard to pull the previous state and compare it to the current reading. Anyone can help???

substitutions:
  esphome_name: garagedistance

esphome:
  name: ${esphome_name}
  platform: ESP32
  board: esp-wrover-kit

# Enable logging
logger:
  level: debug

# Enable Home Assistant API
api:

ota:
  password: ""

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Garagedistance Fallback Hotspot"
    password: "L5xk6KRzodqq"

captive_portal:

# Example configuration entry
esp32_ble_tracker:
bluetooth_proxy:

time:
  - platform: homeassistant
    id: hass_time

switch:
#  - platform: gpio
#    name: "${esphome_name}-Onboard-LED"
#    pin: 0
#    inverted: True
  - platform: restart
    name: ${esphome_name}-restart
    id: restart_switch

text_sensor:  
  - platform: version
    name: "${esphome_name}_ver"

font:
  - file: "arial.ttf"
    id: digit_font
    size: 11
  - file: "Eight-Bit-Dragon.ttf"
    id: dragon_font
    size: 8

# Example configuration entry
light:
  - platform: neopixelbus
    type: GRBW
    variant: SK6812
    method:
        type: esp32_i2s
        bus: 0
    pin: 15
    num_leds: 4
    name: "garage_box_light"

# Example configuration entry
sensor:
  - platform: ultrasonic
    trigger_pin: 22
    echo_pin: 23
    name: "GAR_Ultrasonic"
    id: garultra
    update_interval: 2s

spi:
  clk_pin: 14
  mosi_pin: 12


display:
  - platform: max7219digit
    cs_pin: 13
    num_chips: 4
    intensity: 15
    lambda: |-
      it.scroll(false);
      if (isnan(id(garultra).state)) { 
        it.strftime(3, -2, id(digit_font), "%H:%M", id(hass_time).now());
      } else {
        it.printf(0, 0, id(dragon_font), "D:%.2f", id(garultra).state);
         
                  
      }

I would a create a binary sensor that tracks when you want to change the screen.

This or very similar should work

binary_sensor:
  - platform: template
    name: "Save Display"
    lambda: |-
      if (id(garultra).state < 30) {
        // Car is parked. Adjust value as required.
        return true;
      } else {
        return false;
      }
    filters:
      - delayed_on: 5min # Adjust as required.

Then you can use this trick to “fake pages” and change the page as part of on_press / on_release binary_sensor triggers.

The values of those sensors tend to bounce around a bit, so the < will help with that noise.

Great suggestion. I will give this a try. Thanks!!!