How can I determine the light level of a block with commands?

I want to build a set of command blocks that will automatically place a torch at the foot of a player (or entity) when the light level falls below a certain threshold. So basically I need to be able to determine the light level at a player using command blocks.
I've looked the some of the wiki pages on NBT data to see if the light level is stored with the block, so I could do something like /execute @p ~ ~ ~ testforblock ~ ~ ~ minecraft:air 0 {dataTagForLight:3b}
, but there doesn't seem to be any such data tag.
I've considered spawning a daylight detector, but I have a feeling it won't do what I want it to do while in caves, since as far as I can tell, in the primary mode, it's only affected by sunlight. Plus there's other issue with the plan, and it just feels like a kludge.
So, is there a way to determine the light level of a given block? If so, how?
Best Answer
This method is also a fairly bad kludge and a bit impractical, but it should be able to detect block light as well as sunlight.
There are a few of processes that only occur at certain light levels, sunlight or not:
Event || Light Level
================================
Snow/ice melt || >=12
Mushrooms uproot || >=13
Flowers uproot || <=7
Plants grow || <=9
With a very high /gamerule randomTickSpeed
, these processes can happen almost immediately. Obvious disadvantage here is that the gamerule will also effect the processes throughout the entire world.
Different mobs also have different spawning light levels:
Mob || Light Level
================================
Bats || <=3
Most hostiles || <=7
Blaze/silverfish || <=11
Most passives || >=9
You can also create custom mob spawners will spawn a mob every tick by changing its MinSpawnDelay
and MaxSpawnDelay
.
Using either of these methods (or both), what you'd need to do is something like:
- Clone blocks around the block/player to a specific area
- Set the block/spawner in the middle of the newly cloned area
- Wait a few ticks
- Test if a mob has spawned, or a process occurred
You could also have the mob spawner (minecart) or test block at the actual player's location, rather than cloning it first, which would reduce lag a lot but cause the player to see flickering mobs/blocks.
Pictures about "How can I determine the light level of a block with commands?"



Quick Answer about "How can I determine the light level of a block with commands?"
How do I check my block light level?
Light Levels CommandBy pressing F3, the debug screen will appear containing a \u201cLight source\u201d in the bottom part of the debug menu. So, in the debug menu, every piece of information concerning light levels, ranging from 0 to 15, will appear regarding the block your in-game character is looking at.What is the command to check light level in Minecraft?
Light blocks will not be found in the Creative menu, so they have to be acquired by a command. In Minecraft Bedrock, the syntax is "/give <target> light_block [amount: int] [data: int (0 - 15)] [components: json]" and in Java Edition it is "/give <target> minecraft:light{BlockStateTag: {level:"<int>"}}."Minecraft - How To Use The Light Block (Java/Bedrock)
Sources: Stack Exchange - This article follows the attribution requirements of Stack Exchange and is licensed under CC BY-SA 3.0.
Images: Andrea Piacquadio, Алекке Блажин, Miriam Alonso, Andrea Piacquadio