Adjustable bed using ESP32 Home and MPU-6050 Acceleration Sensors

I’m working on a project that will add some Home Assistant smarts to my adjustable bed. I am using an ESP32 to control a set of relays to bypass the normal operating remote for a bed to raise and lower the head and the feet. I added two MPU-6050 Acceleration Sensors to the frame of the bed to monitor the tilt positions. What I am struggling with is having the ability to adjust the bed to predetermined tilt positions. My plan is to use a global variable in the ESPHome configuration to set the desired tilt positions and activate the motors to either lift or lower the bed and stop when it reaches that position. Here is what I’m trying that is not working for a button configuration. Any help or ideas would be appreciated.

button:
  - platform: template
    name: Craftmatic Zero G
    icon: "mdi:bed"
    on_press:
      then:
        - lambda: |-
            id(head_target) = 11;
            id(feet_target) = 44;
            ESP_LOGI("main", "head_target: %d. feet_target: %d", id(head_target), id(feet_target));
#    on_value_range: head_pitch
#    - above: 11
#      then:
#        - switch.turn_on: head_down
#    - below: 11
#      then:
#        - switch.turn_on: head_up

You need to put the above/below code on the sensor.
Now you have it on the button and the button does not have a value.

But having it on the sensor means it will probably not allow you to set a custom position.
You probably need a boolean as the condition on the sensor so that it knows if it’s supposed to move by itself or if you want to be in controll.

image