Help- how to compare a sensor value sent from HA with a variable in esphome

I setup a slider in HA with a range from 1-10 that gets passed to my esphome device. The variable in esphome updates each time a switch is pressed as the motor rotates. I’d like to compare the two values so when they match, the motor is turned off. I’ve been trying with a lambda, but have had no luck. The lambda below is my latest iteration. When I turn on the switch it runs thru immediately and turns off the motor, resets the variable to 0 and turns off the switch, ignoring anything in the lambda. Which I assume is formatted wrong.

 switch:
  - platform: template
    name: "Feed on/off"
    id: molly_switch
    restore_state: true
    turn_on_action:
      - switch.template.publish:
          id: molly_switch
          state: ON
      - switch.turn_on: 
          id: feeder
      - if:
          condition:
            - lambda: |-    
                if ((id(num_clicks) = (id("feed_molly"))));{
                return true;

                 }
          then:
          - delay: 5s
          - switch.turn_off:
              id: feeder
          - delay: 3s
          - lambda: |-
                 id(num_clicks) = 0;
          - switch.template.publish:
              id: molly_switch
              state: OFF

These are the sensors.

Sent From Home Assistant to ESPHome:

   sensor:    
      - platform: homeassistant
        name: "feed molly"
        entity_id: sensor.feed_volume_molly

Variable in ESPHome:

globals:
  - id: num_clicks
    type: int
    restore_value: yes
    initial_value: '0'

I ended up figuring this out. I ended up creating another sensor with the lambda function in it.

      - platform: template
        id: pineapple
        name: "pineapple"
        lambda: |-    
          if (id(feed_molly).state == id(num_clicks)){
            return true;}
            else  {
            return false;
          }

Then removed the if condition and changed it to a wait_until with the new sensor…

switch:
  - platform: template
    name: "Feed on/off"
    id: molly_switch
    restore_state: true
    turn_on_action:
      - switch.template.publish:
          id: molly_switch
          state: ON
      - switch.turn_on: 
          id: feeder
      - wait_until:
          binary_sensor.is_on: pineapple
      - delay: 3s
      - switch.turn_off:
          id: feeder
      - delay: 3s
      - lambda: |-
           id(num_clicks) = 0;
      - switch.template.publish:
          id: molly_switch
          state: OFF