Automatic scene switch when playing media [extended]

Hi there,

I want to share my first project. I took the example from the home assistant page and modified it to that what I want.

Here is what my configuration does:

  • automatic change scene when playing media
  • go to “normal” scene when pause or stopping movie
  • toggle that automatic scene mode e.g. when you watch something besides and you need the normal light mode
  • add and switch between more than one scene, e.g. football scene, romantic scene, action scene…

Here is my config for that:

Switch

platform: template
switches:
  wozi_moviemode2:
    value_template: "{{ is_state('automation.wozi_moviemode2_turn_on', 'on') }}"
    friendly_name: 'Moviemode II'
    turn_on:
      service: automation.turn_on
      entity_id: 
        -  automation.wozi_moviemode2_turn_on
        -  automation.wozi_moviemode2_turn_off
    turn_off:
      - service: automation.turn_off
        entity_id: 
          -  automation.wozi_moviemode2_turn_on
          -  automation.wozi_moviemode2_turn_off
      - service: scene.turn_on
        entity_id: 
          -  scene.wozi_normal 

Turn-On Automation

alias: "wozi_moviemode2_turn_on"
trigger:
 - platform: state
   entity_id: 
     -  media_player.wozi
     -  media_player.amazon_fire_tv
   state: 'playing'
 - platform: homeassistant
   event: start
 - platform: state
   entity_id: 
     -  light.puelight
   from: 'unavailable'
   to: 'on'
 - platform: state
   entity_id: 
     -  light.puelight
   from: 'off'
   to: 'on' 
 - platform: state
   entity_id: 
     -  input_select.wozi_movie_mode_scenes
condition:
 - condition: state
   entity_id: switch.wozi_moviemode2
   state: 'on'
 - condition: or
   conditions:
     - condition: state
       entity_id: media_player.wozi
       state: 'playing'     
     - condition: state
       entity_id: media_player.amazon_fire_tv
       state: 'playing'
action:
    service: script.turn_on
    entity_id: script.wozi_movie_script

Turn-Off Automation

alias: "wozi_moviemode2_turn_off"
trigger:
 - platform: state
   entity_id: 
     -  media_player.wozi
     -  media_player.amazon_fire_tv
   from: 'playing'
   to: 'off'
 - platform: state
   entity_id: 
     -  media_player.wozi
     -  media_player.amazon_fire_tv
   from: 'playing'
   to: 'paused'  
 - platform: state
   entity_id: 
     -  media_player.wozi
     -  media_player.amazon_fire_tv
   from: 'playing'
   to: 'idle'
 - platform: state
   entity_id: 
     -  media_player.wozi
     -  media_player.amazon_fire_tv
   from: 'playing'
   to: 'standby'
action:
    service: scene.turn_on
    entity_id: scene.wozi_normal

Input-select

wozi_movie_mode_scenes:
  name: 'Genre:'
  options:
    - Standard
    - Fußball
    - Romantik
  initial: Standard

Scene football

name: wozi_fuba
entities:
  light.puelight:
    state: on
    transition: 2
    brightness: 255
    rgb_color: [100,180,0]

Scene movie

name: wozi_movie
entities:
    light.puelight:
      state: on
      transition: 2
      brightness: 20
      rgb_color: [0,80,180]       

Scene normal

name: wozi_normal
entities:
  light.puelight:
    state: on
    transition: 2
    brightness: 154
    color_temp: 265
    rgb_color: [255,68,0]

Scene romantic

name: wozi_romantic
entities:
    light.puelight:
      state: on
      transition: 2
      brightness: 20
      rgb_color: [255,30,110]  

Customize

switch.wozi_moviemode2:
  icon: mdi:movie
  friendly_name: Lichtautomatik   

Group for the card

wozi_movie_automatic:
    name: Filmautomatik
    control: shown
    entities:
      - switch.wozi_moviemode2
      - input_select.wozi_movie_mode_scenes   

script

 wozi_movie_script:
  alias: Set scene for wozi
  sequence:
    -  service: scene.turn_on
       data_template:
         entity_id: >
          {% if is_state("input_select.wozi_movie_mode_scenes", "Standard") %} scene.wozi_movie
          {% elif is_state("input_select.wozi_movie_mode_scenes", "Fußball") %} scene.wozi_fuba
          {% elif is_state("input_select.wozi_movie_mode_scenes", "Romantik") %} scene.wozi_romantic
          {% endif %} 

This is what it looks like in HA:

3 Likes

Nice.

I have a similar setup but sometimes it also triggers when playing Youtube for example.

- alias: 'Dim lights when playing Netflix'
  trigger:
    platform: state
    entity_id: media_player.living_room
    state: 'playing'
  condition:  
    condition: and
    conditions:
    - condition: sun
      after: sunset
      after_offset: "-0:20:00"
    - condition: template
      value_template: '{{  states.media_player.living_room.attributes.app_name == Netflix  }}' 
  action:
    service: scene.turn_on
    entity_id: scene.netflix_and_chill
- alias: 'Brighten lights when stop playing Netflix in Living Room'
  trigger:
    -  platform: state
       entity_id: media_player.living_room
       from: 'playing'
       to: 'off'
    -  platform: state
       entity_id: media_player.living_room
       from: 'playing'
       to: 'idle'
    -  platform: state
       entity_id: media_player.living_room
       from: 'playing'
       to: 'paused'
  condition:  
    condition: sun
    after: sunset
    after_offset: "-0:20:00"
  action:
    service: scene.turn_on
    entity_id: scene.evening

EDIT: now I see that I missed the condition on the 2nd Automation :kissing_closed_eyes:

2 Likes

Ah ok! It seems that I only need the state “playing” in my trigger. Then I can save some code lines, can’t I?

Yes you can but I just changed it to:

  trigger:
    platform: state
    entity_id: media_player.living_room
    to: 'playing'

I want to check if this keeps it from triggering the automation over and over again when it’s playing. Will let you know how it works out.

1 Like

I will try it my girlfriend stops watching tv. :joy: when it works I edit the initial post.

If someone want to use that its maybe better to insert a condition if the light is turned on or check the sun state like you did. In my case I like it oldschool: I turn my lights on and off manually. :slight_smile:

1 Like

Sometimes that is just way easier than having to take out your phone or something else. Fortunately I have a Google Home so I can use my voice for that but still a simple switch in most cases is the best solution.