I have found a way to detect thunderstorms. But it has to depend on a block, the daylight detector, and it only works if the power level of the
daylight detector is above two.

> It only works between sunrise to sunset

Start by creating a scoreboard called `time` and `thunder`:

    /scoreboard objectives add time dummy
    /scoreboard objectives add thunder dummy
    
Then, summon an armor stand with a tag `time` and `thunder`. Surely you can summon two different armor stands but then you have to change some commands as well.

    /summon minecraft:armor_stand ~ ~ ~ {Tags:["time","thunder"]}
    
This armor stand will store the in-game time (and the score later on if thunder is true). In a repeating command block:

    /execute as @e[tag=time] store result score @s time run time query daytime
    
Now place a daylight detector. This block would detect the thunderstorm.

> **Note**: The daylight sensor should not be blocked from the sunlight.
>
> **Note**: The daylight detector should not be inverted.
>
> Placing it on `y = 254` and surrounding it with barrier blocks should keep it inaccessible from players.

Here's the chain of command for thunder detection:

    execute as @e[tag=thunder] run scoreboard players set @s thunder 0
    execute as @e[tag=thunder] if entity @s[scores={time=23074..23297}] if block x y z minecraft:daylight_detector[power=2] run scoreboard players set @s thunder 1
    execute as @e[tag=thunder] if entity @s[scores={time=23298..23532}] if block x y z minecraft:daylight_detector[power=2] run scoreboard players set @s thunder 1
    execute as @e[tag=thunder] if entity @s[scores={time=23298..23532}] if block x y z minecraft:daylight_detector[power=3] run scoreboard players set @s thunder 1
    execute as @e[tag=thunder] if entity @s[scores={time=23533..23768}] if block x y z minecraft:daylight_detector[power=3] run scoreboard players set @s thunder 1
    execute as @e[tag=thunder] if entity @s[scores={time=23533..23768}] if block x y z minecraft:daylight_detector[power=4] run scoreboard players set @s thunder 1
    execute as @e[tag=thunder] if entity @s[scores={time=23769..23963}] if block x y z minecraft:daylight_detector[power=4] run scoreboard players set @s thunder 1
    execute as @e[tag=thunder] if entity @s[scores={time=23964..23999}] if block x y z minecraft:daylight_detector[power=4] run scoreboard players set @s thunder 1
    execute as @e[tag=thunder] if entity @s[scores={time=23964..23999}] if block x y z minecraft:daylight_detector[power=5] run scoreboard players set @s thunder 1
    execute as @e[tag=thunder] if entity @s[scores={time=0..170}] if block x y z minecraft:daylight_detector[power=4] run scoreboard players set @s thunder 1
    execute as @e[tag=thunder] if entity @s[scores={time=0..170}] if block x y z minecraft:daylight_detector[power=5] run scoreboard players set @s thunder 1
    execute as @e[tag=thunder] if entity @s[scores={time=171..537}] if block x y z minecraft:daylight_detector[power=5] run scoreboard players set @s thunder 1
    execute as @e[tag=thunder] if entity @s[scores={time=171..537}] if block x y z minecraft:daylight_detector[power=6] run scoreboard players set @s thunder 1
    execute as @e[tag=thunder] if entity @s[scores={time=538..940}] if block x y z minecraft:daylight_detector[power=6] run scoreboard players set @s thunder 1
    execute as @e[tag=thunder] if entity @s[scores={time=941..1375}] if block x y z minecraft:daylight_detector[power=6] run scoreboard players set @s thunder 1
    execute as @e[tag=thunder] if entity @s[scores={time=941..1375}] if block x y z minecraft:daylight_detector[power=7] run scoreboard players set @s thunder 1
    execute as @e[tag=thunder] if entity @s[scores={time=1376..1867}] if block x y z minecraft:daylight_detector[power=7] run scoreboard players set @s thunder 1
    execute as @e[tag=thunder] if entity @s[scores={time=1376..1867}] if block x y z minecraft:daylight_detector[power=8] run scoreboard players set @s thunder 1
    execute as @e[tag=thunder] if entity @s[scores={time=1868..2450}] if block x y z minecraft:daylight_detector[power=8] run scoreboard players set @s thunder 1
    execute as @e[tag=thunder] if entity @s[scores={time=2451..3175}] if block x y z minecraft:daylight_detector[power=8] run scoreboard players set @s thunder 1
    execute as @e[tag=thunder] if entity @s[scores={time=2451..3175}] if block x y z minecraft:daylight_detector[power=9] run scoreboard players set @s thunder 1
    execute as @e[tag=thunder] if entity @s[scores={time=3176..4301}] if block x y z minecraft:daylight_detector[power=9] run scoreboard players set @s thunder 1
    execute as @e[tag=thunder] if entity @s[scores={time=3176..4301}] if block x y z minecraft:daylight_detector[power=10] run scoreboard players set @s thunder 1
    execute as @e[tag=thunder] if entity @s[scores={time=4302..7707}] if block x y z minecraft:daylight_detector[power=10] run scoreboard players set @s thunder 1
    execute as @e[tag=thunder] if entity @s[scores={time=7708..8833}] if block x y z minecraft:daylight_detector[power=10] run scoreboard players set @s thunder 1
    execute as @e[tag=thunder] if entity @s[scores={time=7708..8833}] if block x y z minecraft:daylight_detector[power=9] run scoreboard players set @s thunder 1
    execute as @e[tag=thunder] if entity @s[scores={time=8834..9957}] if block x y z minecraft:daylight_detector[power=9] run scoreboard players set @s thunder 1
    execute as @e[tag=thunder] if entity @s[scores={time=8834..9957}] if block x y z minecraft:daylight_detector[power=8] run scoreboard players set @s thunder 1
    execute as @e[tag=thunder] if entity @s[scores={time=9958..10139}] if block x y z minecraft:daylight_detector[power=8] run scoreboard players set @s thunder 1
    execute as @e[tag=thunder] if entity @s[scores={time=10140..10632}] if block x y z minecraft:daylight_detector[power=8] run scoreboard players set @s thunder 1
    execute as @e[tag=thunder] if entity @s[scores={time=10140..10632}] if block x y z minecraft:daylight_detector[power=7] run scoreboard players set @s thunder 1
    execute as @e[tag=thunder] if entity @s[scores={time=10633..11067}] if block x y z minecraft:daylight_detector[power=7] run scoreboard players set @s thunder 1
    execute as @e[tag=thunder] if entity @s[scores={time=10633..11067}] if block x y z minecraft:daylight_detector[power=6] run scoreboard players set @s thunder 1
    execute as @e[tag=thunder] if entity @s[scores={time=11068..11469}] if block x y z minecraft:daylight_detector[power=6] run scoreboard players set @s thunder 1
    execute as @e[tag=thunder] if entity @s[scores={time=11470..11836}] if block x y z minecraft:daylight_detector[power=6] run scoreboard players set @s thunder 1
    execute as @e[tag=thunder] if entity @s[scores={time=11470..11836}] if block x y z minecraft:daylight_detector[power=5] run scoreboard players set @s thunder 1
    execute as @e[tag=thunder] if entity @s[scores={time=11837..12041}] if block x y z minecraft:daylight_detector[power=5] run scoreboard players set @s thunder 1
    execute as @e[tag=thunder] if entity @s[scores={time=11837..12041}] if block x y z minecraft:daylight_detector[power=4] run scoreboard players set @s thunder 1
    execute as @e[tag=thunder] if entity @s[scores={time=12042..12238}] if block x y z minecraft:daylight_detector[power=4] run scoreboard players set @s thunder 1
    execute as @e[tag=thunder] if entity @s[scores={time=12239..12474}] if block x y z minecraft:daylight_detector[power=4] run scoreboard players set @s thunder 1
    execute as @e[tag=thunder] if entity @s[scores={time=12239..12474}] if block x y z minecraft:daylight_detector[power=3] run scoreboard players set @s thunder 1
    execute as @e[tag=thunder] if entity @s[scores={time=12475..12708}] if block x y z minecraft:daylight_detector[power=3] run scoreboard players set @s thunder 1
    execute as @e[tag=thunder] if entity @s[scores={time=12475..12708}] if block x y z minecraft:daylight_detector[power=2] run scoreboard players set @s thunder 1
    execute as @e[tag=thunder] if entity @s[scores={time=12709..12933}] if block x y z minecraft:daylight_detector[power=2] run scoreboard players set @s thunder 1
    
> Change `x` `y` `z` with the coordinates of your daylight detector.

The first line of command sets the score of thunder to 0 which is false but it is overcome by another command that detects the power level of the daylight detector during a certain time of the game. For example, the weather is clear and the time is between 4302 and 7707. The power of the daylight detector during that time is 15 but the change of weather also changes the power level of the daylight detector and is set to 10.

And to use the output:

    execute if entity @e[tag=thunder,scores={thunder=1}] run <command>