The Home Assistant Kodi Integration is nice, but as of writing it becomes slower the longer Home Assistant runs. This sucks, especially when automations (like turning on a power hungry receiver) don’t run because of it.

This is a hack around that desynchronization, allowing much faster updates with a new Now Playing state. Usually under a second.

You will need:

Put those into this python script, and you’re off to the races:

import requests, time


counter = 0
state = 0
head = {'Authorization': 'Bearer ' + HOME_ASSISTANT_LONG_LIVED_API_KEY}

while True:
		r = requests.get(KODI_URL+'/jsonrpc?request=%7B%22jsonrpc%22%3A%20%222.0%22%2C%20%22method%22%3A%20%22Player.GetActivePlayers%22%2C%20%22id%22%3A%201%7D')
		newState = len(r.json()['result'])

		if newState is not state or counter is 30:
			state = newState
			counter = 0
			r = + "/api/states/binary_sensor.kodi_is_playing", json={"state": "on" if state is not 0 else "off", "attributes": { "friendly_name": "Kodi is Playing" }}, headers=head)

		counter += 1

	except Exception as e: