fix: correct Mikrotik voltage sensor divisor from 1 to 10

Mikrotik/RouterOS voltage sensors were showing incorrect values (469.0
instead of 46.9) because existing sensors in the database have
sensor_divisor=1 instead of the correct value of 10.

The RouterOS profile code was fixed in a previous commit to set
divisor=10 for new sensors, but existing sensors need to be updated.

Migration changes:
- Updates sensor_divisor from 1 to 10 for Mikrotik voltage sensors
- Identifies Mikrotik sensors by OID prefix: 1.3.6.1.4.1.14988.1.1.3.100.1.3
- Divides all historical sensor_reading values by 10 to correct the data
- Both up and down migrations included for full reversibility

After this migration:
- Voltage will show as 46.9V instead of 469.0V
- Historical graphs will show correct voltage values
This commit is contained in:
Graham McIntire 2026-01-25 12:11:48 -06:00
parent 86cac2b567
commit 8158042220
No known key found for this signature in database

View file

@ -0,0 +1,64 @@
defmodule Towerops.Repo.Migrations.FixMikrotikVoltageSensorDivisor do
use Ecto.Migration
def up do
# Fix voltage sensors for RouterOS/Mikrotik devices
# These should have sensor_divisor=10 (deci-volts) but were incorrectly set to 1
# Update voltage sensors where:
# 1. sensor_type = 'voltage'
# 2. sensor_unit = 'V'
# 3. sensor_divisor = 1 (incorrect)
# 4. OID starts with Mikrotik gauge table: 1.3.6.1.4.1.14988.1.1.3.100.1
execute """
UPDATE snmp_sensors
SET sensor_divisor = 10
WHERE sensor_type = 'voltage'
AND sensor_unit = 'V'
AND sensor_divisor = 1
AND sensor_oid LIKE '1.3.6.1.4.1.14988.1.1.3.100.1.3.%'
"""
# Also divide existing sensor_reading values by 10 for these sensors
# This fixes historical data so graphs show correct values
execute """
UPDATE snmp_sensor_readings
SET value = value / 10
WHERE sensor_id IN (
SELECT id
FROM snmp_sensors
WHERE sensor_type = 'voltage'
AND sensor_unit = 'V'
AND sensor_divisor = 10
AND sensor_oid LIKE '1.3.6.1.4.1.14988.1.1.3.100.1.3.%'
)
"""
end
def down do
# Revert voltage sensors back to divisor=1
execute """
UPDATE snmp_sensors
SET sensor_divisor = 1
WHERE sensor_type = 'voltage'
AND sensor_unit = 'V'
AND sensor_divisor = 10
AND sensor_oid LIKE '1.3.6.1.4.1.14988.1.1.3.100.1.3.%'
"""
# Multiply sensor readings back by 10
execute """
UPDATE snmp_sensor_readings
SET value = value * 10
WHERE sensor_id IN (
SELECT id
FROM snmp_sensors
WHERE sensor_type = 'voltage'
AND sensor_unit = 'V'
AND sensor_divisor = 1
AND sensor_oid LIKE '1.3.6.1.4.1.14988.1.1.3.100.1.3.%'
)
"""
end
end