OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Temperatures OpenHardwareMonitor with python

  • Thread starter Thread starter zumbide123
  • Start date Start date
Z

zumbide123

Guest
I want to retrieve the temperatures, RAM memory usage, used disk space, and CPU load from my computer. I'm using OpenHardwareMonitor for this, but I'm able to get everything except the temperature. When I execute OpenHardwareMonitor on my computer, the temperatures are displayed, but I can't retrieve them with Python. Any ideas?

Of all the options I tried, OpenHardwareMonitor was the only one that showed me results.

enter image description hereenter image description here

Code:
import clr

file = "OpenHardwareMonitorLib.dll"
clr.AddReference(file)

from OpenHardwareMonitor import Hardware

openhardwaremonitor_sensortypes = [
    "Voltage",      # 0
    "Clock",        # 1
    "Temperature",  # 2
    "Load",         # 3
    "Fan",          # 4
    "Flow",         # 5
    "Control",      # 6
    "Level",        # 7
    "Factor",       # 8
    "Power",        # 9
    "Data",         # 10
    "SmallData",    # 11
]

openhardwaremonitor_hwtypes = {
    Hardware.HardwareType.Mainboard: "Mainboard",
    Hardware.HardwareType.SuperIO: "Super I/O",
    Hardware.HardwareType.CPU: "CPU",
    Hardware.HardwareType.RAM: "RAM",
    Hardware.HardwareType.GpuNvidia: "Nvidia GPU",
    Hardware.HardwareType.GpuAti: "ATI GPU",
    Hardware.HardwareType.TBalancer: "T-Balancer",
    Hardware.HardwareType.Heatmaster: "Heatmaster",
    Hardware.HardwareType.HDD: "HDD",
}

def initialize_openhardwaremonitor():
    handle = Hardware.Computer()
    handle.MainboardEnabled = True
    handle.SuperIOEnabled = True
    handle.CPUEnabled = True
    handle.RAMEnabled = True
    handle.GPUEnabled = True
    handle.HDDEnabled = True
    handle.SSDEnabled = True
    handle.BatteryEnabled = True
    handle.PsuEnabled = True
    handle.CoolingDeviceEnabled = True
    handle.UPSEnabled = True

    handle.Open()
    return handle

def fetch_stats(handle):
    for hardware in handle.Hardware:
        hardware.Update()
        print(f"Hardware: {openhardwaremonitor_hwtypes.get(hardware.HardwareType, 'Unknown')} - {hardware.Name}")
        for sensor in hardware.Sensors:
            parse_sensor(sensor)

        for subhardware in hardware.SubHardware:
            subhardware.Update()
            print(f"  SubHardware: {openhardwaremonitor_hwtypes.get(subhardware.HardwareType, 'Unknown')} - {subhardware.Name}")
            for subsensor in subhardware.Sensors:
                parse_sensor(subsensor)

def parse_sensor(sensor):
    if sensor.Value is not None:
        sensor_type = openhardwaremonitor_sensortypes[sensor.SensorType]
        unit = "\u00B0C" if sensor_type == "Temperature" else ""
        if sensor_type == "Temperature":
            print(
                u"%s %s %s Sensor #%i %s - %.2f%s"
                % (
                    openhardwaremonitor_hwtypes.get(sensor.Hardware.HardwareType, "Unknown"),
                    sensor.Hardware.Name,
                    sensor_type,
                    sensor.Index,
                    sensor.Name,
                    sensor.Value,
                    unit
                )
            )
        else:
            print(
                u"%s %s %s Sensor #%i %s - %.2f"
                % (
                    openhardwaremonitor_hwtypes.get(sensor.Hardware.HardwareType, "Unknown"),
                    sensor.Hardware.Name,
                    sensor_type,
                    sensor.Index,
                    sensor.Name,
                    sensor.Value
                )
            )
    else:
        print(f"Sensor without value: {sensor.Name} of type {openhardwaremonitor_sensortypes[sensor.SensorType]}")

if __name__ == "__main__":
    print("OpenHardwareMonitor:")
    HardwareHandle = initialize_openhardwaremonitor()
    fetch_stats(HardwareHandle)
<p>I want to retrieve the temperatures, RAM memory usage, used disk space, and CPU load from my computer. I'm using OpenHardwareMonitor for this, but I'm able to get everything except the temperature. When I execute OpenHardwareMonitor on my computer, the temperatures are displayed, but I can't retrieve them with Python. Any ideas?</p>
<p>Of all the options I tried, OpenHardwareMonitor was the only one that showed me results.</p>
<p><a href="https://i.sstatic.net/wrjssFY8.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/wrjssFY8.png" alt="enter image description here" /></a><a href="https://i.sstatic.net/3KZK46Wl.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/3KZK46Wl.png" alt="enter image description here" /></a></p>
<pre><code>import clr

file = "OpenHardwareMonitorLib.dll"
clr.AddReference(file)

from OpenHardwareMonitor import Hardware

openhardwaremonitor_sensortypes = [
"Voltage", # 0
"Clock", # 1
"Temperature", # 2
"Load", # 3
"Fan", # 4
"Flow", # 5
"Control", # 6
"Level", # 7
"Factor", # 8
"Power", # 9
"Data", # 10
"SmallData", # 11
]

openhardwaremonitor_hwtypes = {
Hardware.HardwareType.Mainboard: "Mainboard",
Hardware.HardwareType.SuperIO: "Super I/O",
Hardware.HardwareType.CPU: "CPU",
Hardware.HardwareType.RAM: "RAM",
Hardware.HardwareType.GpuNvidia: "Nvidia GPU",
Hardware.HardwareType.GpuAti: "ATI GPU",
Hardware.HardwareType.TBalancer: "T-Balancer",
Hardware.HardwareType.Heatmaster: "Heatmaster",
Hardware.HardwareType.HDD: "HDD",
}

def initialize_openhardwaremonitor():
handle = Hardware.Computer()
handle.MainboardEnabled = True
handle.SuperIOEnabled = True
handle.CPUEnabled = True
handle.RAMEnabled = True
handle.GPUEnabled = True
handle.HDDEnabled = True
handle.SSDEnabled = True
handle.BatteryEnabled = True
handle.PsuEnabled = True
handle.CoolingDeviceEnabled = True
handle.UPSEnabled = True

handle.Open()
return handle

def fetch_stats(handle):
for hardware in handle.Hardware:
hardware.Update()
print(f"Hardware: {openhardwaremonitor_hwtypes.get(hardware.HardwareType, 'Unknown')} - {hardware.Name}")
for sensor in hardware.Sensors:
parse_sensor(sensor)

for subhardware in hardware.SubHardware:
subhardware.Update()
print(f" SubHardware: {openhardwaremonitor_hwtypes.get(subhardware.HardwareType, 'Unknown')} - {subhardware.Name}")
for subsensor in subhardware.Sensors:
parse_sensor(subsensor)

def parse_sensor(sensor):
if sensor.Value is not None:
sensor_type = openhardwaremonitor_sensortypes[sensor.SensorType]
unit = "\u00B0C" if sensor_type == "Temperature" else ""
if sensor_type == "Temperature":
print(
u"%s %s %s Sensor #%i %s - %.2f%s"
% (
openhardwaremonitor_hwtypes.get(sensor.Hardware.HardwareType, "Unknown"),
sensor.Hardware.Name,
sensor_type,
sensor.Index,
sensor.Name,
sensor.Value,
unit
)
)
else:
print(
u"%s %s %s Sensor #%i %s - %.2f"
% (
openhardwaremonitor_hwtypes.get(sensor.Hardware.HardwareType, "Unknown"),
sensor.Hardware.Name,
sensor_type,
sensor.Index,
sensor.Name,
sensor.Value
)
)
else:
print(f"Sensor without value: {sensor.Name} of type {openhardwaremonitor_sensortypes[sensor.SensorType]}")

if __name__ == "__main__":
print("OpenHardwareMonitor:")
HardwareHandle = initialize_openhardwaremonitor()
fetch_stats(HardwareHandle)
</code></pre>
 
Top