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.

6 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.

1 Like

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

1 Like

Hi @GraysonPeddie… Sorry to revive an older thread.

Is the correct behavior, that commencing to watch a movie doesn’t turn the light off until after I pause?

e.g If my lights are on and I start watching a movie, they don’t change at all. Pausing the movie dims them, then playing again turns them off and everything else works correctly.

I’m fine with reviving my old thread.

Watching the movie should turn off the light as detailed in my first post in my thread.

Strange… I still can’t get it to work (I’m kinda new to this)

You can see where I first play the movie (idle to playing) and for some reason it doesn’t trigger the automation, but when resuming after a pause it works exactly as it should?

Any ideas?

This is my YAML if it helps - Have I made a mess of something in it?

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

I do not have the floor lamps with me to test the setup again, but it did work for me as of last year.

Sorry I couldn’t have been any help right now.

No worries - Thanks anyway.

Got it solved!.

It looks like the automation was too quick for Kodi to have broadcast the media type. Moving the conditions into the action and adding a slight delay solved the issue:

I also added a check for night time into the conditions and am using a different iteration to get the state, but the original also works

# Automations for Kodi playing movies
- id: '1560018463289'
  alias: KODI Home Theater Movie Playing
  trigger:
  - entity_id: media_player.kodi_lounge
    platform: state
    to: playing
  action:
  - delay:
      seconds: '1'
  - condition: state
    entity_id: sun.sun
    state: 'below_horizon'
  - condition: template
    value_template: "{{ is_state_attr('media_player.kodi_lounge', 'media_content_type', 'movie') }}"
  - data:
      entity_id: light.lounge_lamp
    service: light.turn_off
  - service: input_text.set_value
    data_template:
      entity_id: input_text.kodi_media_content_type
      value: '{{ states.media_player.kodi_lounge.attributes.media_content_type }}'
2 Likes

I have never seen documentation that the condition header can be inside an action. Interesting.

Glad you got it to work.

I implemented an automation based off of your post that randomly stopped working for me a couple months ago. I traced it down to needing to change the syntax a little.

Old approach (that no longer works):

'{{ states.media_player.kodi_home_theater.attributes.media_content_type
      == "movie" }}'

New approach (works now):

{{ state_attr('media_player.kodi_home_theater', 'media_content_type') == "movie" }}

Posting this here in case it might help others with the same issue.

1 Like

Has anybody managed to get the stop behaviour working? I can get it turn off the lights when playing, lights on when paused, but can’t get the lights on at stop working.

- 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.bf02f6d15ebe9ea5a4m0no
    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.bf02f6d15ebe9ea5a4m0no
    service: light.turn_on

Once the movie has stopped there is no longer a media_content_type

Is that not the idea of the input text to know that a movie was playing?

I just copied the code from previous posts.

The property “old_state.attributes.media_content_type” of your kodi entity has to be set to movie because as @nickrout is saying once you are stopping, there is no longer a media_content_type.
The idle state is right.

I cannot seem to get the movie ending automation to work either. When I check Home Assistant Step Details it fails with this error:
Error: In ‘state’: In ‘state’ condition: unknown entity input_text.kodi_media_content_type

Not sure how to fix it.
Any help would be greatly apprecitated.
Robert