Cant figure out how to set timer for motion lights

so if i have something “turn on bedroom lights when bedroom motion activated” how do i get it to automatically turn off after like 5 minutes? also make some kinda “virtual switch” to enable and disable this motion lighting? and is there like a way to edit this automation as a script somehow? to see whats actually going on and stuff. p.s. i just got this to work so im not sure how this stuff works