Determine if rotary encoder sensor is moving or not

I’m trying to make a sensor that determines if a rotary encoder is turning (sensor.esphome_web_5355c8_encoder). Does anybody have a clue for me, please?

  - platform: template
        friendly_name: "Rotary Encoder Turning"
        value_template: >
          {% set current_position = states('sensor.esphome_web_5355c8_encoder') | float %}
          {% set previous_position = state_attr('sensor.rotary_encoder_turning', 'previous_position') | default(0) | float %}
          {% set threshold = 1.0 %}  # Adjust this threshold as needed
          {{ (current_position - previous_position) > threshold }}
          previous_position: "{{ states('sensor.esphome_web_5355c8_encoder') }}"

use the derivative integration to create a sensor. It will show you the rate of change a sensor has.

Your other option is to make a template sensor that calculates the difference between the current state change and the last state change. With a periodic trigger that returns zero when there are no state changes.

Keep in mind that HA only knows the state and the time it was reported. Therefore HA will never know if something is currently happening. Instead HA can only tell you how recently something has happened.

So you need to re-frame your question with this in mind. You can’t create a sensor that tells you if the knob is turning, but you can create a sensor that tells you if the knob has been changed in the past 5 seconds, for example.