How to have a delay happen only when needed

I have the following script right now that is triggered by an Alexa:

What I have been trying to figure out and have not been successful in finding information about is how can I have the delays only happen if they are needed. Like if I go to bed and I wasn’t watching TV I would like to skip the delay for that step so it goes right to the next step. I don’t care really if it still calls for the state change in the devices (actually would like that still just in case there is an error in reading the state, but want to eliminate the long delay of the last step happening if that’s the only device actually on.

Add a delay template for every delay you want to skip. Check the device that the delay is linked to. Add this to the delay prior to the device turning off/ executing command. Note that your off commands will still be sent.

  alias: Goodnight
      #Stop Chromecast
    - service: media_player.media_stop
      entity_id: media_player.master_bedroom_tv
    - delay:
        seconds: "{{ 0 if is_state_attr('','current_activity','PowerOff') else 3 }}"
      #Turn Off Chromecast
    - service: media_player.turn_off
        entity_id: media_player.master_bedroom_tv
      #Turn Off TV
    - service: remote.send_command
        device: 54740953
          - PowerOff
    - delay:
        seconds: "{{ 0 if is_state('group.all_switches','off') else 3 }}"
      # Switches Off
    - service: switch.turn_off
        entity_id: group.all_switches
    - delay:
        seconds: "{{ 0 if is_state('group.all_lights','off') else 3 }}"
      #Lights Off
    - service: light.turn_off
        entity_id: group.all_lights
        transition: 2

Awesome thank you! I will give this a test tomorrow hopefully.