Display time since sensor below or above specific value

Hello all,

I want to display the time since a specific sensor value went below a certain threshold or above it.

What would be the best way to do this in your view?

Create an automation that records the time, when the sensor value decreases below the threshold, in an Input Datetime.

alias: example
  - platform: numeric_state
    entity_id: sensor.your_sensor
    below: 50
condition: []
  - service: input_datetime.set_datetime
      entity _id: input_datetime.your_input_datetime
      timestamp: "{{ now().timestamp() }}"

Create a Template Sensor that reports the time difference between the current time and the value in the Input Datetime.

  - sensor:
      - name: Below Time
        state: "{{ relative_time(states('input_datetime.your_input_datetime') | as_datetime) }}"

The same can be done for “Above Time”.