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

I’ve had mine working for quite some time, also implemented the same for TV shows.

Here is my stop automation.

alias: KODI Home Theater Movie Ended
trigger:
  - entity_id: media_player.kodi
    platform: state
    to: idle
condition:
  - condition: state
    entity_id: input_text.kodi_media_content_type
    state: movie
action:
  - data:
      entity_id: light.diningroomlamp
    service: light.turn_on
  - data:
      entity_id: light.livingroomlamp
    service: light.turn_on
  - data:
      entity_id: input_text.kodi_media_content_type
      value: ''
    service: input_text.set_value
mode: single

Thank you so much. I’ll try it out tonight.

Anyone know how this would be setup using the GUI automations interface?

I’m trying to like the ‘new’ way of doing things, but some things are just not intuative, the automations setup is one of them.

There are many automations in this thread. Which one do you want guidance on?

I was trying to setup the first one in the post. I haven’t been able to figure out even how to get light to dim when Kodi plays a movie or a TV show.

Typing this while doing it, so forgive me if I don’t even finish this post (my entities will be different to Grayson’s)

  1. Configuration|Autmations|Add Automation|Start with an empty automation

  2. Fill in a name and description

  3. Scroll down to trigger and choose state, then start typing entity_id. As you type (It’ll start with media_player) the choices will narrow until you can click on an entity you want in the automation. Mine is media_player.kodibedroom

  4. In the To: field type playing

  5. Click Add Condition, then under Type choose template.

  6. In the Value Template field type {{ states.media_player.kodibedroom.attributes.media_content_type == "movie" }}

  7. Under Action choose Call Service

  8. In service type or choose light_turnoff

  9. Choose the area/device/entity you want to turn off.

  10. Click Save (bottom right)

Actually I have to say that doesn’t work on my system. The media player entity state switches to playing moments before media_content_type is set to movie, resulting in the condition failing. At least I think that is what is happening.

EDIT: more correctly media_content_type changes to video then to movie. Fixed by adding

for: '1'

to the trigger, which I also did in the UI.

I can’t post all 3 of my automations if you like? Your best bet if you’re not confident with Yaml would be to paste the YAML code and then switch to GUI mode. You’ll also need to add the input text into your config as well.

There is more than one way to skin a cat

here is how I skin this cat

#=======================================================================
#
#=======================================================================
- id: 95f0a44f-da18-4462-9239-67e0287bc6ea
  alias: Watch TV in Movie MODE
  trigger:
    - platform: state
      entity_id: media_player.tv_65
      to: "playing"
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: sensor.day_night
        state: "Night"
      - condition: state
        entity_id: light.shelly_lounge
        state: "on"
  action:
    - data_template:
        entity_id: script.movie_on
      service: script.turn_on
    - data_template:
        entity_id: input_boolean.watching_movie
      service: input_boolean.turn_on

      
- id: 137821a1-af9c-4198-b4c3-cf87773a846c
  alias: Watching movie idle - off
  trigger:
    - platform: state
      entity_id: media_player.tv_65
      to: idle
    - platform: state
      entity_id: media_player.tv_65
      to: "off"
    - platform: state
      entity_id: media_player.tv_65
      to: "paused"
  action:
    - data_template:
        entity_id: script.movie_off
      service: script.turn_on
    - data_template:
        entity_id: input_boolean.watching_movie
      service: input_boolean.turn_off
#=======================================================================

and the scripts

movie_on:
  sequence:
  - service: light.turn_on
    data:
      entity_id: light.shelly_lounge
      brightness_pct: 40
movie_off:
  sequence:
  - choose:
    - conditions:
      - condition: state
        entity_id: light.shelly_lounge
        state: 'on'
      sequence:
      - service: light.turn_on
        data:
          entity_id: light.shelly_lounge
          brightness_pct: 100

so when I watch a movie on the Chromecast Playing and its Night and the lounge lights are on dim the lights to 40%

then if I paused, Idle or off Chromecast lights go back to 100%

Thanks, that helped me sort some things out

Thanks I’d like to see what you are doing.
As for YAML, I’m quite comfortable with it, HA seems to be pushing the GUI interface a lot so I figured why not learn the GUI as I build a new HA instance.

And, I just figured out that if I hit the three dot menu for an ‘entry’ there is an option to edit in YAML… I’ll show myself out now :man_facepalming:

1 Like

Make sure you add the below to your configuration.yaml, if you want to implement for tv shows as well change the media content type from movie to tvshow

input_text:
  kodi_media_content_type:
    name: KODI Media Content Type

Movie Playing

alias: KODI Home Theater Movie Playing
trigger:
  - entity_id: media_player.kodi
    platform: state
    to: playing
condition:
  - condition: state
    entity_id: sun.sun
    state: below_horizon
action:
  - delay: '00:00:01'
  - condition: template
    value_template: '{{ is_state_attr(''media_player.kodi'', ''media_content_type'', ''movie'') }}'
  - data:
      entity_id: light.diningroomlamp
    service: light.turn_off
  - data:
      entity_id: light.livingroomlamp
    service: light.turn_off
  - service: input_text.set_value
    data_template:
      entity_id: input_text.kodi_media_content_type
      value: '{{ states.media_player.kodi.attributes.media_content_type }}'
mode: single

Movie Pause / Play

alias: KODI Home Theater Movie Paused
trigger:
  - entity_id: media_player.kodi
    from: playing
    platform: state
    to: paused
condition:
  - condition: template
    value_template: '{{ states.media_player.kodi.attributes.media_content_type == "movie" }}'
  - condition: and
    conditions:
      - condition: state
        entity_id: sun.sun
        state: below_horizon
action:
  - data:
      entity_id: light.diningroomlamp
    service: light.turn_on
  - data:
      entity_id: light.livingroomlamp
    service: light.turn_on
mode: single

Movie Stopped

alias: KODI Home Theater Movie Ended
trigger:
  - entity_id: media_player.kodi
    platform: state
    to: idle
condition:
  - condition: state
    entity_id: input_text.kodi_media_content_type
    state: movie
action:
  - data:
      entity_id: light.diningroomlamp
    service: light.turn_on
  - data:
      entity_id: light.livingroomlamp
    service: light.turn_on
  - data:
      entity_id: input_text.kodi_media_content_type
      value: ''
    service: input_text.set_value
mode: single

1 Like

Hi there. Thank you for your great code. I saw this and ordered a new Synology DS to run docker and HA make this happen to my homecinema. So… as you will guess… I’m a total newbie :wink:

I have a problem with the content type detection. When it’s enabled the automation isn’t working. If I disable the template condition, it works. I’ve added

input_text:
  kodi_media_content_type:
    name: KODI Media Content Type`Preformatted text`

to the top of my configuration.yaml in the Home Assistant root-folder and rebootet the HA-server but still no function. I’m using Kodi 20.1.

Here’s my yaml:

alias: KODI Home Theater Movie Playing
trigger:
  - entity_id: media_player.kodi_kino
    platform: state
    to: playing
condition: []
action:
  - delay:
      hours: 0
      minutes: 0
      seconds: 2
      milliseconds: 0
  - condition: template
    value_template: "{{ is_state_attr('media_player.kodi', 'media_content_type', 'movie') }}"
    enabled: true
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.3gang_switch_switch_1
  - service: input_text.set_value
    data_template:
      entity_id: input_text.kodi_media_content_type
      value: "{{ states.media_player.kodi.attributes.media_content_type }}"
mode: single

Thank you in advance

What is the ‘Preformatted text’ you have on the end of Kodi Media Content Type?

Any errors within the logs that might help?

Thanks for your fast response. That was an copy-error, but I still fixed it. Looks like this now:

# Loads default set of integrations. Do not remove.


default_config:

# Load frontend themes from the themes folder
frontend:
  themes: !include_dir_merge_named themes

# Text to speech
tts:
  - platform: google_translate

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

input_text:
  kodi_media_content_type:
    name: KODI Media Content Type

Now it’s working but it also does when I run tvshows or music.

I’d have a look in the log book and also developer tools to make sure Kodi is passing the media type over to the input_text

It’s working now. The problem was that I haven’t named my Kodi right. Thank you again.

Continuing the discussion from How To Automate Lights When Playing Movie in Kodi Based On Content Type?:

I know this is a extremely old post. I’m really new to the HA world. I’ve been trying to get this to work properly for days. Hopefully someone will chime in. Whatever I need to upload please let me know anything would be appreciated.

What is the automation you have tried, post the yaml and tell us what you are observing.

Sorry for the late reply. Soon as I get to my computer I’ll copy the yaml code I’ve using