Sunday, June 24, 2018

Use Windows WMI and Powershell to send data to Homeseer

For instance if you want to sent the average temperature or the CPU cores on your Windows PC to Homeseer virtual devices so you can trigger and action on temperature too high.
Create a user to use for updating if you do not already have one

Create a virtual device in Homeseer similar to this
Be sure and uncheck  "Do not update device last change time if device value does not change:"


Note -1 is set by script if it encounters and error getting data for the virtual

Create a script like the script below replacing
10.10.1.45 with your Homeseer IP address
wmi with the user you created
wmiPass with the password for the above user.
4570 with the ref ID of the virtual device you created

function Get-Temperature {
    $t = @( Get-WmiObject MSAcpi_ThermalZoneTemperature -Namespace "root/wmi" )
    $cores = 0
    $tempTotal=0
    foreach ($temp in $t)
    {
        $CORES += 1
        $currentTempKelvin = $temp.CurrentTemperature / 10
        $currentTempCelsius = $currentTempKelvin - 273.15
        $tempTotal += $currentTempCelsius 
        $currentTempFahrenheit = (9/5) * $currentTempCelsius + 32

        #write ($currentTempCelsius.ToString() + " C : " + $currentTempFahrenheit.ToString() + " F : " + $currentTempKelvin + "K")

    }
    return $tempTotal / $cores
}

$avgTemp=Get-Temperature


$url="http://10.10.1.45/JSON?user=wmi&pass=wmiPass&request=controldevicebyvalue&ref=4570&value=" + $avgTemp
#write ("avgTemp="+$avgTemp)
#write ("url="+$url)

(New-Object System.Net.WebClient).DownloadString($url);


My script is called cpuTemp.ps1 and is in C:\diags so I set up an task to run every hour (you can make it as often as every 5 minutes) like this.


Plus you probably want to set these

For more of a rounder picture with logging, here is a fuller script.



Which looks like this


When I deployed to my second PC I found not even the Administrator could run scripts and had to set its policy to RemoteSigned and unblock the file.
PS C:\> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
PS C:\> Unblock-File -Path "C:\diags\cpuTemp.ps1"
See this Microsoft doc for more info on that.

Also the temperature readings are the same and unchanging on both PCs I've deployed to so far so they might not be useful despite all the posts out there claiming they are.

5 comments:

  1. Change to show "Do not update device last change time if device value does not change:" unchecked.

    ReplyDelete
  2. Add pending updates and log monitoring

    ReplyDelete
  3. Updated script to include sending the CPU load of a Blue Iris console (similar can be used for any process) to monitor if it is hung. This can happen if the total CPU usage is at 100% for too long.

    ReplyDelete
  4. Moved script to my Homeseer stuff github repo and put link to it here instead

    ReplyDelete