Read sensor value 5min after event

I try to figure out how to read the values of a (temperature) sensor 5 minutes after an event (pump turned on) happend. I figured out how to create a custom sensor that only updates if a condition (pump turned on) is met, but I need to include a delay of 5 minutes. I tried with using an automation that allows for a time delay, but I failed to combine it with my custom sensor. Any help greatly appreciated!

My best solution so far but it lacks the 5min delay:

 - platform: template
        friendly_name: "supply temperature when heatpump is running"
        unit_of_measurement: '°C'
        icon_template: 'mdi:coolant-temperature'
        value_template: >
          {% if is_state('binary_sensor.vicare_compressor_active', 'on') %}
            {{states('sensor.vicare_primary_circuit_supply_temperature') }}
          {% endif %}

Background: I want to know the water temperature 5 minutes after the water pump started. If the pump is not running or only just started to run, the water in the pipe has room temperature and thus the sensor readings are irrelevant.

OK, I might have solved this myself. Not 100% sure though:

value_template: >
          {% if is_state('binary_sensor.vicare_compressor_active', 'on') and as_timestamp(now())-as_timestamp(states.binary_sensor.vicare_compressor_active.last_changed) > 5*60 %}
            {{states('sensor.vicare_primary_circuit_supply_temperature') }}
          {% endif %}

Is the pump turned on by an HA automation or is it a standalone device?

It’s standalone. The binary sensor indicates if the pump is running or not but it is not controlled from home assistant.

If you switch to the current template sensor style you can take advantage of trigger-based sensors. A basic example would be as follows:

  - trigger:
      - platform: state
        entity_id: binary_sensor.vicare_compressor_active
        to: 'on'
        for: "00:05:00"
      - name: "supply temperature when heatpump is running"
        unit_of_measurement: '°C'
        icon: 'mdi:coolant-temperature'
        state: "{{states('sensor.vicare_primary_circuit_supply_temperature') }}"

This makes the same assumption that your second post makes, i.e. that you only want to update the temperature after the compressor has been running for 5 minutes.

Nice, thanks a lot! A much cleaner solution. One thing that took me a while to understand though is that the code may not be written in sensors.yaml but directly into customize.yaml.