Stop playback when tv is off and resume when tv is on?

I have NAS+TVHeadend+DVB-C tuner, TV (CEC)+RPi2 OSMC+tvheadend client.
When I turn off the TV, KODI client continues to playback a channel or movie. How to do that after turning TV off playback was stopped and resumed after switching TV on ?
Thanks in advance.

Did you try using the Keymaps to make it so hitting the power button on the TV remote causes Kodi to play/pause? This will require CEC compatibility. I’m not certain if the TV would send the command though CEC while powering on/off, but it’s the best solution I can think of!

More info on keymaps here:
You can make your own keymap files manually (hard work) or you can use the keymap addon.

Edit: Alternatively, if the TV won’t send CEC commands during power on/off, you can set a useless button on the remote (like one of them red/green/yellow/blue buttons) to be the play/pause command. Perhaps this works better, because you can use one button for “stop” and another for “open window tvheadend” instead of only pausing.

My TV when turned off/on it sends this (from cec-cleint -s) :

TRAFFIC: [ 12331] >> 0f:36
DEBUG: [ 12331] >> TV (0) -> Broadcast (F): standby (36)
DEBUG: [ 12331] TV (0): power status changed from ‘on’ to ‘standby’
TRAFFIC: [ 35364] >> 0f:84:00:00:00
DEBUG: [ 35364] >> TV (0) -> Broadcast (F): report physical address (84)
DEBUG: [ 35364] << Recorder 1 (1) -> broadcast (F): physical adddress 1000
TRAFFIC: [ 35365] << 1f:84:10:00:01
TRAFFIC: [ 35675] >> 0f:87:00:90:3e
DEBUG: [ 35676] >> TV (0) -> Broadcast (F): device vendor id (87)
DEBUG: [ 35676] TV (0): power status changed from ‘standby’ to ‘on’
DEBUG: [ 35676] << Recorder 1 (1) -> Broadcast (F): vendor id Philips (903e)

Сan I somehow intercept this command and use it in the bash script?

Why don’t you just use the built in CEC options that do what you need without any scripting ?

Got to Settings->System->Input Devices->Peripherals->CEC Adaptor

Enable the option Pause playback when switching to another Source - this will also pause playback when switching the TV off.

It’s dosnt work for LiveTV.