Automate TV bias light based on lux

Hi, I’m struggling to automate my bias TV light to turn on when lux is below 40 and TV is on, any help will be much appreciated.

I have 4 things I’d like to happen

  1. when TV turns on and lux already below 40 light turns on

  2. When TV already on and lux goes below 40 light turns on

  3. When TV already on and lux goes above 40 light turns of

  4. When TV turns off light turns off

Number 1 above I really struggle with and would appreciate help with the whole script.

Cheers

I don’t think that you need a script for this. An automation should suffice.