(My Extream Driveway Alarm, Custom) Need help With only turning on or off a service via a swith?

I Need a Way to Turn On Off the Random Service call for Bellow Code.yaml In My Main.yaml
any help is much appreciated. there’s probably a way better way to write this driveway yaml code I’ve built as I’m not a pro in any regard but learning, code would love help.

I would prefer to keep this Driveway code as one big string if its possible as I like reading things all in one place OCD probably because i don’t understand all of YAML yet as learning

####### Needs To be controllable on off E.g a switch or condition #############

##########CODE.YAML##################################

    - service: input_select.select_option
      target:
        entity_id: input_select.mp3_driveway_alarm_list
      data:
        option: >
            {{ state_attr('input_select.mp3_driveway_alarm_list', 'options')
                | reject('in', [states('input_select.mp3_driveway_alarm_list'), 'My option value'])
                | list | random }}
##################### **MAIN.YAML** ##############################
- alias: Driveway Linecrossing MP3
  trigger:
    platform: state
    entity_id: 
      - binary_sensor.driveway_beem
    to: 'on'
  condition:
    - condition: template
      value_template: '{{ (as_timestamp(now()) - as_timestamp(states.automation.driveway_linecrossing_mp3.attributes.last_triggered | default(0)) | int > 200)}}'
  action:
    - service: input_select.select_option
      target:
        entity_id: input_select.mp3_driveway_alarm_list
      data:
        option: >
            {{ state_attr('input_select.mp3_driveway_alarm_list', 'options')
                | reject('in', [states('input_select.mp3_driveway_alarm_list'), 'My option value'])
                | list | random }}
    - service: media_player.volume_set
      data_template:
        entity_id: >
            {% if is_state("input_select.speaker_select", "ALL Speakers") %}
              media_player.all_speakers
            {%-elif is_state("input_select.speaker_select", "All Bed Rooms") %}
              media_player.all_bedrooms_and_office
            {%-elif is_state("input_select.speaker_select", "Master,Lounge,Office") %}
              media_player.all_speakers_but_dakota
            {%-elif is_state("input_select.speaker_select", "Lounge,Office") %}
              media_player.office_and_lounge
            {%-elif is_state("input_select.speaker_select", "Master Bed Speaker") %}
              media_player.bedroom_speaker
            {%-elif is_state("input_select.speaker_select", "Lounge Speaker") %}
              media_player.family_room_speaker
            {%-elif is_state("input_select.speaker_select", "Office Speaker") %}
              media_player.googlehome8575
            {%-elif is_state("input_select.speaker_select", "Dakotas Speaker") %}
              media_player.dakota_s_bedroom_speaker
            {% else %}
              media_player.office_and_lounge
            {% endif %}
        volume_level: "{{states('input_select.driveway_volume')}}"    
    - service: media_player.play_media
      data_template:
        entity_id: >
            {% if is_state("input_select.speaker_select", "ALL Speakers") %}
              media_player.all_speakers
            {%-elif is_state("input_select.speaker_select", "All Bed Rooms") %}
              media_player.all_bedrooms_and_office
            {%-elif is_state("input_select.speaker_select", "Master,Lounge,Office") %}
              media_player.all_speakers_but_dakota
            {%-elif is_state("input_select.speaker_select", "Lounge,Office") %}
              media_player.office_and_lounge
            {%-elif is_state("input_select.speaker_select", "Master Bed Speaker") %}
              media_player.bedroom_speaker
            {%-elif is_state("input_select.speaker_select", "Lounge Speaker") %}
              media_player.family_room_speaker
            {%-elif is_state("input_select.speaker_select", "Office Speaker") %}
              media_player.googlehome8575
            {%-elif is_state("input_select.speaker_select", "Dakotas Speaker") %}
              media_player.dakota_s_bedroom_speaker
            {% else %}
              media_player.office_and_lounge
            {% endif %}
        media_content_type: 'music'
        media_content_id: >
          {% if is_state("input_select.mp3_driveway_alarm_list", "Yoda-BLR-Hmm-Ha") %}
            http://192.168.1/local/Yoda-BLR-Hmm-Ha.mp3
          {%-elif is_state("input_select.mp3_driveway_alarm_list", "Metalica One") %}
            http://192.168.1./local/metalica_one_intro.mp3
          {%-elif is_state("input_select.mp3_driveway_alarm_list", "MessageAlert") %}
            http://192.168.1./local/MessageAlert.mp3 
          {%-elif is_state("input_select.mp3_driveway_alarm_list", "pinksoldier") %}
            http://192.168.1/local/pinksoldier.mp3 
          {%-elif is_state("input_select.mp3_driveway_alarm_list", "carlpoppa2") %}
            http://192.168.1/local/carlpoppa2.mp3 
          {%-elif is_state("input_select.mp3_driveway_alarm_list", "Trump Driveway") %}
            http://192.168.1/local/Trump Driveway.mp3
          {%-elif is_state("input_select.mp3_driveway_alarm_list", "Driveway Alert") %}
            http://192.168.1/local/driveway_movement.mp3
          {%-elif is_state("input_select.mp3_driveway_alarm_list", "Driveway Alert2") %}
            http://192.168.1./local/driveway_single.mp3  
          {%-elif is_state("input_select.mp3_driveway_alarm_list", "Squid Games") %}
            http://192.168.1./local/squid_game.mp3
          {% else %}
            http://192.168.1./local/Trump Driveway.mp3
          {% endif %}        
    - service: camera.snapshot
      data:
        entity_id: camera.driveway
        filename: /config/www/images/driveway.jpg
    - delay:
        seconds: 2
    - service: homeassistant.turn_on
      entity_id: script.driveway_notify_alarm

Not really sure what you mean…

Are you asking for a way to decide if that service call gets made or not when the automation is triggered and then just move on to the next step in the actions?

Because otherwise the binary sensor turning on will start the automation and the service call gets made at that moment.

again are you asking to be able to enable or disable those service calls with the status of a switch but also allowing the rest of the automation actions to run?

Hey thanks for reply,

Ideally I would be able to add something like this to the template.


{%-elif is_state("input_select.mp3_driveway_alarm_list", "Random") %}
            
###### add input_select.Random elif here#####
- service: input_select.select_option
      target:
        entity_id: input_select.mp3_driveway_alarm_list
      data:
        option: >
            {{ state_attr('input_select.mp3_driveway_alarm_list', 'options')
                | reject('in', [states('input_select.mp3_driveway_alarm_list'), 'My option value'])
                | list | random }}

          {% else %}
            None
          {% endif %}
#######################################

This would be my best choice of options then if random is not selected it will just play the selected option

look at either the “choose” or “if-then-else” action.

then use the state of the input_select being random or not as the test for which one it uses.