Laptop state check with Smartthings

Hi All,

I need the blinds to automatically close when I use my laptop so I need to know how to check if my laptop is in use or wake state. Rest of the times, I keep the lid closed so it goes to sleep mode. I tried using the LAN monitor driver by TA Austin but this doesn’t seem to work as I have been playing with this for the past 2 days with no luck. Any advise will be appreciated.

1 Like

Never had Windows in my whole life, but on my Linux box, I’d write a short (max 5 lines) script that checks the state of the lid (somewhere in /dev) and execute a command via the SmartThings CLI (smartthings devices:commands …).

SmartThings CLI is available for Windows and I think there’s something like an init system on Windows that executes scripts when it’s booting up.

Thanks but I am not a developer so was hoping for some sort of a driver to do what I wanted to. Is something possible at all without coding? Even if I were to magically write the script, how can ST call it?

Download and install the SmartThings CLI and run smartthings devices:commandson the command line. You’ll quickly figure out how to turn a light on and off with the CLI. Now create a virtual switch in SmartThings and turn this on and off via the command line. Create routines to do whatever you want when the switch is flipped.

Add the command line to the - how is it called on Windows? - AUTOEXEC.BAT.

Sorry for being brief, but I’m sitting here in the cold in the middle of nowhere.

OK but how it this going to address my requirement. I want to see if my laptop is on or off and then inform ST when it is off or on so I can trigger a routine based on the on or off state?

When your laptop boots, it tells the SmartThings cloud to turn on a virtual switch.

Fingers too cold to type, so I gave ChatGPT the link to this thread and asked it to create a step-by-step guide:

Laptop state check with SmartThings — Step-by-step guide (thread-based + reliable approach)

The reliable way is not “SmartThings tries to detect the laptop” (ping/presence often fails when the laptop sleeps). Instead: the laptop reports its own state to SmartThings by toggling a Virtual Switch.


Overview: what you’ll build

  • A Virtual Switch in SmartThings: Laptop Active

  • Two SmartThings Routines:

    • Laptop Active = ONClose blinds
    • Laptop Active = OFFOpen blinds (optional)
  • On the laptop: small scripts that run automatically on:

    • Unlock / Wake → turn switch ON
    • Lock / Sleep → turn switch OFF

Approach A (recommended): Laptop pushes state to SmartThings via Virtual Switch + SmartThings CLI

1) Create a Virtual Switch: Laptop Active

Option A1: vEdge Creator (most repeatable)

  1. Install vEdge Creator / Virtual Devices via the vEdge Creator community thread (channel link).
  2. In SmartThings app, install the “Virtual Devices” Edge driver.
  3. Add the vEdge Creator controller device (often via “Scan nearby”).
  4. Use it to create one Virtual Switch named: Laptop Active.

Result: You now have a SmartThings device you can turn ON/OFF.

Option A2: SmartThings Labs virtual devices (if available)

Some accounts/regions show a built-in virtual device creator, but it’s inconsistent. If you don’t see it, use vEdge Creator.


2) Install SmartThings CLI on the laptop & sign in

  1. Install SmartThings CLI (Windows MSI, or via package manager on other OS).

  2. Open a terminal and verify:

    smartthings --help
    
  3. The first run typically opens a browser to sign in and authorize.


3) Find the Device ID of Laptop Active

List your devices:

smartthings devices

Copy the UUID (Device ID) for Laptop Active.


4) Test toggling the switch from the laptop

Turn ON:

smartthings devices:commands <DEVICE_ID> main:switch:on

Turn OFF:

smartthings devices:commands <DEVICE_ID> main:switch:off

Example:

smartthings devices:commands 12345678-9012-3456-7890-123456789012 main:switch:on

If this works, the rest is just automation.


5) Create two tiny scripts (Windows PowerShell)

Create a folder, e.g.:
C:\SmartThings\

5.1 ON script — C:\SmartThings\laptop-active-on.ps1

$deviceId = "PUT-YOUR-DEVICE-UUID-HERE"
$cli      = "smartthings"   # or full path to smartthings.exe if needed

& $cli devices:commands $deviceId main:switch:on

5.2 OFF script — C:\SmartThings\laptop-active-off.ps1

$deviceId = "PUT-YOUR-DEVICE-UUID-HERE"
$cli      = "smartthings"

& $cli devices:commands $deviceId main:switch:off

Run them manually once to confirm the switch changes in the SmartThings app.


6) Auto-run the scripts (Windows Task Scheduler)

You can trigger based on Unlock/Lock which usually matches “actively using the laptop” best.

6.1 Turn ON on unlock

  • Open Task Scheduler

  • Create Task

  • TriggersNew… → “On an event”

    • Log: Security
    • Event ID: 4801 (Workstation unlocked)
  • ActionsNew…

    • Program: powershell.exe

    • Arguments:

      -ExecutionPolicy Bypass -File "C:\SmartThings\laptop-active-on.ps1"
      

6.2 Turn OFF on lock

  • Trigger:

    • Log: Security
    • Event ID: 4800 (Workstation locked)
  • Action calls:

    -ExecutionPolicy Bypass -File "C:\SmartThings\laptop-active-off.ps1"
    

Optional: also handle wake/sleep

If you want extra coverage, add triggers for wake/sleep events (these vary by device/Windows power mode). Many people find lock/unlock sufficient.


7) Create the SmartThings routines for blinds

Routine 1: Close blinds when laptop becomes active

  • SmartThings app → RoutinesAdd routine
  • If: Laptop ActiveSwitch turns ON
  • Then: Blinds device → Close
  • Save

Routine 2 (optional): Open blinds when laptop becomes inactive

  • If: Laptop ActiveSwitch turns OFF
  • Then: Blinds device → Open (or set level/position)
  • Save

Troubleshooting checklist

  • Does this work from the laptop terminal?

    smartthings devices:commands <id> main:switch:on
    
  • Are you using the UUID (Device ID), not a list index?

  • In Task Scheduler, does the task succeed when you click Run manually?

  • Do Security events 4800/4801 appear in Event Viewer (some systems need auditing enabled)?

Thanks for patiently answering my questions, I installed smartthings cli in my laptop, created a virtual switch in the ST app and executed these commands in my laptop’s command prompt which turned the virtual switch on and off perfectly

I now need to figure out how to execute the batch script for lid opening and closing and if I can do that via scripts. If I have anything more, I will reach out to you and please extend your help if you can. If I figure this out myself, then I will feed it back in this thread and accept the solution to close this thread.

See the step-by-step guide above.

Edit: since I’ve never used a Windows computer, I had to ask ChatGPT how to execute scripts on boot and on shut down - it would be so easy on Linux…


Boot/Shutdown → SmartThings Virtual Switch (Windows)

0) Prerequisites (same as before)

  • You already have a SmartThings virtual switch named Laptop Active

  • SmartThings CLI is installed and you can run:

    smartthings devices:commands <DEVICE_ID> main:switch:on
    smartthings devices:commands <DEVICE_ID> main:switch:off
    

1) Create the two PowerShell scripts

Create folder:
C:\SmartThings\

1.1 ON at boot — C:\SmartThings\laptop-boot-on.ps1

$deviceId = "PUT-YOUR-DEVICE-UUID-HERE"
$cli      = "smartthings"

& $cli devices:commands $deviceId main:switch:on

1.2 OFF at shutdown — C:\SmartThings\laptop-shutdown-off.ps1

$deviceId = "PUT-YOUR-DEVICE-UUID-HERE"
$cli      = "smartthings"

& $cli devices:commands $deviceId main:switch:off

Test both manually once.


2) Turn ON when Windows boots (Task Scheduler)

  1. Open Task Scheduler

  2. Create Task… (not “Basic Task”)

  3. General

    • Name: SmartThings - Laptop boot ON
    • Check: Run whether user is logged on or not
    • Check: Run with highest privileges
  4. TriggersNew…

    • Begin the task: At startup
  5. ActionsNew…

    • Action: Start a program

    • Program/script: powershell.exe

    • Add arguments:

      -NoProfile -ExecutionPolicy Bypass -File "C:\SmartThings\laptop-boot-on.ps1"
      
  6. Conditions

    • (Recommended) Uncheck Start the task only if the computer is on AC power if you want it on battery too
  7. Settings

    • Check Run task as soon as possible after a scheduled start is missed
    • If it fails, you can set Restart every 1 minute, Attempt 3 times

:white_check_mark: This will turn the virtual switch ON right after boot.


3) Turn OFF when Windows shuts down

Shutdown is trickier: Windows doesn’t guarantee long-running tasks at the exact moment of power-off. The most reliable method is using a shutdown event trigger in Task Scheduler and keeping the task tiny.

Option 3A (recommended): Trigger on shutdown event (Event Log)

  1. Create Task…

  2. General

    • Name: SmartThings - Laptop shutdown OFF
    • Run whether user is logged on or not
    • Run with highest privileges
  3. TriggersNew…

    • Begin the task: On an event

    • Log: System

    • Source: User32

    • Event ID: 1074

      • This event is commonly logged when a shutdown/restart is initiated (user/app/system request).
  4. Actions

    • Program: powershell.exe

    • Arguments:

      -NoProfile -ExecutionPolicy Bypass -File "C:\SmartThings\laptop-shutdown-off.ps1"
      
  5. Settings

    • Ensure it’s allowed to run on demand
    • Keep it fast (no delays)

:white_check_mark: In many setups, this flips the switch OFF when shutdown starts.

Option 3B (fallback): “On shutdown” via Group Policy script

If Event ID 1074 doesn’t fire in your shutdown flow (some devices/fast startup modes), use a shutdown script:

  1. gpedit.msc

  2. Computer Configuration → Windows Settings → Scripts (Startup/Shutdown)

  3. Shutdown → Add:

    • Script: powershell.exe

    • Parameters:

      -NoProfile -ExecutionPolicy Bypass -File "C:\SmartThings\laptop-shutdown-off.ps1"
      

This runs as the machine shuts down (still subject to Windows time limits, so keep it instant).


4) SmartThings routines (same idea)

  • If Laptop Active ON → close blinds
  • If Laptop Active OFF → open blinds (optional)

Notes / gotchas

  • Fast Startup / Hybrid shutdown can affect what counts as “shutdown” vs “hibernate”. If your OFF trigger seems flaky, consider disabling Fast Startup:

    • Control Panel → Power Options → Choose what the power buttons do → uncheck Turn on fast startup
  • If you need “OFF when sleeping/hibernating”, that’s separate from shutdown.


Many thanks Andreas, I successfully set up the unlock event an hour ago but trying to break my heads on the lock event for the past one hour as it does not fire. The events for unlock and lock are 4624 and 4634 respectively so changed this one in the task scheduler > trigger. Also, changed the ps1 file to match the screenshot I sent in my previous post and it works perfectly fine for unlock only. When I close the lid, the lock does not fire but actually goes into a hold state and when I open the lid, both the lock and the unlock fires.

See my previous comment from two hours ago for boot/shutdown.

Yeah saw that but don’t want it to fire on boot or shutdown. Just want to fire it on lock as the script does not run since the system is going into lock state

Well… Your laptop goes immediately to sleep when you close the lid. No time to execute a script.

Yup so any suggestions? When does the lock script fire?

But I’m sure there’s a different event you could use to execute the script.

One example is when the computer is locked. So you have to lock (logout?) first, before you close the lid - as already mentioned above:

When I looked at the event viewer, I can see logoff as 4634 and logon as 4624 which is what I used but only the logon works fine but this also gets overridden by the logoff event that does not fire when the laptop is logged out or I press Win + L

I repeat myself:

Edit: that’s why I hate Windows. Let’s ask the AI - seems to be quite useful…

Absolutely — here’s the “extended” Windows version that covers sleep / hibernate / Modern Standby in addition to your boot = ON and shutdown = OFF requirement.

You can choose the policy you want:

  • Policy 1 (pure power-state):
    ON = boot / resume, OFF = shutdown / sleep / hibernate
  • Policy 2 (strictly boot/shutdown only):
    ON = boot, OFF = shutdown, ignore sleep/hibernate
    (Still useful to detect sleep/modern-standby for troubleshooting.)

Below I’ll give you Policy 1, and I’ll point out what to skip if you want Policy 2.


SmartThings Virtual Switch from Windows power events (Boot / Shutdown / Sleep / Hibernate / Modern Standby)

Prerequisites (quick)

  • You have a SmartThings virtual switch (e.g. Laptop Active)

  • SmartThings CLI works manually:

    smartthings devices:commands <DEVICE_ID> main:switch:on
    smartthings devices:commands <DEVICE_ID> main:switch:off
    

1) Make the command as fast as possible

Recommended: call smartthings.exe directly (no PowerShell at shutdown)

This reduces “startup overhead” right when Windows is trying to power off.

Create two tiny .cmd files (fast to launch):

C:\SmartThings\st-laptop-on.cmd

@echo off
smartthings devices:commands <DEVICE_ID> main:switch:on

C:\SmartThings\st-laptop-off.cmd

@echo off
smartthings devices:commands <DEVICE_ID> main:switch:off

If smartthings is not in PATH, use the full path to smartthings.exe.


2) Boot → switch ON (reliable)

  1. Task Scheduler → Create Task…

  2. General

    • Name: ST Laptop ON (Startup)
    • :white_check_mark: Run whether user is logged on or not
    • :white_check_mark: Run with highest privileges
  3. TriggersNew…

    • Begin the task: At startup
    • Optional: Delay task for 30 seconds (helps if Wi-Fi takes a moment)
  4. ActionsNew…

    • Action: Start a program
    • Program: C:\SmartThings\st-laptop-on.cmd
    • Start in: C:\SmartThings\

3) Shutdown → switch OFF (best “planned shutdown” trigger)

3A) OFF on shutdown/restart initiation (Event ID 1074)

This is the common “shutdown initiated” event.

  1. Task Scheduler → Create Task…

  2. General

    • Name: ST Laptop OFF (Shutdown 1074)
    • :white_check_mark: Run whether user is logged on or not
    • :white_check_mark: Run with highest privileges
  3. TriggersNew…

    • Begin the task: On an event
    • Log: System
    • Source: User32
    • Event ID: 1074
  4. Actions

    • Program: C:\SmartThings\st-laptop-off.cmd
    • Start in: C:\SmartThings\

Important limitation

This won’t fire on hard power loss / crash (nothing can). Microsoft troubleshooting differentiates expected vs unexpected shutdowns (e.g. unexpected reboot/shutdown events).


4) Sleep / Hibernate (classic) — optional OFF/ON

4A) Entering sleep/hibernate → OFF (Kernel-Power 42)

Event ID 42 is widely used for “system is entering sleep.”

Create task:

  • Name: ST Laptop OFF (Sleep 42)

  • Trigger: On an event

    • Log: System
    • Source: Microsoft-Windows-Kernel-Power
    • Event ID: 42
  • Action: C:\SmartThings\st-laptop-off.cmd

4B) Resuming from sleep/hibernate → ON (Kernel-Power 107)

Event ID 107 appears as “system has resumed from sleep.”

Create task:

  • Name: ST Laptop ON (Resume 107)

  • Trigger:

    • Log: System
    • Source: Microsoft-Windows-Kernel-Power
    • Event ID: 107
  • Action: C:\SmartThings\st-laptop-on.cmd

Alternative resume trigger (also common): Power-Troubleshooter 1

Many people trigger “wake” tasks using Power-Troubleshooter Event ID 1.

  • Source: Microsoft-Windows-Power-Troubleshooter
  • Event ID: 1

Note: Some users report Power-Troubleshooter triggers aren’t always consistent on every system/power mode, so Kernel-Power 107 is often the simplest if it appears reliably for you.


5) Modern Standby (S0 “Connected Standby”) — optional OFF/ON

On Modern Standby systems, you may see Kernel-Power 506/507 for entering/exiting Modern Standby.

5A) Enter Modern Standby → OFF (Kernel-Power 506)

  • Source: Microsoft-Windows-Kernel-Power
  • Event ID: 506
  • Action: st-laptop-off.cmd

5B) Exit Modern Standby → ON (Kernel-Power 507)

  • Source: Microsoft-Windows-Kernel-Power
  • Event ID: 507
  • Action: st-laptop-on.cmd

6) If shutdown tasks are flaky: disable Fast Startup (highly recommended)

Fast Startup (hybrid shutdown) can make “shutdown” behave more like hibernate and can interfere with shutdown semantics. Disabling it often improves predictability.

Disable via Registry

Set HiberbootEnabled to 0.

reg add "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Power" /v HiberbootEnabled /t REG_DWORD /d 0 /f

Reboot once afterward.


7) Choose your policy

Policy 1 (recommended if blinds should react to “laptop not running”)

Create all tasks:

  • Startup → ON
  • Shutdown 1074 → OFF
  • Sleep 42 → OFF
  • Resume 107 → ON
  • Modern Standby 506 → OFF
  • Modern Standby 507 → ON

Policy 2 (your original strict requirement)

Create only:

  • Startup → ON
  • Shutdown 1074 → OFF
    …and skip sections 4 and 5.

8) Quick “what events does my laptop actually log?”

Event Viewer → Windows Logs → SystemFilter Current Log…

Check these sources/IDs and see which appear on your machine:

  • User321074 (planned shutdown/restart)
  • Microsoft-Windows-Kernel-Power42 (enter sleep), 107 (resume)
  • Microsoft-Windows-Power-Troubleshooter1 (returned from low power state)
  • Microsoft-Windows-Kernel-Power506/507 (Modern Standby enter/exit)
1 Like

Many thanks for your research and help. I created two events, one to fire ‘at event’ (laptop on cmd) and the other to fire ‘at workstation lock’ (laptop off cmd) and both works flawlessly. The VS turns on and off perfectly in ST and my routines are also triggered correctly so happy for now.

The shutdown (event is 1074 in my event viewer) and sleep (506 in my event viewer) does not work so need to explore this when I get time and will post it in this thread if I find a solution.

1 Like

Finally, after racking my brains and exploring multiple options for the past couple of days, I found a solution and am now sharing it in this forum in case anyone has the same need,

  1. I installed Smartthings Cli
  2. In Windows 11, I specified ‘Do nothing’ in the power options when I close the lidimage
  3. I also set the sleep time to be 5 min from idle state in the power management
  4. I created a virtual switch in ST called VS - Laptop
  5. I created two .cmd files in C:/Smartthings; laptop-on and laptop-off to turn on and turn off VS-Laptop respectively. I also added the lock workstation in the the laptop-off code and it looks like this @echo off
    smartthings devices:commands switch:off
    rundll32.exe user32.dll, LockWorkStation
  6. The laptop-on code is this @echo off
    smartthings devices:commands switch:on
  7. I created 3 tasks in the windows task scheduler,
  • Laptop-Idle - Triggers when the laptop enters idle state (normally takes around 3 min) and the action is set to execute the laptop-off.cmd file from C:/Smartthings folder. Also, it’s important to specify the conditions correctly for this task as shown below

  • Laptop-On - Triggers on event as shown below and the action is set to execute the laptop-on.cmd file in C:/Smartthings folder after 10 seconds (delay task option)

  • Laptop-Lock - Triggers when you lock the laptop or press Win + L and the action is set to execute the laptop-off.cmd in C:/Smartthings folder. Instead of Lock, if you want to get the laptop to sleep straightaway, then replace this command ‘rundll32.exe user32.dll, LockWorkStation’ with ‘rundll32.exe powrprof.dll,SetSuspendState 0,1,0’. You have to specify one or the other for the VS-Laptop to turn on correctly

  • In all the 3 tasks, below is the general tab:

I don’t shutdown my laptop so this is good for me. I have also tested this enough to ensure it works flawlessly so if anyone tries this and needs to tap into my experience, then please ask and I will be happy to share my thoughts or suggest some ideas. Thanks to @Andreas_Roedl for giving me a head start on this.