Turn on and off TV if no signal from kodi

Hi guys. This is what I have. Raspberry Pi 4 running libreelec (kodi) with home assistant in a docker.

the RPI is connected to a dumb tv hia hdmi for KODI.
I also have a chromecast connected to the tv via another HDMI port.
I then wired the TV power cable through a sonoff switch to be able to control it (on off only).
When not in use for 15 minutes, kodi will cut off power to hdmi (power saving mode) which will make the screen go black with backlight on.

What I want to do is,

  1. Turn off the tv if
    condition a. kodi is in power saving mode (hdmi turned off)
    b. chromecast is not playing anything.

  2. turn on the tv if
    condition a. kodi exits power saving mode
    b. chromecast starts playing.

I think i know what to do with respect to the chromecast. How do I get HA to detect when kodi enters power saving mode? my TV has got no CEC function.

Saw this guide - https://www.home-assistant.io/integrations/kodi/ but appears to be for cec supported tv only.

I don’t know if you can detect the HDMI power saving mode. But perhaps the Kodi state ‘idle’ for 15 minutes will serve about the same purpose?

Hi. Kodi is in idle state when browsing. Only when a media is playing it changes state. This could result in tv turning off while some one is looking for a movie

You are right. But no one is responding to your literal request. I think you should start thinking of different approaches.

Its on of those things that would be nice to have. If not I’ll just have to live with it until i buy a newer tv :slight_smile:

Solution. Pinned here for future reference and if anyone else wants to use it.

  1. Set up SSH from HA to libreelec

Guide modified from

https://dovydas.xyz/homeassistant/hyperion/ssh/hassio/2018/01/22/controlling-anything-via-ssh.html

1.1 MASTER:

Connect to the MASTER. This will not work “out of the box” so first follow tutorial on how to connect to the HA to Libreelec

ssh [email protected]

Now attach to the Homeassistant docker container. List available docker containers.

docker ps -a

find one looking something like homeassistant/homeassistant3 . Copy its CONTAINER ID looking something like b7dfc2f4d0c4 . Then attach to your container

docker exec -it b7dfc2f4d0c4 /bin/bash

now finally generate your SSH key, but this time in a different directory

mkdir /config/ssh
ssh-keygen -t rsa -f /config/ssh/id_rsa  

lets checkout our two brand new PUBLIC & PRIVATE keys

cd /config/ssh
ls -al
cat id_rsa.pub

if all went well you public key output should look something like this:

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDyiprxeHAAieq2YtiXhFgSQIhZwvY6zsPAhsNU/N6yJ+JptVJGWBNY0tAD4eQiSsl88Qe4ryWVmtnw83jUjDMZp24uRtEAPnPW3f9N8mbDnyCEtbYhIDn1KseL3SuRWyFzk0fcMExZfsXrxgZ5nD/yQKvjcHm52LrhDfauxYADItonBZA+6mXh0E1LBrk6gP884IpLLbT9xetW2ZLP6htJDTPc2k9qN1cRVj3DD5Ppfyct1FmfZcAyi3Ua2dPxzngI5RUsjLBaqP+3lluc7fJVYK7fhnGZ36E/JNEamlzktBuLG1+1G3wxCshMFFBBuLTHb7qhtueIBY/4+wduJlFD root@libreelec

COPY this value it is your PUBLIC key we will use later!

1.2 SLAVE:

ssh [email protected]
ssh-keygen -t rsa

Okay we have our keys setup but what about passwordless connection from MASTER? Its easy we have to add the the public key we copied before to our SLAVE machines’ authorized_keys file.

echo "PASTE YOUR MASTER KEY INSTEAD OF THIS TEXT" >> ~/.ssh/authorized_keys

On some systems authorized_keys file must have specific unix permissions set and it will fail silently if you do not add these permissions so lets do that. Line below will only make the file readable and writable to our user

chmod 600 ~/.ssh/authorized_keys

Okay our connection should be good to go. So what we just did is we created a SSH key pair on both MASTER and the SLAVE machines & we installed MASTERS public key onto SLAVE. Now the SLAVE trusts the MASTER machine and allows it to connect without a password.

2. Testing SSH connection MASTER → SLAVE

lets connect to our MASTER machine again

ssh [email protected]

then lets issue a command to our SLAVE

docker exec -it b7dfc2f4d0c4 /bin/bash
ssh -i /config/ssh/id_rsa -o StrictHostKeyChecking=no root@libreelec 'vcgencmd display_power'
    1. Set up command line sensor using this guide : https://www.home-assistant.io/integrations/sensor.command_line/

in configuration,yaml , add

sensor:
  - platform: command_line
    command: ssh -i /config/ssh/id_rsa -o StrictHostKeyChecking=no [email protected] 'vcgencmd display_power'
    name: Kodi Sleep Status