Condition in automation?

How can I add a condition to an automation?

I have this:

sensor:
  - platform: mpu6050
    address: 0x68
    update_interval: 102s
    accel_x:
      name: "MPU6050 Accel X"
    accel_y:
      name: "MPU6050 Accel Y"
    accel_z:
      name: "MPU6050 Accel z"
    gyro_x:
      name: "MPU6050 Gyro X"
    gyro_y:
      name: "MPU6050 Gyro Y"
    gyro_z:
      name: "MPU6050 Gyro z"
    temperature:
      name: "MPU6050 Temperature"
      
  - platform: ultrasonic
    trigger_pin: GPIO12
    echo_pin: GPIO14
    update_interval: 1s
    name: "Ultrasonic Sensor"
    on_value_range:
      - below: 0.5
        then:
          - dfplayer.play_next:

Now I want to limit the dfplayer to only play when the acceleration of x is more than 7.

I believe I need to add id’s to the acceleration variables, but what should I do with the automation? Lambda?

sensor:
  - platform: mpu6050
    address: 0x68
    update_interval: 102s
    accel_x:
      name: "MPU6050 Accel X"
      id: "x"
    accel_y:
      name: "MPU6050 Accel Y"
      id: "y"
    accel_z:
      name: "MPU6050 Accel z"
      id: "z"
    gyro_x:
      name: "MPU6050 Gyro X"
    gyro_y:
      name: "MPU6050 Gyro Y"
    gyro_z:
      name: "MPU6050 Gyro z"
    temperature:
      name: "MPU6050 Temperature"
      
  - platform: ultrasonic
    trigger_pin: GPIO12
    echo_pin: GPIO14
    update_interval: 1s
    name: "Ultrasonic Sensor"
    on_value_range:
      - below: 0.5
        then:
          - lambda: !lambda |-
              if (id(x).state) {
                return dfplayer.play_next:
              }

Is that correct??

EDIT:

Is there any way to figure out if the value of the ultrasonic sensor is increasing or decreasing, and only run the action if decreasing?