Switch with auto logic, user override, and reset

Here’s what I’m trying to achieve:

Automatic Logic: The switch should turn off if there are less than 6 hours until midnight and turn on otherwise:

$if(tf(0h0m0sa1d, hh)<6,0,1)$

User Input: User can manually toggle the switch. This input should override the automatic logic.

Reset: If user overrides, I want the switch to reset to auto logic whenever that changes. This could reference the auto logic but I presume it’s safer to hard-code it.

Visually this will be a switch button recreated from Material UI. Another way to phrase what I need is: ‘actual state’, ‘auto logic’ and ‘manual state’. The button appearance should be determined by actual state, the touch action should affect manual state. As long as I get my desired logic working, that should be easy to implement myself. I’ve tried a few ways to do this but especially with the reset functionality it’s a bit too complex for me…

I’d very much appreciate any help or advise with this!