Macro with different components [SOLVED]

Hi,
every day I dig deeper into Hass.
After a lot of research I still cant get through how I manage my macros.
I read the instructions for “group”, “template switches” and “Command Line Switch” but I guess Im searching on the wrong place :neutral_face:
What I try to do is relatively intuitive.
I need a switch to toggle a scene:
Mediacenter ON:

  • media_player.avr_denon - ON
  • switch.wol_htpc - ON
  • switch.tv - ON
  • media_player.aver_denon:source - HTPC

Mediacenter OFF:

  • media_player.kodi - OFF
  • switch.tv - OFF
  • media_player.aver_denon - OFF

I have diferent components for on and off.
How do I realize this in general?
Where do I have to look? :slight_smile:

Use input Boolean and automation.

1 Like

Daniel, thanks for your quick answer.
Its more complicated then I thought :innocent:

What I gathered till now is:

input_boolean:
  switch_mediacenter:
    name: Watch Movies and more
    initial: off
    icon: mdi:movie
    
automation:
  alias: control mediacenter
  trigger:
    platform: state
    entity_id: switch_mediacenter
    to: 'on'
  condition:
    condition: state
    entity_id: input_boolean.switch_mediacenter
    state: 'on'
  action:
    switch: switch.turn_on
    entity_id: switch.htpc
    media_player: media_player.turn_on
    entity_id: media_player.avr_wohnzimmer
    media_player: media_player.select_source.htpc
    entity_id: media_player.avr_wohnzimmer
    #TV not yet implemented

I guess that wont work that way, but I hope thats the right direction on how to do it? :slight_smile:

Either i read it wrong - but you don’t need any booleans here. What’s wrong with (of course with correct YAML syntaxes)…

automation:
  alias: control mediacenter on
  trigger:
    platform: state
    entity_id: switch_mediacenter
    from: 'off'
    to: 'on'
  action:
    switch: switch.turn_on
    entity_id: switch.htpc
    media_player: media_player.turn_on
    entity_id: media_player.avr_wohnzimmer
    media_player: media_player.select_source.htpc
    entity_id: media_player.avr_wohnzimmer
    #TV not yet implemented

  alias: control mediacenter off
  trigger:
    platform: state
    entity_id: switch_mediacenter
    from: 'on'
    to: 'off'
  action:
    switch: switch.turn_on
    entity_id: switch.htpc
    media_player: media_player.turn_on
    entity_id: media_player.avr_wohnzimmer

Also don’t forget you can fork rules like this (in action part)

  - service_template: >
      {% if states.switch.epson_projector == 'off' %}
        switch.turn_on
      {% else %}
        "None"
      {% endif %}

Your action:part will not work, bad sintaxis.
For every component you want to switch, it needs to be a service: call.
Take a look here (the automation 2 part).
Also take a look at the cookbook to get more examples

As a starter you can omit the automation entirely (and add it to your automation later on)

For both your scenarios (media center on / media center off)
create a script (below only one):

script:
  media_center_on:
    alias: Turn media center on.
    sequence:
     - service: media_player.turn_on
       entity_id: media_player.avr_denon
     - service: switch.turn_on
       entity_id:
        - switch.tv
        - switch.wol_htpc

If you didn’t define any “default_view” as a group your script should come up in you ui automatically with an “activate” button.
Please let me know if that works for you…

Thanks to all of you!
I try to get through all that information :smiley:

What Im trying atm is:

 automation:
  - alias: 'mediacenter on'
    trigger:
      platform: state
      entity_id: switch_mediacenter
      from: 'off'
      to: 'on'
    action:
      - service: media_player.turn_on
        entity_id: media_player.avr_wohnzimmer
      - service: switch.turn_on
        entity_id: switch.htpc
      - service: media_player.select_source.htpc
        entity_id: media_player.avr_wohnzimmer

Errors are:
17-01-24 12:03:32 homeassistant.bootstrap: Invalid config for [switch]: string value is None for dictionary value @ data['platform']. Got None. (See /home/homeassistant/.homeassistant/configuration.yaml, line 174). Please check the docs at https://home-assistant.io/components/switch/ 17-01-24 12:04:11 homeassistant.bootstrap: Invalid config for [automation]: Entity ID switch_mediacenter is an invalid entity id for dictionary value @ data['trigger'][0]['entity_id']. Got None Service media_player.select_source.htpc does not match format <domain>.<name> for dictionary value @ data['action'][2]['service']. Got None. (See /home/homeassistant/.homeassistant/configuration.yaml, line 87). Please check the docs at https://home-assistant.io/components/automation/ 17-01-24 12:04:11 homeassistant.bootstrap: Invalid config for [automation]: Entity ID switch_mediacenter is an invalid entity id for dictionary value @ data['trigger'][0]['entity_id']. Got None. (See /home/homeassistant/.homeassistant/configuration.yaml, line 87). Please check the docs at https://home-assistant.io/components/automation/ 17-01-24 12:04:11 homeassistant.bootstrap: component automation failed to initialize

What I coundt find till now is: I havent implemented a switch yet so entity_id: switch_mediacenter will be created automatically or do I have to implement that switch separately?
If so, it would be an empty switch. Dont know exactly how to do that…

@sanders:
Yeah, I thought I get a switch that would “switch” multiple components when I activate it. And later on I could link that switch to an automation.
Will try your sollution as well.
There are so many possibilities that I dont know which I should take.
But I guess the simplest for the moment with the opportunity to take it to more complexity as for Alexa or automation.

@5p4rtan
The switch “switch_mediacenter” is not created automatically.
You need to define it first.

If I were to solve this I’d try to make the problem smaller and focus on the action part by creating a separate script like I described. If that works, you don’t have to look at it again and focus on other parts of what you’re trying to do.

@sanders:
Yes thanks, I will try that once Im home again :slight_smile:
Cause I cant test it right now may I ask,
will the script have just an on button (no switch) or could I also define an off state?
For Alexa I need to have an on/off switch in order to turn the script properly on and of with only one command.

EDIT:

Okay, got it working now:

script: media_center_on: alias: Mediacenter sequence: - service: media_player.turn_on entity_id: - media_player.avr_wohnzimmer - service: switch.turn_on entity_id: - switch.htpc - service: media_player.select_source data: entity_id: media_player.avr_wohnzimmer source: HTPC

As I expected I just can activate the script.
For my use I need to switch between the on and of states.

@5p4rtan
Well, conceptually a script or an automation cannot be “on” or “off”. It can only run/start.

When your talking on or off, what would be the state of your media_center when one of the devices in that automation (like media_player.avr_wohnzimmer) is switched off manually ?

So my suggestion would be to have two scripts: media_center_on and media_center_off

In future I plan to extend these sort of scripts (I will have an aditional for TV, XBox, FireTV…) wiht if/else cases. If allready on just switch source etc.
Therefore I would also check the corresponding device state.
But for now it wouldnt make a difference if a device is allready of, cause there is no state toggle (if off then on)
Another problem is as I mentioned before. If I have to separate scripts, the Emulated Hue Bridge simulates two scripts as well. To avoid entity collision I have to use different names like:
script: “mediacenter_on” and script: “mediacenter_off”.
Alexa would get two emulated devices (mediacenter__on and mediacenter__off) and I would have to say: “Alexa, mediacenter onon” or “Alexa, mediacenteroff off” instead of just “Alexa, mediacenter on” or off.
Furthermore there would also be two script pattern instead of one switch.

I like these style of two separate script files but therefore it would be nice if I coud hide them and link their activation to a dummy switch.

But for that I have no cloue how to create souch an empty switch…

EDIT :
I tried to use a commandline switch but with no success till now:

################################
    ###CMD LINE SWITCHES###
################################
switch:
 - platform: command_line
   switches:
      mediacenter:
        friendly_name: Mediacenter
        command_on: script.media_center_on
        command_off: script.media_center_off

script:
  mediacenter_on:
    alias: mediacenter on
    sequence:
     - service: media_player.turn_on
       entity_id: 
        - media_player.avr_wohnzimmer
     - service: switch.turn_on
       entity_id:
        - switch.htpc
     - service: media_player.select_source
       data:
         entity_id: media_player.avr_wohnzimmer
         source: HTPC

  mediacenter_off:

alias: mediacenter off
sequence:
 - service: media_player.turn_off
   entity_id: 
    - media_player.avr_wohnzimmer
 - service: media_player.turn_off
   entity_id: 
    - media_player.kodi

I know nothing about Alexa (living in The Netherlands ;-( ) Are you able to control just an “input boolean” with alexa ?

If so, you could use an automation which is triggered by the state of that input boolean.

You can with the emulated_hue component. Just expose the input_boolean domain, give it an emulated_hue_name, and then rescan for devices (via voice or app).

Alexa works with the emulated hue bridge.

The Echo Dot thinks its controlling light bulbs. Every light bulb is one component thats emulated by the bridge.

emulated_hue:
  host_ip: <IP>
  listen_port: 8300
  off_maps_to_on_domains:
    - script
    - scene
  expose_by_default: true
  exposed_domains:
    - media_player
    - switch
    - script

all components of the exposed domains are controllable with “on” and “off”
You can add all domains you want.

Thanks, I better ask before I try :smiley: :

input_boolean:
  test_on:
    name: watch movie
    initial: off
    icon: mdi:car #mandatory :)

automation:
  alias: 'mediacenter on'
    trigger:
      platform: state
      entity_id: script.mediacenter_on
      from: 'off'
      to: 'on'
    condition:
      condition: state
      entity_id: input_boolean.test_on
      state: 'on'
    action:
      - service: input_boolean.turn_on
          data:
            entity_id: input_boolean.test_on

Is that okay this way?
Why do I need to specify the trigger action when it is allready declared in line 6 and 7?

So if I would have a switch:

input_boolean:
  play_media:
    name: media player on or off
    initial: off

and I would say “alexa play_media on” or “off” it would switch the input boolean ?

If so you could use this to trigger your scripts.

Yes thats the way I guess (so “play_media” could be “true” or “false”?)

What I definetely know is:

switch:
  - platform: wake_on_lan
    mac_address: "xx-xx-xx-xx-xx-xx"
    host: [IP]
    name: Computer

I can switch this on and of by saying: “Alexa, turn on Computer”

Same with e.g.:
media_player: - platform: denonavr host: [IP] name: AVR_livingroom

If you put the code below in your configuration.yaml there will be a toggle button in your frontend. If you are able to control that through alexa then there are only a few steps left to get what you want. But you need to try -and confirm- that first.

input_boolean:
  play_media:
    name: media player on or off
    initial: off

Yes, its working fine :slight_smile:

I think this should work.

input_boolean:
  play_media:
    name: media player on or off
    initial: off

automation:
  - alias: switch on media player.
    trigger:
      platform: state
      entity_id: input_boolean.play_media
      from: 'off'
      to: 'on'
    action:
      service: script.turn_on
      entity_id: script.mediacenter_on

  - alias: switch off media player.
    trigger:
      platform: state
      entity_id: input_boolean.play_media
      from: 'on'
      to: 'off'
    action:
      service: script.turn_on
      entity_id: script.mediacenter_off

1 Like