After some deep research on how to execute a script using a service/unit but only when the system is being powered off, I managed to find a conditional execution that determines if this is a power down using the systemctl list-jobs command:
Determining within a script if the system is going to poweroff:
jobs=$(systemctl list-jobs)
if echo "$jobs" | grep -q "poweroff.target.*start"; then
echo "System is going to poweroff"
exit 0
fi
Anyway, as pointed out by @G-ManSays'ReinstateMonica ' in a comment, Piotr Jurkiewicz’s answer to a similar question proposes a simpler soluction that does address the specified objective. (Thank you @G-ManSays'ReinstateMonica for raising this to my attention. Really appreciated)
OBS: The only actual advantage this approach offers over Piotr's approach is that it allows the script to be run before filesystems get mounted readonly. But to achieve the goal I had when asking, it didn't make any difference anyway.
/usr/lib/systemd/system-shutdowndirectory.” The script will be run with one argument: “either "poweroff", "halt", "reboot" or "kexec", depending on the chosen action.” So you should try writing a script that looks at its argument and switches the TV off if the argument is "poweroff" (or "halt"?), but not "reboot".the/usr/lib/systemd/system-shutdown/directory is a simpler to address the specified problem. Thank you. OBS: After some research on how to do this, I did found a way of doing this using a service (I'll answer my own question), but a simpler solution will be better.