FloKra 958c653cf9 onShutdown.ps1: added check if there is a Windows Update reboot required. As in this case the update will be installed on shutdown, the Tasmota-Powerdown command is then omitted. Requires PSWindowsUpdate to be installed. 2 years ago
..
README.md 958c653cf9 onShutdown.ps1: added check if there is a Windows Update reboot required. As in this case the update will be installed on shutdown, the Tasmota-Powerdown command is then omitted. Requires PSWindowsUpdate to be installed. 2 years ago
onShutdown.ps1 958c653cf9 onShutdown.ps1: added check if there is a Windows Update reboot required. As in this case the update will be installed on shutdown, the Tasmota-Powerdown command is then omitted. Requires PSWindowsUpdate to be installed. 2 years ago

README.md

MQTT-Message on Shutdown/Restart

PowerShell script for Windows 10 to send MQTT messages on Shutdown and Restart.

Intended to switch off a Tasmota plug, delayed, after shutdown, but NOT on restart.

Prerequisites

  • download Mosquitto Windows x64 installer
  • extract and copy mosquitto_pub.exe, mosquitto.dll, libssl-1_1-x64.dll and libcrypto-1_1-x64.dll to c:\Tools\mosquitto (or anywhere and change $mosquittoPubPath accordingly)
  • create folder C:\LOG (or comment out $logFile to disable logging)

Description

This script checks if there is a Event 1074 (source: User32) in the System Event Log, that is less than 15 seconds old. If so, it checks the content of this event, which contains the info if we are about to "power off" or "restart".

The script must be set as shutdown script via Windows Group Policy (local GPO).

V2 now checks if there is a Windows Update reboot required. As in this case the update will be installed on shutdown, the Tasmota-Powerdown command is then omitted. Requires PSWindowsUpdate to be installed.

Setup

  • install PSWindowsUpdate (PS as Admin): Install-Module -Name PSWindowsUpdate
  • copy script to C:\scripts\onShutdown.ps1
  • edit topics, MQTT host, etc.
  • Start -> Run -> gpedit.msc
  • Computer Configuration -> Windows Settings -> Scripts (Startup/Shutdown) -> Shutdown
  • there, go to tab "PowerShell Scripts" and simply browse for this script. Group Policy service seems to handle the PS Execution-Policy, so that it does not have to be set to unrestricted system-wide.

Starting it via Task Scheduler on Event 1074 unfortunately does not work, as the script is getting killed before it actually has done anything most times. Via Group Policy, the shutdown will be delayed until the script has finished its work.

Tasmota

In order to implement the "delayedOff" function, a rule has to be added to the Tasmota device.

Add it via console on Tasmota´s Web Interface:

rule1 on event#delayedOff do RuleTimer1 25 endon on rules#Timer=1 do power1 0 endon

Enable the rule:

rule1 1

In this example, Relais 1 is switched off 25 seconds after the event "delayedOff" was triggered.

Now the delayedOff-function can be triggered via MQTT:

topic:   cmnd/[TasmotaTopic]/event
payload: delayedOff