Automate light depending on time and Sonos

I want to create my first automation and I want to achieve the following:

If the time is between 6 am and 9 am and a specific Sonos is playing I want a specific lamp to be turned on. During the same period, if that Sonos is not playing then I want the light to turn off.

Where do I start?

BR
Daniel

Thats two automations.

The first:

  • has a trigger (platform: state) for your Sonos media_player being to: 'playing'.
  • You then have some conditions.
    1. The time (condition: time) being after: '05:59:59'
    1. The time being before: '09:01:00'
    1. A template with the value_template being '{{ states.media_player.YOUR_SONOS.attributes.media_title == "This title" }}' (replace YOUR_SONOS and This title as appropriate)
  • Then there will be the action which will use the light.turn_on service (assuming it’s a light entity) on your light

The second will use a trigger of the media player going to: 'paused' and the same first 2 conditions. The action will use light.turn_off instead:

  - alias: 'Music stopped in the morning'
    initial_state: true
    trigger:
      - platform: state
        entity_id: media_player.YOUR_SONOS
        to: 'paused'
    condition:
      - condition: and
        conditions:
          - condition: time
            after: '05:59:59'
          - condition: time
            before: '09:01:00'
    action:
      - service: light.turn_off
        entity_id: light.YOUR_LIGHT

It looks like the previous poster covered it for the most part, just a couple things to add: add a time trigger and a condition of playing to the first automation, that way if your music is already playing your light will come on at that time, and on the second automation, add a time trigger, a media stopped trigger, and a condition of paused or stopped. Let me know if you need me to write out the yaml for it, I can do so from my computer tomorrow.

@Spartan.II.117
I would really appriciate some yaml code to get me started!

- alias: 'Music stopped in the morning'
    initial_state: true
    trigger:
      - platform: state
        entity_id: media_player.YOUR_SONOS
        to: 'paused'
      - platform: state
        entity_id: media_player.YOUR_SONOS
        to: 'idle'
      - platform: time
        at: 6:00:00
      - platform: state
        entity_id: media_player.YOUR_SONOS
        to: 'off 
    condition:
        - condition: time
          after: '05:59:59'
          before: '09:01:00'
        - condition: state
          entity_id: media_player.YOUR SONOS
          state: paused
        - condition: state
          entity_id: media_player.YOUR SONOS
          state: idle
        - condition: state
          entity_id: media_player.YOUR SONOS
          state: off
    action:
      - service: light.turn_off
        entity_id: light.YOUR_LIGHT


- alias: 'Music playing in the morning'
    initial_state: true
    trigger:
      - platform: state
        entity_id: media_player.YOUR_SONOS
        to: 'playing'
      - platform: time
        at: 6:00:00
    condition:
        - condition: time
          after: '05:59:59'
          before: '09:01:00'
        - condition: state
          entity_id: media_player.YOUR SONOS
          state: playing
    action:
      - service: light.turn_on
        entity_id: light.YOUR_LIGHT

Okay, so I have this line in my configuration.yaml:

automation: !include automations.yaml

Then my automations.yaml looks as below. When I have that code in my automations file I am not able to log in to homeassistant. If I remove it there is no problems to login. What can be wrong?

- id: music_stopped_morning
alias: ‘Music stopped in the morning’
initial_state: true
trigger:
- platform: state
entity_id: media_player.Sovrum
to: ‘paused’
- platform: state
entity_id: media_player.Sovrum
to: ‘idle’
- platform: time
at: 6:00:00
- platform: state
entity_id: media_player.Sovrum
to: 'off
condition:
- condition: time
after: ‘05:59:59’
before: ‘11:01:00’
- condition: state
entity_id: media_player.Sovrum
state: paused
- condition: state
entity_id: media_player.Sovrum
state: idle
- condition: state
entity_id: media_player.Sovrum
state: off
action:
- service: light.turn_off
entity_id: light.tradfri_bulb_e14_ws_opal_400lm

  • id: music_playing_morning
    alias: ‘Music playing in the morning’
    initial_state: true
    trigger:
    • platform: state
      entity_id: media_player.Sovrum
      to: ‘playing’
    • platform: time
      at: 6:00:00
      condition:
    • condition: time
      after: ‘05:59:59’
      before: ‘11:01:00’
    • condition: state
      entity_id: media_player.Sovrum
      state: playing
      action:
    • service: light.turn_on
      entity_id: light.tradfri_bulb_e14_ws_opal_400lm

If you can correctly format your paste by selecting all the YAML at once and marking it with </> then we can probably help you.

Knowing the error message you get from the configuration check (hass --script check_config) is pretty important though.

I was looking for how to do a correct paste, thank you! Does it look better now below? I am using the “Check config” function on the configuration tab in the gui and it shows success.

- id: music_stopped_morning
  alias: 'Music stopped in the morning'
  initial_state: true
  trigger:
    - platform: state
      entity_id: media_player.sovrum
      to: 'paused'
    - platform: state
      entity_id: media_player.sovrum
      to: 'idle'
    - platform: time
      at: 6:00:00
    - platform: state
      entity_id: media_player.sovrum
      to: 'off 
  condition:
    - condition: time
      after: '05:59:59'
      before: '11:01:00'
    - condition: state
      entity_id: media_player.sovrum
      state: paused
    - condition: state
      entity_id: media_player.sovrum
      state: idle
    - condition: state
      entity_id: media_player.sovrum
      state: off
  action:
    - service: light.turn_off
      entity_id: light.tradfri_bulb_e14_ws_opal_400lm

- id: music_playing_morning
  alias: 'Music playing in the morning'
  initial_state: true
  trigger:
    - platform: state
      entity_id: media_player.sovrum
      to: 'playing'
    - platform: time
      at: 6:00:00
  condition:
    - condition: time
      after: '05:59:59'
      before: '11:01:00'
    - condition: state
      entity_id: media_player.sovrum
      state: playing
  action:
    - service: light.turn_on
      entity_id: light.tradfri_bulb_e14_ws_opal_400lm

You get no error, but:

When I have that code in my automations file I am not able to log in to homeassistant. If I remove it there is no problems to login.

Can you run the command and provide the output please.

core-ssh:~# hassio homeassistant check
Error on homeassistant/check: starting version 3.2.2
Testing configuration at /config
2017-10-06 11:33:54 ERROR (MainThread) [homeassistant.bootstrap] Unable to setup error log /config/home-assistant.log (access denied)
2017-10-06 11:33:55 ERROR (SyncWorker_0) [homeassistant.util.yaml] while parsing a block mapping
  in "/config/automations.yaml", line 13, column 7
expected <block end>, but found '<scalar>'
  in "/config/automations.yaml", line 18, column 15
2017-10-06 11:33:55 ERROR (MainThread) [homeassistant.bootstrap] Error loading /config/configuration.yaml: while parsing a block mapping
  in "/config/automations.yaml", line 13, column 7
expected <block end>, but found '<scalar>'
  in "/config/automations.yaml", line 18, column 15

Realising that the usage of ‘’ in the yaml file isnt that consistent. Where and when shall i put ‘’ around a value?

What’s on those lines?

The error was that i wasnt using ‘’ around values for states and times but now it is working - almost. My automation to turn on the light if the music is turned on works. However the lights doesnt turn off when i pause the music. So it seems like the first automation isnt triggered correctly.

- id: music_stopped_morning
  alias: 'Music stopped in the morning'
  initial_state: true
  trigger:
    - platform: state
      entity_id: media_player.sovrum
      to: 'paused'
    - platform: state
      entity_id: media_player.sovrum
      to: 'idle'
    - platform: time
      at: '6:00:00'
    - platform: state
      entity_id: media_player.sovrum
      to: 'off' 
  condition:
    - condition: time
      after: '05:59:59'
      before: '13:01:00'
    - condition: state
      entity_id: media_player.sovrum
      state: 'paused'
    - condition: state
      entity_id: media_player.sovrum
      state: 'idle'
    - condition: state
      entity_id: media_player.sovrum
      state: 'off'
  action:
    - service: light.turn_off
      entity_id: light.tradfri_bulb_e14_ws_opal_400lm

- id: music_playing_morning
  alias: 'Music playing in the morning'
  initial_state: true
  trigger:
    - platform: state
      entity_id: media_player.sovrum
      to: 'playing'
    - platform: time
      at: '6:00:00'
  condition:
    - condition: time
      after: '05:59:59'
      before: '12:01:00'
    - condition: state
      entity_id: media_player.sovrum
      state: 'playing'
  action:
    - service: light.turn_on
      entity_id: light.tradfri_bulb_e14_ws_opal_400lm

Triggers are or by default, conditions are and by default.

Your conditions require the media player to be paused and idle and off :wink:

I need a bi-polar media player then… :smile:

Thank you, works good now