How To Automate Lights When Playing Movie in Kodi Based On Content Type?

Let’s say you want to dim or turn off the lights in your living room when watching the movie in Kodi. You looked at the states page in Home Assistant and discovered that there’s a media_content_type for media_player.kodi_home_theater. Because the media_content_type is an attribute, you setup automation as follows:

- id: '1560018463289'
  alias: KODI Home Theater Movie Playing
  trigger:
  - entity_id: media_player.kodi_home_theater
    platform: state
    to: playing
  condition:
  - condition: template
    value_template: '{{ states.media_player.kodi_home_theater.attributes.media_content_type
      == "movie" }}'
  action:
  - data:
      entity_id: light.lr_flamps
    service: light.turn_off
- id: '1560018618332'
  alias: KODI Home Theater Movie Ended
  trigger:
  - entity_id: media_player.kodi_home_theater
    to: idle
    platform: state
  condition:
  - condition: template
    value_template: '{{ states.media_player.kodi_home_theater.attributes.media_content_type
      == "movie" }}'
  action:
  - data:
      brightness: '96'
      entity_id: light.lr_flamps
    service: light.turn_on
  - delay: '300'
  - data:
      brightness: '64'
      entity_id: light.lr_flamps
    service: light.turn_on
- id: '1560018800221'
  alias: KODI Home Theater Movie Paused
  trigger:
  - entity_id: media_player.kodi_home_theater
    from: playing
    platform: state
    to: paused
  condition:
  - condition: template
    value_template: '{{ states.media_player.kodi_home_theater.attributes.media_content_type
      == "movie" }}'
  action:
  - data:
      brightness: '48'
      entity_id: light.lr_flamps
    service: light.turn_on

You applied changes to automation.yaml and reloaded the automations within the General page. Your living room lights are on, so as soon as you play the movie, You pause the movie and the lights turn on at a dimmed setting. You resumed the movie and the lights turned off. You then stop the movie and although the movie stopped and you are back to the movie selection screen, the lights do not turn on.

“Why is that,” you asked?

That’s because as soon as you stop the movie, the media_content_type disappears from the state page in Home Assistant before the trigger happens. Well, it’s as though the condition is never met once the trigger state is set to “idle.” The automation for “KODI Home Theater Movie Ended” does not fire!

“What can I do to make it work,” you asked?

input_text to the rescue!

input_text:
  kodi_media_content_type:
    name: KODI Media Content Type

Here’s the new automation.yaml code:

- id: '1560018463289'
  alias: KODI Home Theater Movie Playing
  trigger:
  - entity_id: media_player.kodi_home_theater
    platform: state
    to: playing
  condition:
  - condition: template
    value_template: '{{ states.media_player.kodi_home_theater.attributes.media_content_type
      == "movie" }}'
  action:
  - data:
      entity_id: light.lr_flamps
    service: light.turn_off
  - service: input_text.set_value
    data_template:
      entity_id: input_text.kodi_media_content_type
      value: '{{ states.media_player.kodi_home_theater.attributes.media_content_type
        }}'
- id: '1560018618332'
  alias: KODI Home Theater Movie Ended
  trigger:
  - entity_id: media_player.kodi_home_theater
    platform: state
    to: idle
  condition:
  - condition: state
    entity_id: input_text.kodi_media_content_type
    state: movie
  action:
  - data:
      brightness: '96'
      entity_id: light.lr_flamps
    service: light.turn_on
  - data:
      entity_id: input_text.kodi_media_content_type
      value: ''
    service: input_text.set_value
  - delay: '300'
  - data:
      brightness: '64'
      entity_id: light.lr_flamps
    service: light.turn_on
- id: '1560018800221'
  alias: KODI Home Theater Movie Paused
  trigger:
  - entity_id: media_player.kodi_home_theater
    from: playing
    platform: state
    to: paused
  condition:
  - condition: template
    value_template: '{{ states.media_player.kodi_home_theater.attributes.media_content_type
      == "movie" }}'
  action:
  - data:
      brightness: '48'
      entity_id: light.lr_flamps
    service: light.turn_on

Once the automations are reloaded again, this time the input_text will be updated when the media is playing and the type of content is movie. In other words, when you watch a movie, input_text.kodi_media_content_type is changed to reflect the media content type.

So, you decided to watch the movie again. You paused the movie to make sure it works, resumed the movie, and after a few minutes, you decided to stop the movie and the light begins to turn on. So, all is well.

(Side note: The reason why I chose low brightness settings is because I have a family member who’s asleep and my family’s leaving for work, but that is besides the point as I want to talk about a workaround for Kodi.)

**Update:** Don’t forget to enable remote control via HTTP and allow remote control from this/other systems. You can find the setting by going to Settings, Services, and Control. Change the default username and password to something strong. Although optional, might as well change the port number from 8080 to something obscure, such as 65431. I would always choose something random just for sake of security through obscurity. It’s to keep honest users out but won’t stop determined attackers when in the local network.

3 Likes

G’Day GraysonPeddie,
this is pretty much what I want to achieve but having almost zero experience in coding I am somewhat at a loss to know where to start.
I have searched for days trying to get a handle on HA but as of now it feels like my head is gunna explode!
I have lights setup in HA and I can control them, but how to create the automation has proven quite the task.
Wondering if you can spare the time to advise and 'ol dog what the first (and last) steps are to make this happen.
Cheers!!

I will get back to you tonight once I’m back home from work.

See the top of the page? Click the docs link and then read the automation section of the docs.

Thanks mate…I’d appreciate that!

I’ve always used an input_boolean as the persistent movie flag but same principle.

Thanks so much, works a treat! Added nicely to the atmospherics of the film we watched last night! :slight_smile: