How do i detect when a player is in the nether or any other dimension to run a command? [duplicate]

Im new in making datapacks but, All im trying to do is make it detect when im in the minecraft:nether_1. So if im in the nether it will run the command "execute as @a at @s in minecraft:nether_2 run tp @s ~ ~ ~"


There is an advancement condition called location that can check for dimension.

In commands you can read out the "Dimension" field in NBT:

/say @a[nbt={Dimension:-1}]

This tells you all players in the Nether. 0 is Overworld, 1 is End.

It might give you slightly better performance to use this workaround:

/execute in the_nether run say @a[distance=0..]

With that, the game doesn't need to read any NBT and can use its already existing lists of players per dimension.