Ultrasonic sensor readings condition

I just installed an Ultrasonic sensor (Ultrasonic Distance Sensor — ESPHome) within a cabinet and I want to receive sensor readings whenever the cabinet is open instead of the whole time, let’s say every 2 seconds, to preserve the sensor. The cabinet will only open a few times a week with a motor connected to a relay.

So whenever the relay is activated I want to receive sensor readings. Is this possible?