"Last Run" timestamp

I have a smart plug that I am monitoring power values for a device. How can I create a sensor to display “Last Run”? So basically, if the current or power value increased above a specific value and then dropped below a specific value for a predetermined amount of time, set a timestamp so I can display this in the frontend. Below is my typical sensor for these type of devices, which shows how long the device has been running. {Disclaimer: the “smart_plug_2_info” run time code is not mine, I just tailored it to my application, and I don’t use the “on after” portion}

  - platform: template
    sensors:
      smart_plug_2_status:
        friendly_name: 'Smart Plug 2 Status'
        value_template:  >
          {% if states.sensor.awp04l_plug_2_power.state | float > 100.0 %}
            {{ "Running" }}
          {% elif states.sensor.awp04l_plug_2_power.state | float > 5.0 %}
            {{ "On" }}
          {% elif states.sensor.awp04l_plug_2_power.state | float < 4.9 %}
            {{ "Off" }}
          {% else %}
            {{ "Unknown" }}
          {% endif %}
      smart_plug_2_info:
        friendly_name: 'Smart Plug 2 Info'
        entity_id: sensor.time
        value_template:  >
          {%- macro as_formatted_elapsed_time(now, other_date_time) %}
          {% set duration = as_timestamp(now) - as_timestamp(other_date_time) %}
          {% set seconds = (duration % 60) | int %}
          {% set minutes = ((duration / 60) | int) % 60 %}
          {% set hours = (duration / 3600) | int %}
          {{ [hours, "hours", minutes, "minutes"] | join(' ') }}
          {%- endmacro %}
          {% if states.input_text.smart_plug_2_enriched_status.state == "Running" %}
            Run time: {{ as_formatted_elapsed_time(now(), states.input_text.smart_plug_2_enriched_status.last_changed)}}
          {% elif states.input_text.smart_plug_2_enriched_status.state == "On_After_Running" %}
            Smart Plug 2 off for: {{ as_formatted_elapsed_time(now(), states.input_text.smart_plug_2_enriched_status.last_changed)}}
          {% elif states.input_text.smart_plug_2_enriched_status.state == "On_After_Off" %}
            {{ "Smart Plug 2 ready to start" }}
          {% else %}
            {{ "~~~" }}
          {% endif %}