Condtition in action EDIT: Different actions in one automation

Can’t make this work.
I want it to stop all media and turn off TV if it is on when i turn my alarm on.

 - alias: Larm på Media av
   trigger:
     platform: state
     entity_id: alarm_control_panel.alarm_1
     state: armed_away

   action:
     - service: media_player.media_stop
     - condition:
         condition: state
         entity_id: media_player.sony_bravia_tv
         state: 'on'
     - service: media_player.turn_off
       entity_id: media_player.sony_bravia_tv

Get this in the log.

17-01-30 23:35:55 homeassistant.bootstrap: Invalid config for [automation]: [condition] is an invalid option for [automation]. Check: automation->action->1->condition. (See /home/homeassistant/.homeassistant/automation.yaml:0). Please check the docs at https://home-assistant.io/components/automation/

I think the condition needs to be on its own, like this:

- alias: Larm på Media av
   trigger:
     platform: state
     entity_id: alarm_control_panel.alarm_1
     state: armed_away
   condition:
     condition: state
     entity_id: media_player.sony_bravia_tv
     state: 'on'
   action:
     - service: media_player.media_stop
       entity_id: media_player.sony_bravia_tv
     - service: media_player.turn_off
       entity_id: media_player.sony_bravia_tv

The flow is Triggers > Conditions > Actions.

I want to always send a stop command to all media players.
Then turn off the TV if it is on.
Like the example in the bottom of this page.

@xydix, Try

- alias: Larm på Media av
   trigger:
     platform: state
     entity_id: alarm_control_panel.alarm_1
     state: armed_away

   action:
     - service: media_player.media_stop
     - condition:
       condition: state
       entity_id: media_player.sony_bravia_tv
       state: 'on'
     - service: media_player.turn_off
       entity_id: media_player.sony_bravia_tv

Your indentations for

       condition: state
       entity_id: media_player.sony_bravia_tv
       state: 'on'

has two too many spaces.

Edit: Sorry @rpitera, didn’t mean to reply to your comment.

That will give me this in the log.

01-31 00:22:37 homeassistant.util.yaml: duplicate key: "condition"
  in "/home/homeassistant/.homeassistant/automation.yaml", line 499, column 0
  in "/home/homeassistant/.homeassistant/automation.yaml", line 500, column 0
17-01-31 00:22:37 homeassistant.components.automation: duplicate key: "condition"
  in "/home/homeassistant/.homeassistant/automation.yaml", line 499, column 0
  in "/home/homeassistant/.homeassistant/automation.yaml", line 500, column 0

Try this:

 - alias: Larm på Media av
   trigger:
     platform: state
     entity_id: alarm_control_panel.alarm_1
     state: armed_away

   action:
     - service: media_player.media_stop
     - condition: and
       conditions:
         condition: state
         entity_id: media_player.sony_bravia_tv
         state: 'on'
     - service: media_player.turn_off
       entity_id: media_player.sony_bravia_tv

Some trial and error are is need sometimes. Since the error is saying duplicate key “condition” a couple options,

Try removing the first condition and correct the indentation again.

 - alias: Larm på Media av
   trigger:
     platform: state
     entity_id: alarm_control_panel.alarm_1
     state: armed_away

   action:
     - service: media_player.media_stop
     - condition: state
       entity_id: media_player.sony_bravia_tv
       state: 'on'
     - service: media_player.turn_off
       entity_id: media_player.sony_bravia_tv

Or try mimicking the notes and adding an “and” instead of an “or” as the value for the first condition

 - alias: Larm på Media av
   trigger:
     platform: state
     entity_id: alarm_control_panel.alarm_1
     state: armed_away

   action:
     - service: media_player.media_stop
     - condition: and
         conditions: 
           - condition: state 
             entity_id: media_player.sony_bravia_tv
             state: 'on'
     - service: media_player.turn_off
       entity_id: media_player.sony_bravia_tv

looks like @brg468 got it first.

Just tried this.

 - alias: Larm på Media av
   trigger:
     platform: state
     entity_id: alarm_control_panel.alarm_1
     state: armed_away

   action:
     - service: media_player.media_stop
     - condition: state
       entity_id: media_player.sony_bravia_tv
       state: 'on'
     - service: media_player.turn_off
       entity_id: media_player.sony_bravia_tv

No error in the log.
Will try it tomorrow to see it it works.
It seems wrong to me but I get back when I tried it.

It seems to work with the config in my post above.
Now after some thinking i would like to do this a little smarter.
Is there a way to use one trigger, then different action depending of the conditions.
Like this.
Trigger:

If this condition is true
Perform this action

But if this condition is true.
Perform this action instead or also.

And so on…
I want to use ONE trigger in ONE automation.
Then get different actions depending of the condition

If i understand things right i can use AND or OR condition
But if that is true it will perform all actions.

The goal is to pause my Chromecast Audio if it is playing when i arm the alarm, resume it when i disarm, but only if is was playing when i armed.
Stop all movies, on two kod machines. Then turn my TV off is it is on.

You can but you need to put the actions per execution flow in a script.
In each script you can stop the execution with the appropriate conditions.

- alias: Larm på Media av
  trigger:
    platform: state
    entity_id: alarm_control_panel.alarm_1
    state: armed_away

  action:
    - service: media_player.media_stop
    - service: script.execution_flow_A
    - service: script.execution_flow_B

I have never used scripts.
Can i do it like this?

- alias: Larm på Media av
  trigger:
    platform: state
    entity_id: alarm_control_panel.alarm_1
    state: armed_away

  action:
    - service: script.alarm_on
script:
 alarm_on:
 alias: Alarm ON
sequence:
  - condition: state
    entity_id: media_player.CCaudio #just an example
    state: playing
  - service media_player.media_stop
  - condition: state
    entity_id: media_player.sony_bravia_tv
    state: 'on'
  - service: media_player.turn_off
    entity_id: media_player.sony_bravia_tv

or do I have to split it up?

script:
 alarm_on1:
 alias: Alarm ON1
sequence:
  - condition: state
    entity_id: media_player.CCaudio #just an example
    state: playing
  - service media_player.media_stop
script:
 alarm_on2:
 alias: Alarm ON2
sequence:
  - condition: state
    entity_id: media_player.sony_bravia_tv
    state: 'on'
  - service: media_player.turn_off
    entity_id: media_player.sony_bravia_tv

And is there a way to make hass remember if music was playing when alarm was turned on and resume the music when disarmed. But only if it was playing when armed.

Sometimes my automation doesn’t work.
I get this in my log.

17-02-02 09:36:20 homeassistant.core: Error doing job: Future exception was never retrieved
Traceback (most recent call last):
  File "/usr/lib/python3.4/concurrent/futures/thread.py", line 54, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/srv/homeassistant/lib/python3.4/site-packages/homeassistant/core.py", line 1054, in execute_service
    service_handler.func(service_call)
  File "/srv/homeassistant/lib/python3.4/site-packages/homeassistant/components/media_player/__init__.py", line 324, in media_player_service_handler
    getattr(player, method)()
  File "/srv/homeassistant/lib/python3.4/site-packages/homeassistant/components/media_player/kodi.py", line 294, in media_stop
    if len(players) != 0:
TypeError: object of type 'NoneType' has no len()

You need to split the script into a script for every action, if you don’t then the bravia tv will not be turned off if CCAudio is not playing.

Is this coming directly from your real automation scripts? This has a missing semi-colon:

  • service media_player.media_stop