I really also wanted this feature but found out that none of the suggested solutions are ideal. I wanted to stop the Live TV almost instantly when TV is switched off. I tried to see if commands
tvservice -s or
fbset return any different information when the TV is on or off but those returned same output regardless if the TV was on or off.
However, I found out one easy solution that works if you have connected the Kodi via HDMI to a receiver which has network connection and you have connected the receiver to your local network. You can simply monitor when your receiver stops responding to
ping and then call Kodi with JSON-RPC API to stop playback.
For example, my receiver has IP 192.168.1.111 and I have enabled HTTP JSON RPC from kodi settings (see this:
http://kodi.wiki/view/JSON-RPC_API ). I have then added the following script via SSH to system running Kodi to
/storage/scripts/detect_tv_off.sh
Code:
#!/bin/bash
if ping -c 1 -W 1 192.168.1.111 >/dev/null; then
echo "$(date -Iseconds): TV is on. Doing nothing."
else
echo "$(date -Iseconds): TV is off. Stop Kodi playback."
curl -H "Content-Type: application/json" -X POST -d '{ "jsonrpc": "2.0", "method": "Player.Stop", "params": { "playerid": 1 }, "id": "mybash"}' http://username:[email protected]:8080/jsonrpc
echo ""
fi
Then with
crontab -e, I modified crontab to have these:
Code:
0 3 * * * echo "$(date -Iseconds): Log cleared" > /storage/scripts/detect_tv_off.log >/dev/null 2>&1
* * * * * /storage/scripts/detect_tv_off.sh >> /storage/scripts/detect_tv_off.log
So every minute, the script checks whether the receiver responds to ping, if not it calls Kodi to stop playback. This works well for me since when I stop watching TV, I switch off the receiver. Now I don't have to remember to press stop from my remote but within one minute playback will be automatically stopped.
EDIT: Also I found out that there is command line tool
cec-client which could also work to detect if TV is off and then one could run the same JSON RPC command to Kodi to stop playback. But using ping in this case is much more simpler.