skip to Main Content

Monitor & Visualize Your SmartThings Smart Home with Splunk

The smart home has gone through quite a convergence in the last few years. Modern protocols like Z-Wave & ZigBee, along with mart hubs, and smart assistants like Amazon Alexa, Google Home & Apple Siri are finally bringing everything together to make the smart home a practical and reliable reality.

What had been still missing from the picture for me, is the ability to log, analyze, and visualize all the data that my smart home generated. I use Splunk (data capture and visualization tool) at work so I decided to give it a try at home and it’s worked out great.

Here’s a Splunk dashboard I created for my home, showing current and historical data from multiple data sources: energy meter, contact sensors, switches, weather data feed, Windows event logs, and some custom PowerShell scripts.

My SmartThings-based smart home setup:

  • Samsung SmartThings Hub 2nd Gen
  • Amazon Echo Devices
  • Various ZigBee/Z-Wave devices
    • Samsung SmartThings GP-U999SJVLAAA Door & Window Multipurpose Sensors
    • Samsung SmartThings GP-U999SJVLBAA Motion Sensors
    • Samsung F-OUT-US-2 SmartThings Outlets
    • Other ZigBee/Z-Wave switches, dimmers, and plugs
    • Samsung ST-CEN-MOIS-1/FTR-US-2 Water Leak Sensors
    • Aeotec HEM G2 whole house energy monitor
    • First Alert ZCOMBO 2-in-1 Smoke Detector & Carbon Monoxide Alarm, Z-Wave
  • PowerShell scripts to pull data from openweathermap.org & run/log periodic Internet speed tests.
  • Splunk Free

Installing Splunk Free Edition

Download and install Splunk. You will start with the Enterprise version which comes with a 60-Day Trial. After that you can switch to the Free edition. Splunk Free allows indexing up to 500 MB of data per day which has been sufficient for my home logging needs. For my setup I installed Splunk on a 14-year old Windows box with a Intel Core2 Quad CPU Q6600 @2.40GHz – Splunk indexing/query performance has been pretty acceptable.

If your install was successful, you should be able to log into Splunk web by navigating to http://localhost:8000 (or replace localhost with your Splunk server hostname).

If you want to monitor other computers, install Splunk Universal Forwarder on each of those computers. I’ll go through how to configure the Universal Forwarders in a future post.

Read more

Posting a feed to Google Search Appliance (GSA) from PowerShell

PowerShell code to submit a feed to Google Search Appliance:

$dataSource = "MyFeed" # replace this with your datasource name
$gsaUrl = "http://testsearch.example.com:19900/xmlfeed"; # replace this with your GSA URL

$xml = @"
$dataSource incremental
"@
   
Write-Output "Submitting feed to $gsaUrl."

$body = "feedtype=metadata-and-url&datasource=$dataSource&data=$xml"
$contentType = "multipart/form-data"
$length = $body.Length

Write-Output "Posting feed to GSA:"
Write-Output $xml

$r=Invoke-WebRequest -uri $gsaUrl -method POST -body $body -ContentType $contentType -UserAgent "Ps-GsaFeedSubmitter"

See also Feeds Protocol Developer’s Guide

Finding the Last Friday (or any weekday) of the Month in PowerShell

If there are less than 7 days left in the month, then it’s the last weekday of this type in this month.

function Is-LastWeekDayOfMonth([DateTime] $d) {
    return [DateTime]::DaysInMonth($d.Year, $d.Month) - $d.Day -lt 7
}

Similarly, finding the last specific weekday of the month involves finding the last weekday, and the difference between that weekday and the target weekday:

function Get-LastFridayOfMonth([DateTime] $d) {
    $lastDay = new-object DateTime($d.Year, $d.Month, [DateTime]::DaysInMonth($d.Year, $d.Month))
    $diff = ([int] [DayOfWeek]::Friday) - ([int]
    $lastDay.DayOfWeek)
    if ($diff -ge 0) {
        return $lastDay.AddDays(- (7-$diff))
    }
    else
    {
        return $lastDay.AddDays($diff)
    }
}

Running MSDEPLOY from PowerShell

I had to spend quite a bit of time figuring this out. So hopefully this will help someone out there. To launch MSDEPLOY from PowerShell, make sure you escape any quote or comma characters.

The “2>&1” at the end allows Powershell to detect errors and bail out if you have $ErrorActionPreference = “Stop”

If you still have problems, download and use EchoArgs.exe to see exactly what PowerShell sees.

Example

function Get-MSWebDeployInstallPath(){
  $path = (get-childitem "HKLM:\SOFTWARE\Microsoft\IIS Extensions\MSDeploy" | Select -last 1).GetValue("InstallPath")
  $path = "${path}msdeploy.exe"

  if (!(Test-Path "$path")) {
    throw "MSDEPLOY.EXE is not installed. See http://go.microsoft.com/?linkid=9278654"
  }

  return $path
}

$msdeploy = Get-MSWebDeployInstallPath

& $msDeploy -verb:sync -source:recycleApp `
  -dest:recycleApp=`"${webSiteName}`"`,recycleMode=StopAppPool`,wmsvc=${server}`,userName="${userName}"`,password="${password}" `
  -allowuntrusted -debug 2>&1

Give The Power of Speech and Sound to Your PowerShell Scripts

Do you ever have the problem where you start a long running script (such as running a code build), multi-task on something else on another monitor while waiting for the script to finish, and then totally forget about the script until half an hour later? Well, here’s a solution your problem: have your script give you holler at you when it’s done.

Hello sir! I am done running your command!

In my library script file, I have the following functions to play sound files and to speak any text:

function PlayMp3($path) {
    # Use the default player to play. Hide the window.
    $si = new-object System.Diagnostics.ProcessStartInfo
    $si.fileName = $path
    $si.windowStyle = [System.Diagnostics.ProcessWindowStyle]::Hidden
    $process = New-Object System.Diagnostics.Process
    $process.startInfo=$si
    $process.start() 
}

function PlayWav($path) {
    $sound = new-Object System.Media.SoundPlayer;
    $sound.SoundLocation="$path";
    $sound.Play();
}

function Say($msg) {
    $Voice = new-object -com SAPI.SpVoice
    $Voice.Speak($msg, 1 )
}

If you like the text-to-speech feature but find Windows’ speech engine lacking, check out Ivona. It’s a commercial text-to-speech engine but you are allow to generate and download short speech files for free personal use. Now, my script can nicely interrupt me to tell me when it’s done. Other online text-to-speech engines: vozMe, SpokenText.

If Making Noise Is Not Your Thing

If making noise is not your thing, consider displaying a message in the Notification Area. Here’s the code (courtesy Microsoft TechNet):

Build complete!
function Get-ScriptName {
    $MyInvocation.ScriptName
} 

function DisplayNotificationInfo($msg, $title, $type) {
    # $type - "info" or "error"
    if ($type -eq $null) {$type = "info"}
    if ($title -eq $null) {$title = Get-ScriptName}

    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    $objNotifyIcon = New-Object System.Windows.Forms.NotifyIcon 
    # Specify your own icon below
    $objNotifyIcon.Icon = "C:CdoScriptsFolder.ico"
    $objNotifyIcon.BalloonTipIcon = "Info"  
    $objNotifyIcon.BalloonTipTitle = $title
    $objNotifyIcon.BalloonTipText = $msg

    $objNotifyIcon.Visible = $True 
    $objNotifyIcon.ShowBalloonTip(10000)
}
Back To Top