If you want a script to run when the TV/media player turns on, make an automation with the trigger being the media player being turned on and the action being the script. Did I understand you correctly?
Look at the service_template you’ve created and ask yourself what happens when the media_player is noton? What result does the template produce?
The answer is it produces nothing. That means it assigns nothing to service_template and will cause an error because Home Assistant needs the name of a valid service.
In your example, there is probably no other service that you want to run when the media_player is off. Therefor you need to move this test (Is the media_player on?) out of the service_template and into either the condition or, if feasible, into the trigger (as suggested by Tediore).
Ideally, you want the automation to determine if it should, or should not, run the service before it gets to the action section.