Sky Quality Calculations¶
SQMeter converts TSL2591 light readings into three astronomical metrics. The conversion is only as good as the optical build and calibration: a bare TSL2591 is not a calibrated SQM instrument because it has a very wide angular response and can collect stray light from the horizon, ground, buildings, vehicles, and the enclosure.
flowchart TB
accTitle: From sensor readings to the readings document
accDescr: Light counts become lux, then SQM, NELM and Bortle. The IR sky minus ambient temperature, corrected for humidity, becomes cloud cover. Temperature and humidity give the dew point. Everything goes into one readings document that the dashboard, REST, WebSocket, MQTT, Alpaca and the safety rules all use.
TSL["TSL2591 light counts<br/>auto-ranged gain and integration"] --> LUXAVG["Visible counts averaged over the averaging window<br/>(restarted on each gain change),<br/>minus the dark offset, to lux,<br/>plus the SQM calibration offset if set"]
LUXAVG --> SQM["SQM = 12.6 - 2.5 log10 lux"]
SQM --> NELM["NELM"]
SQM --> BORTLE["Bortle class"]
MLX["MLX90614<br/>sky temperature"] --> DELTA["delta = sky - air"]
BME["BME280<br/>temperature, humidity, pressure"] -->|humidity, or 53% assumed| CORR
BME -->|air temperature| DELTA
DELTA --> CORR["corrected = delta - k/100 x humidity"]
CORR --> CLOUD["Cloud cover % and condition<br/>from the Clear below and Overcast above limits"]
BME --> DEW["Dew point, Magnus formula"]
NELM --> DOC
BORTLE --> DOC
CLOUD --> DOC
DEW --> DOC
DOC["One readings document<br/>with a status per sensor group;<br/>with SQM and SQM-L, both values,<br/>the authoritative one for safety"] --> OUT["Dashboard, REST and WebSocket,<br/>MQTT state, Alpaca properties,<br/>safety rules and alerts"]
Diagram in words
- Light: the TSL2591 auto-ranges its gain and integration time.
- Visible counts are averaged over the averaging window, the dark offset is subtracted, the result is converted to lux, and the SQM calibration offset is applied if one is set. This is the same in every mode; the average restarts whenever the gain or integration time changes, so it never mixes ranges.
- Night mode (maximum gain and integration) is reported separately; it changes sensitivity, not the calculation.
- Sky quality: SQM = 12.6 - 2.5 × log₁₀(lux). NELM and the Bortle class are worked out from the SQM.
- Cloud: the MLX90614's sky temperature minus the BME280's air temperature (the MLX90614's own temperature when the BME280 isn't working), corrected for humidity: corrected = delta - (k / 100) × humidity. The BME280's humidity is used, or 53 % when it isn't working. The corrected delta, against the Clear below and Overcast above limits, gives the cloud cover % and condition.
- Dew point: from the BME280's temperature and humidity (Magnus formula).
- Everything goes into one readings document, with a status per sensor group. With two sky sensors (SQM and SQM-L) each runs the light chain on its own bus; both values are in the document and the authoritative one is used by Alpaca and the safety rules. The dashboard, REST and WebSocket, MQTT
<base>/state, the Alpaca ObservingConditions properties, the safety rules and the alerts all use it, so they always agree.
SQM — Sky Quality Meter¶
Measures sky brightness in magnitudes per square arcsecond (mag/arcsec²). Higher is darker.
Light below 0.0001 lux is treated as 0.0001 lux (SQM 22.6), the darkest the formula reports.
Firmware averages raw TSL2591 counts before this conversion, in every measurement mode: it applies a rolling average (restarted on each gain or integration change), subtracts the saved dark visible offset, then applies the optional SQM calibration offset. sky.sqm is the calibrated value and sky.rawSqm the uncalibrated one, so sqm − rawSqm equals the offset whenever calibration is on. Night mode (MAX gain, 600 ms integration) is when the reading is most sensitive.
To calibrate in Settings → Sensors → Sky quality (with two sky sensors there is one card per sensor, titled SQM and SQM-L): cover the sensor completely (lens cap or foil), wait until Averaging window shows the window full, then press Calibrate dark. The device refuses while the sensor still sees light or the window holds readings from before it was covered. Averaging window (10-300 s, default 90) and Apply SQM offset (±5 mag/arcsec², e.g. to match a reference SQM-L) are in the same card.
Dark calibration should be repeated whenever the lens, baffle, aperture, lens-to-sensor distance, or internal finish changes.
Typical values:
| Location | SQM |
|---|---|
| Excellent dark site | > 22 |
| Rural sky | ~21.5 |
| Suburban sky | ~19–20 |
| City centre | < 17 |
Two sky sensors: SQM and SQM-L¶
A device can carry two TSL2591s: the wide SQM on the first I2C bus, and the narrow-lens
SQM-L on the second I2C bus (sensor.i2c2SDA / sensor.i2c2SCL). Both are read the same way
(auto-ranging, averaging window, dark offset, SQM offset), each with its own averaging and
calibration settings (skyAveraging / skyCalibration for the SQM, skyAveragingL /
skyCalibrationL for the SQM-L), so one can be calibrated without touching the other.
The SQM-L usually reads a little darker than the SQM. Its narrow lens sees only the sky around the zenith; the wide sensor also collects light from near the horizon, where skyglow, buildings and the enclosure add light. A difference of a few tenths of a mag/arcsec² is normal; a large one (more than about 1.5) usually means stray light reaching one of them.
The authoritative reading. One sensor's reading counts: sky.authoritative (sqm, the
default, or sqml). It is the sky and light reading in every interface, the minimum-SQM
safety rule, Alpaca SkyQuality and SkyBrightness, Bluetooth, {sqm} in alerts and Home
Assistant's Sky quality entity; sky.source says which one it is. Both readings are also
published under their own names (skySensors in the readings, {sqm_wide} / {sqm_l} in
alerts, Home Assistant's SQM (wide), SQM-L (narrow) and SQM-L − SQM entities).
No fallback. If the authoritative sensor is switched off, not found or stops answering, the sky reading is missing: the safety verdict is unsafe with a reason naming it ("Sensor fault: SQM-L sensor"), Alpaca reports no data, and the other sensor's value is not used in its place. Pick the other sensor as authoritative to use it: the dashboard's Sky Quality card names the missing sensor with a Use SQM (or Use SQM-L) button, and Settings shows the same note. The sensor that isn't authoritative never changes the safety verdict.
In Settings. Settings → Sensors → Sky sensors lists the SQM and the SQM-L, each with its state and a Use SQM / Use SQM-L switch, the I2C-2 SDA and I2C-2 SCL pins (empty when not wired), Authoritative reading and Show other reading (both only matter with the two sensors on). Each sensor then has its own Sky quality card.
On the dashboard. With both on, the Sky Quality card's big value is the authoritative reading with a pill naming it; with Show other reading on and the other sensor reporting, two more tiles show the other reading and the Difference (SQM-L − SQM), amber above 1.5 mag/arcsec².
An unplugged sensor is reported as a read error, never as a dark sky, and is found again without a restart when it is plugged back in (the device retries every 5 s, backing off to once a minute).
NELM — Naked Eye Limiting Magnitude¶
The faintest star visible to the naked eye. SQMeter estimates it from SQM with Unihedron's formula:
Below SQM 15 (twilight, indoors, city lights) NELM is reported as 0 - no stars visible. Higher = more stars visible. A dark rural sky gives NELM ≈ 6.5. Suburban skies typically give 4–5.
Bortle Dark Sky Scale¶
Each class starts at its lower bound (inclusive): SQM 21.99 is class 1, 21.98 class 2.
| Class | SQM | Description |
|---|---|---|
| 1 | ≥ 21.99 | Excellent dark-sky site |
| 2 | 21.89 – < 21.99 | Typical truly dark site |
| 3 | 21.69 – < 21.89 | Rural sky |
| 4 | 20.49 – < 21.69 | Rural/suburban transition |
| 5 | 19.50 – < 20.49 | Suburban sky |
| 6 | 18.94 – < 19.50 | Bright suburban sky |
| 7 | 18.38 – < 18.94 | Suburban/urban transition |
| 8 | 17.00 – < 18.38 | City sky |
| 9 | < 17.00 | Inner-city sky |
Cloud Detection (MLX90614)¶
When an MLX90614 IR thermometer is present, SQMeter estimates cloud cover by comparing sky IR temperature to the air temperature from the BME280. The MLX90614's own temperature is used only when the BME280 isn't working: it is the sensor's case temperature, which the enclosure warms. A large negative delta (sky much colder than ambient) indicates clear sky. A small delta indicates cloud cover blocking the sky's thermal emission.
Humid air radiates more, making a clear sky look warmer, so the delta is corrected for humidity before it's classified:
delta = sky temperature − air temperature
corrected = delta − (k / 100) × humidity (humidity clamped to 0–100 %)
| Corrected delta | Condition | Cloud cover |
|---|---|---|
| below Clear below (default −13 °C) | Clear | 0 % |
| from Clear below up to Overcast above (default −3 °C) | Cloudy | linear 0 → 100 % |
| at or above Overcast above | Overcast | 100 % |
k is Humidity correction (default 0.75). All three are in Settings → Sensors → Cloud detection.
Humidity comes from the BME280. Without a working BME280 the model assumes 53 % and says so: clouds.humiditySource is assumed in the readings, and the safety verdict counts the environment sensor as faulted. Alpaca's Humidity property never reports the assumed value.
This is a heuristic and works best in dry climates; fog and haze affect it. The model is computed once per reading, so the dashboard, MQTT, Alpaca and the safety verdict always agree.
The dew point is the Magnus formula (a = 17.27, b = 237.7 °C).
Implementation¶
lib/SkyLogic - SkyQuality.cpp, CloudDetection.cpp, Dewpoint.cpp and SkyReadCycle.cpp
(when each sky sensor is started, read and re-found) - with native tests in test/test_sky_logic.
The authoritative copy of the sky reading is chosen in lib/DeviceCore (DeviceSky.cpp).