Lighting scene & Kodi automation - head scratcher

I have a rule that changes my lighting and amp settings when a movie starts playing in Kodi and the media type is a ‘movie’. The room setup is triggered via a scene.

I have another scene setup that turns the lighting back to a normal setup which is triggered via an automation once Kodi state stops being ‘playing’.

Unfortunately the latter rule triggers whenever any media has stopped playing in Kodi. I.e. not just movies.

Is there a way to monitor what the previouw trigger scene was? Or what the last media_type was that had been playing?

For reference here’s my two rules.

#Movie Lights ON rule

- id: a_lights_scene_movie_playing
  alias: 'Turn on movie scene lighting when playing a movie'
  trigger:
    - platform: state
      entity_id: media_player.osmc
      to: 'playing' #if osmc starts playing
  condition:
    - condition: state
      entity_id: sun.sun
      state: 'below_horizon'
    - condition: template
      value_template: "{{ is_state_attr('media_player.osmc', 'media_content_type', 'movie') }}"
  action:
    - delay:
        seconds: '5'
    - service: scene.turn_on
      entity_id: scene.movie_time

#Movie Lights OFF rule

- id: a_lights_scene_movie_playing_off
  alias: 'Turn OFF movie lights / back to relax scene lighting'
  trigger:
    - platform: state
      entity_id: media_player.osmc #if osmc state changes
  condition:
    - condition: template
      value_template: "{{ states('media_player.osmc') != 'playing' }}"
    - condition: state
      entity_id: sun.sun
      state: 'below_horizon'
    - condition: state
      entity_id: group.living_room_lights_card
      state: 'on'
  action:
    - service: scene.turn_on
      entity_id: scene.relax_lounge
1 Like

Set an input boolean in the first automation, and make it a condition in the second automation (and then switch it back off in the action).

1 Like

:raised_hands: @anon43302295

Is there any way of storing text in a sudo variable too? Ie, if I wanted to make the rule a little more complex and revert to the last activated scene after a movie has finished playing?

Taa-daa…

:smile:

2 Likes

I came up with a very similar automation though I added different results depending on whether it went from play to pause or play to stop. I also used this to detect the media type…

  - condition: template
    value_template: '{{ states.media_player.kodi.attributes.media_content_type == "movie" }}'

…I’m never sure which is technically the best choice for detecting states, just keep trying until I find the one that works in the given situation.

Anyway, I currently use a state from my Harmony hub to decide if it is coming from playing a ‘movie’ but, if you don’t mind sharing, I would be very interested in seeing the code you come up with based on @anon43302295’s suggestion as it would be more versatile for me on this and a few other scenario’s.

As per @anon43302295 suggestion is used an input_boolean as a way of holding a sudo variable state. This isn’t visible in the UI anywhere. Instead the two automations use it to communicate which one has recently triggered.

For clarify here’s the final rules and input boolean. @Bobby_Nobble let me know if you want any additional help.

input_boolean: 
  kodi_movie_scene: #used to track if movie scene was triggered    

#Movie Lights ON rule

- id: a_lights_scene_movie_playing
  alias: 'Turn on movie scene lighting when playing a movie'
  trigger:
    - platform: state
      entity_id: media_player.osmc
      to: 'playing' #if osmc starts playing
  condition:
    - condition: state
      entity_id: sun.sun
      state: 'below_horizon'
    - condition: template
      value_template: "{{ is_state_attr('media_player.osmc', 'media_content_type', 'movie') }}"
  action:
    - delay:
        seconds: '5'
    - service: scene.turn_on
      entity_id: scene.movie_time
    - service: input_boolean.turn_on
      entity_id: input_boolean.kodi_movie_scene

#Movie Lights OFF rule

- id: a_lights_scene_movie_playing_off
  alias: 'Turn OFF movie lights / back to relax scene lighting'
  trigger:
    - platform: state
      entity_id: media_player.osmc #if osmc state changes
  condition:
    - condition: template
      value_template: "{{ states('media_player.osmc') != 'playing' }}"
    - condition: state
      entity_id: sun.sun
      state: 'below_horizon'
    - condition: state
      entity_id: group.living_room_lights_card
      state: 'on'
    - condition: state #check if movie scene is on
      entity_id: input_boolean.kodi_movie_scene
      state: 'on'
  action:
    - service: input_boolean.turn_off
      entity_id: input_boolean.kodi_movie_scene
    - service: scene.turn_on
      entity_id: scene.relax_lounge

@Bobby_Nobble Your value template looks fine to me. The templating section of the HA documentation show’s there’s quite a few different ways to arrive at the same output. Some expressions will return the state object and not the state string. But that should affect this instance. You could have also written it as:

- condition: template
  value_template: "{{ is_state_attr('media_player.kodi', 'media_content_type', 'movie') }}"
1 Like

Many thanks @sentur, that looks good to me, needed an example that I could directly relate to so I could work out what was going on and then apply to my set up.

Will give it a go in the next couple of days and let you know.

Eventually got around to sorting the input_boolean as a couple of tweaks I wanted to make to the automation meant it wouldn’t work the way I used to have it with loads of separate conditions. All working good and much simpler code now PLUS I finally get what input_booleans are all about :blush:

Thanks @sentur and @anon43302295

THB, I don’t know where to really start this learning process… BUT, you guys seem to know your stuff, so, is it possible to somehow tag video files with a ‘lights up time’ field and have automation issue a lights up command when a movie’s credits start? Would appreciate a point in the right direction :slight_smile:

I don’t think this is possible unfortunately. I don’t believe there’s a way to tag a video file in Kodi and then access that tag from the Kodi API.

However it maybe possible to do a simpler version and turn the lights on at a set time interval from the end of the movie. I’d start by checking what data can be scraped about video files from the Kodi API.