Dim lights when playing media playing works once

Hello everyone! I’m having some issues regarding dim lights when media is playing. I got it working but for some reason it only dims the lights the first time I play the media. If I pause or stop nothing happens, if play again nothing happens. I’m quite new to home assistant and not really sure why it’s acting this way. I followed the guide at https://home-assistant.io/cookbook/dim_lights_when_playing_media/ , the scenes work fine when manually turning on and off and home assistant can tell when kodi is playing or not since it show on the dashboard. A little confused here to be honest and maybe i missed something really stupid but any help or tips are greatly appreciated. Thanks!

configuration.yaml

scene:
  - name: Livingroom normal
    entities:
        light.grupp_1:
            state: on
            transition: 2
            brightness_pct: 85
        light.grupp_2:
            state: on
            transition: 2
            brightness_pct: 85
  - name: Livingroom dim
    entities:
        light.grupp_1:
            state: on
            transition: 2
            brightness_pct: 20
        light.grupp_2:
            state: on
            transition: 2
            brightness_pct: 20

automations.yaml

  - alias: "Media player paused/stopped"
    trigger:
      - platform: state
        entity_id: media_player.kodi
        from: 'playing'
        to: 'idle'
    condition:
      - condition: state
        entity_id: sun.sun
        state: 'below_horizon'
    action:
        service: scene.turn_on
        entity_id: scene.livingroom_normal

  - alias: "Media player playing"
    trigger:
      - platform: state
        entity_id: media_player.kodi
        to: 'playing'
        from: 'idle'
    condition:
      - condition: state
        entity_id: sun.sun
        state: 'below_horizon'
    action:
        service: scene.turn_on
        entity_id: scene.livingroom_dim

After some further testing it seems that it does work when i stop kodi, but not if i pause even though the dashboard show the media being paused as mentioned in the previous post.

Maybe it does not go to ‘idle’ when you pause? You can check in Developer Tools (the <> icon) what the state actually is called.

You can also just remove the to/from ‘idle’ lines so the automations execute when going to/from ‘playing’ no matter what the other state is called.

Oh, and obviously the automations only work when the Sun is below horizon :slight_smile:

1 Like

It won’t do because the paused state is 'paused.

Good idea to watch the States page so you can see what really happens when planning automations.

1 Like

You are absolutely correct this was the issue and now it works like a charm. Thank you for the tip, I really appreciate it!

1 Like

This was the exact issue, it was going to “paused” state and not idle I guess a got confused because of the example saying “Stopped/Paused”. That is also a really good tip to ignore the to/from ‘idle’. I got it working now thanks to you tip now I just have to make it work more smooth. Yeah I noticed the sun below horizon feature which is great, however this time of year where I live the Sun is usually below the horizon most of the day :smiley: Thanks again for you help.

Hi, i was wondering if its possible to do this with VLC player on my desktop?

The best way to avoid any issues is to do what I’ve done. You don’t need a “from:” state, just a “to:”:

- alias: Chromecast Playing
  initial_state: 'off'
  trigger:
    - platform: state
      entity_id: media_player.living_room
      to: 'playing'
  condition:
    - condition: state
      entity_id: light.living
      state: 'on'
  action:
    - service: light.turn_on
      data:
        entity_id: light.living
        brightness_pct: '40'
        transition: '3'

- alias: Chromecast Paused
  initial_state: 'off'
  trigger:
    - platform: state
      entity_id: media_player.living_room
      from: 'playing'
  condition:
    - condition: state
      entity_id: light.living
      state: 'on'
  action:
    - service: light.turn_on
      data:
        entity_id: light.living
        brightness_pct: '100'
        transition: '3'