tomaae / homeassistant-mikrotik_router

Mikrotik router integration for Home Assistant
Apache License 2.0
302 stars 51 forks source link

More system health variables #320

Open TV456 opened 11 months ago

TV456 commented 11 months ago

The MikroTik Router Integration does not yet supply all my system health items. I am missing fan 3 and fan 4, poe-out-consumptuon, psu1-current, psu1-voltage, psu2-current and psu2-voltages.

I have altered the sensor_types.py file and tested it.

I have added:

UnitOfElectricCurrent

to:

from homeassistant.const import ()

And added the following sensors:

MikrotikSensorEntityDescription(
    key="system_fan3-speed",
    name="Fan3 speed",
    icon="mdi:fan",
    native_unit_of_measurement=REVOLUTIONS_PER_MINUTE,
    device_class=None,
    state_class=SensorStateClass.MEASUREMENT,
    entity_category=EntityCategory.DIAGNOSTIC,
    ha_group="System",
    data_path="health",
    data_attribute="fan3-speed",
    data_name="",
    data_uid="",
    data_reference="",
),
MikrotikSensorEntityDescription(
    key="system_fan4-speed",
    name="Fan4 speed",
    icon="mdi:fan",
    native_unit_of_measurement=REVOLUTIONS_PER_MINUTE,
    device_class=None,
    state_class=SensorStateClass.MEASUREMENT,
    entity_category=EntityCategory.DIAGNOSTIC,
    ha_group="System",
    data_path="health",
    data_attribute="fan4-speed",
    data_name="",
    data_uid="",
    data_reference="",
),
MikrotikSensorEntityDescription(
    key="system_poe_out_consumption",
    name="PoE out power consumption",
    icon="mdi:transmission-tower",
    native_unit_of_measurement=UnitOfPower.WATT,
    suggested_unit_of_measurement=UnitOfPower.WATT,
    suggested_display_precision=1,
    device_class=SensorDeviceClass.POWER,
    state_class=SensorStateClass.MEASUREMENT,
    entity_category=EntityCategory.DIAGNOSTIC,
    ha_group="System",
    data_path="health",
    data_attribute="poe-out-consumption",
    data_name="",
    data_uid="",
    data_reference="",
),
MikrotikSensorEntityDescription(
    key="system_psu1_current",
    name="PSU 1 power consumption",
    icon="mdi:lightning-bolt-circle",
    native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
    suggested_unit_of_measurement=UnitOfElectricCurrent.MILLIAMPERE,
    suggested_display_precision=1,
    device_class=SensorDeviceClass.CURRENT,
    state_class=SensorStateClass.MEASUREMENT,
    entity_category=EntityCategory.DIAGNOSTIC,
    ha_group="System",
    data_path="health",
    data_attribute="psu1-current",
    data_name="",
    data_uid="",
    data_reference="",
),
MikrotikSensorEntityDescription(
    key="system_psu1_voltage",
    name="PSU 1 Voltage",
    icon="mdi:lightning-bolt",
    native_unit_of_measurement=UnitOfElectricPotential.VOLT,
    suggested_unit_of_measurement=UnitOfElectricPotential.VOLT,
    suggested_display_precision=1,
    device_class=SensorDeviceClass.VOLTAGE,
    state_class=SensorStateClass.MEASUREMENT,
    entity_category=EntityCategory.DIAGNOSTIC,
    ha_group="System",
    data_path="health",
    data_attribute="psu1-voltage",
    data_name="",
    data_uid="",
    data_reference="",
),
MikrotikSensorEntityDescription(
    key="system_psu2_current",
    name="PSU 2 power consumption",
    icon="mdi:lightning-bolt-circle",
    native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
    suggested_unit_of_measurement=UnitOfElectricCurrent.MILLIAMPERE,
    suggested_display_precision=1,
    device_class=SensorDeviceClass.CURRENT,
    state_class=SensorStateClass.MEASUREMENT,
    entity_category=EntityCategory.DIAGNOSTIC,
    ha_group="System",
    data_path="health",
    data_attribute="psu2-current",
    data_name="",
    data_uid="",
    data_reference="",
),
MikrotikSensorEntityDescription(
    key="system_psu2_voltage",
    name="PSU 2 Voltage",
    icon="mdi:lightning-bolt",
    native_unit_of_measurement=UnitOfElectricPotential.VOLT,
    suggested_unit_of_measurement=UnitOfElectricPotential.VOLT,
    suggested_display_precision=1,
    device_class=SensorDeviceClass.VOLTAGE,
    state_class=SensorStateClass.MEASUREMENT,
    entity_category=EntityCategory.DIAGNOSTIC,
    ha_group="System",
    data_path="health",
    data_attribute="psu2-voltage",
    data_name="",
    data_uid="",
    data_reference="",
),

Thanks to your, @tomaae, great work, this was quite easily implemented and the sensors showed up immediately after HomeAssistant restart. I hope you add these sensors with the next update.

For the record: fan 3 and fan 4 are added to my router aftermarket to optimize airflow and RouterOS picks up their respectable variables automatically.

tomaae commented 11 months ago

Yea, there are many mikrotik devices with different health sensors. Can you give me output of /system/health/print terse?

TV456 commented 11 months ago

Hereby:

/system/health/print terse

0 name=poe-out-consumption value=21.8 type=W 1 name=cpu-temperature value=57 type=C 2 name=fan1-speed value=1485 type=RPM 3 name=fan2-speed value=1635 type=RPM 4 name=fan3-speed value=2280 type=RPM 5 name=fan4-speed value=2415 type=RPM 6 name=board-temperature1 value=29 type=C 7 name=psu1-voltage value=26.4 type=V 8 name=psu2-voltage value=52.8 type=V 9 name=psu1-current value=0 type=A 10 name=psu2-current value=0.4 type=A

ffries commented 10 months ago

Great, how can I get power consumption from my Mikrotik devices? CRS328-24P-4S+

/system/health/print terse 0 name=poe-out-consumption value=21.9 type=W 1 name=cpu-temperature value=52 type=C 2 name=sfp-temperature value=36 type=C 3 name=fan1-speed value=0 type=RPM 4 name=fan2-speed value=0 type=RPM 5 name=board-temperature1 value=30 type=C 6 name=psu1-voltage value=26.4 type=V 7 name=psu2-voltage value=52.7 type=V 8 name=psu1-current value=0.1 type=A 9 name=psu2-current value=0.3 type=A

TV456 commented 10 months ago

@ffries

Alter (copy and paste) the sensor_types.py like the first post exact as described and you should be good to go.

Alternatively you can add the following lines to the end of the file sensor_types.py:

from homeassistant.const import (
    UnitOfElectricCurrent,
)

SENSOR_TYPES += (
    MikrotikSensorEntityDescription(
        key="system_fan3-speed",
        name="Fan3 speed",
        icon="mdi:fan",
        native_unit_of_measurement=REVOLUTIONS_PER_MINUTE,
        device_class=None,
        state_class=SensorStateClass.MEASUREMENT,
        entity_category=EntityCategory.DIAGNOSTIC,
        ha_group="System",
        data_path="health",
        data_attribute="fan3-speed",
        data_name="",
        data_uid="",
        data_reference="",
    ),
    MikrotikSensorEntityDescription(
        key="system_fan4-speed",
        name="Fan4 speed",
        icon="mdi:fan",
        native_unit_of_measurement=REVOLUTIONS_PER_MINUTE,
        device_class=None,
        state_class=SensorStateClass.MEASUREMENT,
        entity_category=EntityCategory.DIAGNOSTIC,
        ha_group="System",
        data_path="health",
        data_attribute="fan4-speed",
        data_name="",
        data_uid="",
        data_reference="",
    ),
    MikrotikSensorEntityDescription(
        key="system_poe_out_consumption",
        name="PoE out power consumption",
        icon="mdi:transmission-tower",
        native_unit_of_measurement=UnitOfPower.WATT,
        suggested_unit_of_measurement=UnitOfPower.WATT,
        suggested_display_precision=1,
        device_class=SensorDeviceClass.POWER,
        state_class=SensorStateClass.MEASUREMENT,
        entity_category=EntityCategory.DIAGNOSTIC,
        ha_group="System",
        data_path="health",
        data_attribute="poe-out-consumption",
        data_name="",
        data_uid="",
        data_reference="",
    ),
    MikrotikSensorEntityDescription(
        key="system_psu1_current",
        name="PSU 1 power consumption",
        icon="mdi:lightning-bolt-circle",
        native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
        suggested_unit_of_measurement=UnitOfElectricCurrent.MILLIAMPERE,
        suggested_display_precision=1,
        device_class=SensorDeviceClass.CURRENT,
        state_class=SensorStateClass.MEASUREMENT,
        entity_category=EntityCategory.DIAGNOSTIC,
        ha_group="System",
        data_path="health",
        data_attribute="psu1-current",
        data_name="",
        data_uid="",
        data_reference="",
    ),
    MikrotikSensorEntityDescription(
        key="system_psu1_voltage",
        name="PSU 1 Voltage",
        icon="mdi:lightning-bolt",
        native_unit_of_measurement=UnitOfElectricPotential.VOLT,
        suggested_unit_of_measurement=UnitOfElectricPotential.VOLT,
        suggested_display_precision=1,
        device_class=SensorDeviceClass.VOLTAGE,
        state_class=SensorStateClass.MEASUREMENT,
        entity_category=EntityCategory.DIAGNOSTIC,
        ha_group="System",
        data_path="health",
        data_attribute="psu1-voltage",
        data_name="",
        data_uid="",
        data_reference="",
    ),
    MikrotikSensorEntityDescription(
        key="system_psu2_current",
        name="PSU 2 power consumption",
        icon="mdi:lightning-bolt-circle",
        native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
        suggested_unit_of_measurement=UnitOfElectricCurrent.MILLIAMPERE,
        suggested_display_precision=1,
        device_class=SensorDeviceClass.CURRENT,
        state_class=SensorStateClass.MEASUREMENT,
        entity_category=EntityCategory.DIAGNOSTIC,
        ha_group="System",
        data_path="health",
        data_attribute="psu2-current",
        data_name="",
        data_uid="",
        data_reference="",
    ),
    MikrotikSensorEntityDescription(
        key="system_psu2_voltage",
        name="PSU 2 Voltage",
        icon="mdi:lightning-bolt",
        native_unit_of_measurement=UnitOfElectricPotential.VOLT,
        suggested_unit_of_measurement=UnitOfElectricPotential.VOLT,
        suggested_display_precision=1,
        device_class=SensorDeviceClass.VOLTAGE,
        state_class=SensorStateClass.MEASUREMENT,
        entity_category=EntityCategory.DIAGNOSTIC,
        ha_group="System",
        data_path="health",
        data_attribute="psu2-voltage",
        data_name="",
        data_uid="",
        data_reference="",
    ),
)

Note: considering your system health variables, you haven't added custom fans to your MikroTik router (which is optional), so fan 3 and fan 4 will report 0.