Turn screen off if tilted a certain way Phones in bed mean poor sleeping habits.

How would one go about turning the screen off, or fuzzing the screen (probably more practical, less subject to temporary tilt), if the device is tilted in certain ways?

For example,


Device is vertically upside down.
Device is locked in Portrait mode and horizontal.
Screen is facing the center of the Earth.


Answers using apps will be accepted, but, out of interest in the theory, I will also accept answers that outline an app to do such a thing. I know that newer Android versions have screen filters and tilt sensors.
Device is vertically upside down.
  Device is locked in Portrait mode and horizontal.
  Screen is facing the center of the Earth.
  


I suggest you try Automate (user guide) as well as Tasker (free trial; user guide). Both of them offer the features you're seeking. A peek:

(Click an image to enlarge it)





Before I leave you here to tinker with those options, here is some information to ease the tinkering:

In Automate app:


Device Orientation block can be found under Sensors.
Device lock can be found under Interface. Practical usage requires setting Automate as device administrator.


Tip: It is easy in Automate to trigger an action on a prerecorded motion gesture

In Tasker app:


Orientation trigger can be found under State &rarr; Sensor.
Display Orientation trigger can be found under State &rarr; Display. 
System Lock action can be found under Display.


Tip: In Tasker you can setup multiple contexts.