That super simple bit of code can be built and uploaded to Arduino compatybile device.
It can be used to toggle state of relay based on reads from HC-SR04
distance sensor. In a very small room that could translate to whether there is a person in the room or not.
It provides basic relay state change debounbcing to avoid flickering.
It filters out bad distance sensor reads based on configuration.