How to keep a rotary encoder (or other input-like sensor) synced with HA without looping

I’ve tried hiding the rotary encoder sensor and using a template sensor and manually sending the data when it changes for any reason other than data coming back from home assistant using a global variable. It does seem to work most of the time, though occasionally it does seem to still push an old value from HA through causing non-smooth volume changing using the the receiver it’s self.

globals:
  - id: should_send
    type: bool
    initial_value: 'true'

sensor:
  - platform: template
    name: "VKnob1 Value Template"
    id: exposed_volume
    update_interval: never
    accuracy_decimals: 3

  - platform: rotary_encoder
    name: "volume"
    internal: True # don't share this remotely
    id: rcvolume
    pin_a: D3
    pin_b: D2
    min_value: 0
    max_value: 200
    on_value:
      then:
        - script.execute: debounce_sendvolume
        - script.execute: pagetimeout

  # Sensor from home assistant showing onkyo volume.
  - platform: homeassistant
    name: "Onkyo Volume Acording to home assistant"
    internal: True # don't share this remotely
    id: "vknob_onkyo_volume"
    entity_id: media_player.onkyoreceiver
    attribute: volume_level
    accuracy_decimals: 0
    filters:
      - calibrate_linear:
          # Map values from 0-1 to 0-200
          - 0.0 -> 0.0
          - 1.0 -> 200
      - debounce: 1s
    on_value:
      - globals.set:
          id: should_send
          value: 'false'
      - sensor.rotary_encoder.set_value:
          id: rcvolume
          value: !lambda |-
            return id(vknob_onkyo_volume).state;
      - globals.set:
          id: should_send
          value: 'true'

script:
  - id: debounce_sendvolume
    mode: restart
    then:
      - delay: 0.1s
      - lambda: |-
          if(id(should_send)){
            id(exposed_volume).publish_state(id(rcvolume).state / 200.f);
          }