6

So I am trying to access the data from here

in Python. As you can see, it uses wmi. I have tried to use wmi in python before but I am having trouble interpreting the data they are giving me. Please be patient with me as I am a noob to how wmi works. It says that the wmi data is stored in root/OpenHardwareMontor and that it uses two different wmi classes(Hardware and Sensor). But all this information is going over my head.

could someone please give me some sample code to read some data from this?

For example, the code to check cpu core 1 frequency.

EDIT: i have sort of got it working. i run this code:

for Temperature in c.sensor():
    print Temperature.identifier
    print Temperature.value

and i get this:

/hdd/0/load/0
37.6608924866
/intelcpu/0/temperature/1
53.0
/intelcpu/0/temperature/0
42.0
/ram/data/1
2.88324356079
/intelcpu/0/load/2
1.53846144676
/hdd/0/temperature/0
43.0
/intelcpu/0/load/0
2.30768918991
/intelcpu/0/clock/1
1463.29663086
/intelcpu/0/clock/0
133.02696228
/intelcpu/0/clock/2
1463.29663086
/ram/load/0
49.224521637
/ram/data/0
2.79517364502
/intelcpu/0/load/1
3.07692289352

how can i request only the value associated with the identifier /intelcpu/0/temperature/1 ignoring all other values?

ryan27968
  • 437
  • 3
  • 7
  • 14

1 Answers1

7

The most simple example to use WMI:

c = wmi.WMI()
wql = "Select * From Win32_SerialPort"
for item in c.query(wql):
    print item

Output Example:

instance of Win32_SerialPort
{
    Availability = 2;
    Binary = TRUE;
    Caption = "SpectrumAnalyzer1 (COM15)";
    ConfigManagerErrorCode = 0;
    ConfigManagerUserConfig = FALSE;
    CreationClassName = "Win32_SerialPort";
    Description = "SpectrumAnalyzer1";
    DeviceID = "COM15";
    MaxBaudRate = 128000;
    MaximumInputBufferSize = 0;
    MaximumOutputBufferSize = 0;
    Name = "SpectrumAnalyzer1 (COM15)";
    OSAutoDiscovered = TRUE;
    PNPDeviceID = "USB\\VID_10C4&PID_ED00\\1269376";
    PowerManagementCapabilities = {1};
    PowerManagementSupported = FALSE;
    ProviderType = "RS232 Serial Port";
    SettableBaudRate = TRUE;
    SettableDataBits = TRUE;
    SettableFlowControl = TRUE;
    SettableParity = TRUE;
    SettableParityCheck = TRUE;
    SettableRLSD = TRUE;
    SettableStopBits = TRUE;
    Status = "OK";
    StatusInfo = 3;
    Supports16BitMode = FALSE;
    SupportsDTRDSR = TRUE;
    SupportsElapsedTimeouts = TRUE;
    SupportsIntTimeouts = TRUE;
    SupportsParityCheck = TRUE;
    SupportsRLSD = TRUE;
    SupportsRTSCTS = TRUE;
    SupportsSpecialCharacters = TRUE;
    SupportsXOnXOff = TRUE;
    SupportsXOnXOffSet = TRUE;
    SystemCreationClassName = "Win32_ComputerSystem";
    SystemName = ".......";
};

You can access each item by:

myQuery = c.query(wql)
myQuery.Availability 

Output:

2

For more information, try the WMI cookbook.

Edit #1:

Using if statements and in you can do what you want.

for Temperature in c.sensor():
    if "/intelcpu/0/temperature/1" in Temperature.identifier:
        print Temperature.identifier
        print Temperature.value 
nelsonjchen
  • 365
  • 6
  • 16
Kobi K
  • 7,743
  • 6
  • 42
  • 86
  • ok i semi-figured-it-out using the "cockbook". i ran this code: for Temperature in c.sensor(): print Temperature.name print Temperature.value and it worked, but it printed out every single temperature sensor name/value. how can i grab a single value only? – ryan27968 Jan 12 '14 at 14:28
  • @user1803425 Can you add the output to your post? – Kobi K Jan 12 '14 at 14:30
  • when i ran for Temperature in c.sensor(): print Temperature.name print Temperature.value it returned Used Space 37.6607666016 CPU Core #2 56.0 CPU Core #1 46.0 Available Memory 2.88618087769 CPU Core #2 2.30768918991 Temperature 42.0 CPU Total 2.30768918991 CPU Core #1 1463.296875 Bus Speed 133.026992798 CPU Core #2 1463.296875 Memory 49.1727905273 Used Memory 2.79223632812 CPU Core #1 2.30768918991 – ryan27968 Jan 12 '14 at 14:35
  • It's hard to understand, can you clearfy better? add the data to your post (using edit) not as a comment. – Kobi K Jan 12 '14 at 14:48
  • ok. i have added the information to my post. does that help clarify what i am saying? – ryan27968 Jan 12 '14 at 15:03
  • @user1803425 you can see my edit.... you need to read more about `strings` and `in` at python... – Kobi K Jan 12 '14 at 15:07