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
trigger:
  - platform: numeric_state
    entity_id: sensor.your_sensor
    below: 50
condition: []
action:
  - service: input_datetime.set_datetime
    target:
      entity _id: input_datetime.your_input_datetime
    data:
      timestamp: "{{ now().timestamp() }}"

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

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

The same can be done for “Above Time”.