Trigger conditions - loop

Hi,

I am working on a automation/blueprint that will dim the lights when a specific apps are playing on the AppleTV. As only the app really changes on the conditions lines of the trigger, I would like to simplify the automation so that I only have one block of conditions which is looped for every app name thats set and performs the actions one the condition is met.

First question is, if this is even possible or is this something better suited of a script? I couldn’t really find the answer online.
Secondly, if so could this be done in the current automation that I have set up? The only thing that changes in the trigger conditions would be state, of the attribute app_name.

trigger:
platform: device
device_id: ad8a4620d59a87dd496a789fe89820fe`
  domain: media_player
  entity_id: media_player.woonkamer
  type: playing
condition:
 - condition: or
   conditions:
   - condition: and
     conditions:
     - condition: state
       entity_id: media_player.woonkamer
       state: Disney+
       attribute: app_name
       after: sunset
     - condition: sun
       after: sunset
       after_offset: !input 'aftersunset_offset'
       before: sunrise
       before_offset: !input 'beforesunrise_offset'
   - condition: and
     conditions:
     - condition: state
       entity_id: media_player.woonkamer
       state: Netflix
       attribute: app_name
     - condition: sun
       after: sunset
       after_offset: !input 'aftersunset_offset'
       before: sunrise
       before_offset: !input 'beforesunrise_offset'
action:
- choose:
  - conditions:
    - condition: device
      device_id: !input 'mediadevice' #ad8a4620d59a87dd496a789fe89820fe
      domain: media_player
      entity_id: media_player.woonkamer
      type: is_playing
    sequence:
    - service: light.turn_on
      data_template:
        entity_id: !input 'light'
        brightness_pct: !input 'play_brightness'
        transition: !input 'dim_transition'
  - conditions:
    - condition: device
      device_id: !input 'mediadevice' #ad8a4620d59a87dd496a789fe89820fe
      domain: media_player
      entity_id: media_player.woonkamer
      type: is_paused
    sequence:
    - service: light.turn_on
      data_template:
        entity_id: !input 'light'
        brightness_pct: !input 'pause_brightness'
        transition: !input 'dim_transition'

Thanks in advance!

This is what templates are for.

{{ state_attr('media_player.woonkamer', 'app_name') in ['Netflix', 'Disney+', ... ] }}

Thanks for the reply, I wasn’t aware of templates, but i still have some issue getting it to work. HA keeps giving me errors when the blueprint is loaded on restart.

Failed to load blueprint: while parsing a flow mapping in "/config/blueprints/automation/RWA/appletv_light_dimmer.yaml", line 81, column 8 expected ',' or '}', but got '<scalar>' in "/config/blueprints/automation/RWA/appletv_light_dimmer.yaml", line 81, column 57

I try to follow the documentation: Conditions - Home Assistant
The line with > in front is the line that is giving me the error message.

trigger:                                                                                                                                                                                                                                                                                                                   
  platform: device                                                                                                                                                                                                                                                                                                         
  device_id: ad8a4620d59a87dd496a789fe89820fe                                                                                                                                                                                                                                                                              
  domain: media_player                                                                                                                                                                                                                                                                                                     
  entity_id: media_player.woonkamer                                                                                                                                                                                                                                                                                        
  type: playing                                                                                                                                                                                                                                                                                                            
condition:                                                                                                                                                                                                                                                                                                                 
  condition: and                                                                                                                                                                                                                                                                                                           
  conditions:                                                                                                                                                                                                                                                                                                              
>   - {{ state_attr('media_player.woonkamer', 'app_name') in ['Netflix', 'Disney+', 'Plex'] }}                                                                                                                                                                                                                             
    - condition: sun                                                                                                                                                                                                                                                                                                       
      after: sunset                                                                                                                                                                                                                                                                                                        
      after_offset: !input 'aftersunset_offset'                                                                                                                                                                                                                                                                            
      before: sunrise                                                                                                                                                                                                                                                                                                      
      before_offset: !input 'beforesunrise_offset'                                                                                                                                                                                                                                                                         
action:                                                                                                                                                                                                                                                                                                                    
- choose:                                                                                                                                                                                                                                                                                                                  
  - conditions:                                                                                                                                                                                                                                                                                                            
    - condition: device                                                                                                                                                                                                                                                                                                    
      device_id: !input 'mediadevice' #ad8a4620d59a87dd496a789fe89820fe                                                                                                                                                                                                                                                    
      domain: media_player                                                                                                                                                                                                                                                                                                 
      entity_id: media_player.woonkamer                                                                                                                                                                                                                                                                                    
      type: is_playing                                                                                                                                                                                                                                                                                                     
    sequence:                                                                                                                                                                                                                                                                                                              
    - service: light.turn_on                                                                                                                                                                                                                                                                                               
      data_template:                                                                                                                                                                                                                                                                                                       
        entity_id: !input 'light'                                                                                                                                                                                                                                                                                          
        brightness_pct: !input 'play_brightness'                                                                                                                                                                                                                                                                           
        transition: !input 'dim_transition'                                                                                                                                                                                                                                                                                
  - conditions:                                                                                                                                                                                                                                                                                                            
    - condition: device                                                                                                                                                                                                                                                                                                    
      device_id: !input 'mediadevice' #ad8a4620d59a87dd496a789fe89820fe                                                                                                                                                                                                                                                    
      domain: media_player                                                                                                                                                                                                                                                                                                 
      entity_id: media_player.woonkamer                                                                                                                                                                                                                                                                                    
      type: is_paused                                                                                                                                                                                                                                                                                                      
    sequence:                                                                                                                                                                                                                                                                                                              
    - service: light.turn_on                                                                                                                                                                                                                                                                                               
      data_template:                                                                                                                                                                                                                                                                                                       
        entity_id: !input 'light'                                                                                                                                                                                                                                                                                          
        brightness_pct: !input 'pause_brightness'                                                                                                                                                                                                                                                                          
        transition: !input 'dim_transition'

Thanks again, sorry if this seems a bit of a dumb questions :sweat_smile:

single line templates need to be wrapped in quotes. As a beginner, you should look at the script documentation and the available conditions and avoid the shorthand templates until you understand yaml & templates.

And it should be:

condition:                                                                                                                                                                                                                                                                                                                 
  condition: and                                                                                                                                                                                                                                                                                                           
  conditions:                                                                                                                                                                                                                                                                                                              
    - condition: template
      value_template: '{{ state_attr(''media_player.woonkamer'', ''app_name'') in [''Netflix'', ''Disney+'', ''Plex''] }}'
    - condition: sun                                                                                                                                                                                                                                                                                                       
      after: sunset                                                                                                                                                                                                                                                                                                        
      after_offset: !input 'aftersunset_offset'                                                                                                                                                                                                                                                                            
      before: sunrise                                                                                                                                                                                                                                                                                                      
      before_offset: !input 'beforesunrise_offset'    
        

Thank you both for the reply and the solution, will definitely look into the documentation to get a better understanding of how this all works!