Proper Automations

Hello, i would like to know if it’s possible, how to group the automations using the same base.
Here is my example:

 - alias: 'PowerOff MOVIES started from harmony hub'
   initial_state: on
   trigger:
     - platform: state
       entity_id: remote.harmony_hub
   condition:
     - condition: sun
       after: sunset
     - condition: state
       entity_id: input_select.harmony_hub
       state: 'MOVIES'
     - condition: template
       value_template: '{{ trigger.to_state.attributes.current_activity == "PowerOff" }}'
   action:
     service: scene.turn_on
     entity_id: scene.livingroom_normal
     
 - alias: 'PowerOff PC started from harmony hub'
   initial_state: on
   trigger:
     - platform: state
       entity_id: remote.harmony_hub
   condition:
     - condition: sun
       after: sunset
     - condition: state
       entity_id: input_select.harmony_hub
       state: 'PC'
     - condition: template
       value_template: '{{ trigger.to_state.attributes.current_activity == "PowerOff" }}'
   action:
     service: scene.turn_on
     entity_id: scene.livingroom_normal

 - alias: 'Movies started from harmony hub'
   initial_state: on
   trigger:
     - platform: state
       entity_id: remote.harmony_hub
   condition:
     - condition: template
       value_template: '{{ trigger.to_state.attributes.current_activity == "MOVIES" }}'
   action:
     service: scene.turn_on
     entity_id: scene.livingroom_dim
     
 - alias: 'PC started from harmony hub'
   initial_state: on 
   trigger:
     - platform: state
       entity_id: remote.harmony_hub
   condition:
     - condition: template
       value_template: '{{ trigger.to_state.attributes.current_activity == "PC" }}'
   action:
     service: scene.turn_on
     entity_id: scene.livingroom_dim
     
 - alias: 'ONE started from harmony hub'
   initial_state: on 
   trigger:
     - platform: state
       entity_id: remote.harmony_hub
   condition:
     - condition: template
       value_template: '{{ trigger.to_state.attributes.current_activity == "ONE" }}'
   action:
     service: scene.turn_on
     entity_id: scene.livingroom_dim

Thanks

You could chain all of your automations in group one together with simply an or condition because the action is the same for each of them. Same goes for group 2.

You could also combine all of those into one by using if statements in your action, but that might be more complication and trouble than it’s worth.

Thank you very much.

 - alias: 'PC started from harmony hub'
   initial_state: on 
   trigger:
     - platform: state
       entity_id: remote.harmony_hub
   condition:
     condition: or
     conditions:
       - condition: template
         value_template: '{{ trigger.to_state.attributes.current_activity == "ONE" }}'
       - condition: template
         value_template: '{{ trigger.to_state.attributes.current_activity == "PC" }}'
       - condition: template
         value_template: '{{ trigger.to_state.attributes.current_activity == "MOVIES" }}'
       - condition: template
         value_template: '{{ trigger.to_state.attributes.current_activity == "SMART TV" }}'
       - condition: template
         value_template: '{{ trigger.to_state.attributes.current_activity == "TV" }}'        
   action:
     service: scene.turn_on
     entity_id: scene.livingroom_dim
     
 - alias: 'PowerOff MOVIES started from harmony hub'
   initial_state: on
   trigger:
     - platform: state
       entity_id: remote.harmony_hub
   condition:
     condition: or
     conditions:
       - condition: sun
         after: sunset
       - condition: state
         entity_id: input_select.harmony_hub
         state: 'ONE'
       - condition: state
         entity_id: input_select.harmony_hub
         state: 'MOVIES'
       - condition: state
         entity_id: input_select.harmony_hub
         state: 'PC'
       - condition: state
         entity_id: input_select.harmony_hub
         state: 'TV'
       - condition: state
         entity_id: input_select.harmony_hub
         state: 'SMART TV'
       - condition: template
         value_template: '{{ trigger.to_state.attributes.current_activity == "PowerOff" }}'
   action:
     service: scene.turn_on
     entity_id: scene.livingroom_normal

Your second automation will cause you problems. You need to actually nest the sunset with an and condition, so that it won’t fire before sunset.

Do you think it’s ok with this code:

 - alias: 'Activity Dimmed from harmony hub'
   initial_state: on 
   trigger:
     - platform: state
       entity_id: remote.harmony_hub
   condition:
     condition: and
     conditions:
       - condition: sun
         after: sunset
       - condition: or
         conditions:
           - condition: template
             value_template: '{{ trigger.to_state.attributes.current_activity == "ONE" }}'
           - condition: template
             value_template: '{{ trigger.to_state.attributes.current_activity == "PC" }}'
           - condition: template
             value_template: '{{ trigger.to_state.attributes.current_activity == "MOVIES" }}'
           - condition: template
             value_template: '{{ trigger.to_state.attributes.current_activity == "SMART TV" }}'
           - condition: template
             value_template: '{{ trigger.to_state.attributes.current_activity == "TV" }}'        
   action:
     service: scene.turn_on
     entity_id: scene.livingroom_dim
     
 - alias: 'PowerOff Light On started from harmony hub'
   initial_state: on
   trigger:
     - platform: state
       entity_id: remote.harmony_hub
   condition:
     condition: and
     conditions:
       - condition: sun
         after: sunset
       - condition: or
         conditions:
           - condition: state
             entity_id: input_select.harmony_hub
             state: 'ONE'
           - condition: state
             entity_id: input_select.harmony_hub
             state: 'MOVIES'
           - condition: state
             entity_id: input_select.harmony_hub
             state: 'PC'
           - condition: state
             entity_id: input_select.harmony_hub
             state: 'TV'
           - condition: state
             entity_id: input_select.harmony_hub
             state: 'SMART TV'
           - condition: template
             value_template: '{{ trigger.to_state.attributes.current_activity == "PowerOff" }}'
   action:
     service: scene.turn_on
     entity_id: scene.livingroom_normal

Thanks

1 Like

Yes! As long as that doesn’t give any errors when you upload it, I think it will work. If it doesn’t, just post again. :slightly_smiling_face:

There is something wrong with the code, the automation don’t fire.

I think will need help for my first automation with “or” & “and” to clearly understand the typo process.

Thanks in advance.

Your syntax is good (I think), but in your “normal” scene automation you should have the Power Off value template in the “and” portion," not the “or” portion. Right now, as long as it’s after sunset and the input_select is on one, movies, PC, TV, or Smart TV, the second automation will fire on any state change of the hub, not just power off.

Thnak you for you response.