I used macrodroid to toggle torch by pressing power button 3 times and only when it is dark(light sensor reading<3lx) and screen locked. And it also puts a notification saying "Flashlight ON". To easilly turn off this I created another profile with trigger as the "Flashlight ON" notification and what it does is turn off torch/flashlight when the notification is cleared.
I tried to the exact same thing with tasker but facing difficulties with the notification on and off triggers. Can anyone help?