New PowerShell module for Home Assistant

So after making the PowerShell script for Home Assistant I decided to spend a hour and pull it together to be a full module and push it up to GitHub. This is now in place and support just turning a switch entity on and off.


Install-Module -Name PoshHomeAssistant -Scope CurrentUser


Before you can use it set the configuration, this can be done with the following command, set the key and url to match your environment.

Set-HomeAssistantConfiguration -AccessKey "YourAccessKey" -BaseUrl ""

To turn a light on and off run the following commands, they will return the state of the related entities for you to use if needed.

Set-HomeAssistantSwitchState -Entity "switch.2studylamp" -State On
Set-HomeAssistantSwitchState -Entity "switch.2studylamp" -State Off

Related Article: Getting PowerShell to work with Home Assistant

Getting PowerShell to work with Home Assistant

I wanted to turn on and off my office lamp from my desktop, sure I can use a switch but it is hooked up to Home Assistant so lets make it automatic! The main scenario was to toggle it at 10pm so when I get distracted it will let me know the time. You can use this for just about anything and at some point I will add more functions. Here is the snippet for now. You can set the values of global in your profile or in the launching script. As long as they are set this will work.


Just past this into a file and run after updating the password, URL for your Home Assistant installation and then the entity id.

function Set-HomeAssistantAccess($AccessKey, $BaseUrl) {
    $Global:HomeAssistantAccessKey = $AccessKey
    $Global:HomeAssistantBaseUrl = $BaseUrl
    $Global:HomeAssistantHeader = @{ "x-ha-access" = $AccessKey; "Content-Type" = "application/json"}

function Set-HomeAssistantSwitchState {
    param (
        [String] $Entity,
        [ValidateSet('On', 'Off')]
        [String] $State

    $body = @{entity_id = $Entity} | ConvertTo-Json
    switch ($State) {
        "On" { Invoke-RestMethod -Method POST -Uri "$Global:HomeAssistantBaseUrl/services/switch/turn_on" -Headers $Global:HomeAssistantHeader -Body $body }
        "Off" { Invoke-RestMethod -Method POST -Uri "$Global:HomeAssistantBaseUrl/services/switch/turn_off" -Headers $Global:HomeAssistantHeader -Body $body }
        Default {}

Set-HomeAssistantAccess -AccessKey "youraccesskey" -BaseUrl "https://yourdomainorip/api"
Set-HomeAssistantSwitchState -Entity "switch.yourlightentityid" -State On
Set-HomeAssistantSwitchState -Entity "switch.yourlightentityid" -State Off

Home Assistant

iPhone hotspot issues? Getting it to work with windows.

Had an issue the iPhone hotspot would not work on windows, it would see the network and try to connect, on the phone the hotspot bar at the top would appear and disappear. Simple solution… Go to settings and change the name of your iPhone and remove any single quotes (‘) from the name. Go back and turn the hotspot on and enjoy the connection. Well it worked on my computer :)

Getting a DietPi image running on Hyper-V

Why? Because it is faster to test installs and adding items to the DietPi installer with snapshots! Also it makes a great small Linux installation to run other items off in Hyper-V.

  1. Go get the VirtualBox VMWare x64 image from the DietPi site.
  2. Download the QEMU disk image utility from
  3. Extract the VirtualBox VMWare image, it is a ova file. At this point we need to take the actual disk image out of this file. I use 7zip for this purpose. Right click on the ova file and open archive. Then extract the VMDK file out.
  4. Extract the QEMU disk image utility to the same folder as the VMDK file and then run the following command changing the file names as needed:

.\qemu-img.exe convert ‘.\DietPi_v120_VirtualBox-x86_64-(Jessie)-disk1.vmdk’ -O vhdx -o subformat=dynamic ‘.\DietPi_v120_VirtualBox-x86_64-(Jessie)-disk1.vhdx’

.\qemu-img.exe convert “dietpi_vm.vmdk” -O vhdx -o subformat=dynamic ‘.\DietPi_VMWare-x86_64-Stretch.vhdx’

Now you need to open up Hyper-V and make a new VM using the image you just extracted. You may want to keep a copy around to make more images in the future. Related Posts: HA on Pine64 with DietPi

2018/01/30 - Update

Updated to use the VMWare image.

$zipexe = ‘C:\Program Files\7-Zip\7z.exe’
$qemuZip = “”

Push-Location $env:TEMP
if(-not (Test-Path “dietpiimage”)) {
New-Item “dietpiimage” -Type Directory

Push-Location “dietpiimage”

Remove-Item * -Exclude @(‘.7z’,’.zip’)

if(-not (Test-Path $qemuZip)) {
Invoke-WebRequest -Uri “$qemuZip" -OutFile $qemuZip
& $zipexe x $qemuZip

### Image download and conversion

$imageName = “DietPi_VMWare-x86_64-Stretch”

if(-not (Test-Path “$imageName.7z”)) {
Invoke-WebRequest -Uri “$imageName.7z" -OutFile “$imageName.7z”
& $zipexe x “$imageName.7z”

$disk = (Get-ChildItem *.vmdk)[0]
.\qemu-img.exe convert “$disk” -O vhdx -o subformat=dynamic “.\$imageName.vhdx”

Write-Host “$imageName.vhdx is ready!”

2018/07/01 - Stretch Update

Here is a PowerShell script to make it faster.

Push-Location $env:TEMP
if(-not (Test-Path “dietpiimage”)) {
New-Item “dietpiimage” -Type Directory

Push-Location “dietpiimage”

Remove-Item * -Exclude @(‘.7z’,’.zip’)

if(-not (Test-Path “DietPi_VirtualBox-x86_64-(Stretch).7z”)) {
Invoke-WebRequest -Uri “\_VirtualBox-x86\_64-(Stretch).7z" -OutFile “DietPi_VirtualBox-x86_64-(Stretch).7z”

if(-not (Test-Path “”)) {
Invoke-WebRequest -Uri “\_3\" -OutFile “”

$zipexe = ‘C:\Program Files\7-Zip\7z.exe’
& $zipexe x “DietPi_VirtualBox-x86_64-(Stretch).7z”
& $zipexe x “”

Rename-Item (Get-ChildItem *.ova)[0] “DietPi_VirtualBox-x86_64-(Stretch)_OVA.tar”
& $zipexe x “DietPi_VirtualBox-x86_64-(Stretch)_OVA.tar”

$disk = (Get-ChildItem *.vmdk)[0]
.\qemu-img.exe convert “$disk” -O vhdx -o subformat=dynamic ‘.\DietPi_VirtualBox-x86_64-(Stretch)-disk1.vhdx’

Write-Host “DietPi_VirtualBox-x86_64-(Stretch)-disk1.vhdx is ready!”

2017/09/20 - Update

Here is a PowerShell script to make it faster.

Push-Location $env:TEMP
if(-not (Test-Path “dietpiimage”)) {
New-Item “dietpiimage” -Type Directory

Push-Location “dietpiimage”

Remove-Item * -Exclude @(‘.7z’,’.zip’)

if(-not (Test-Path “DietPi_VirtualBox-x86_64-(Jessie).7z”)) {
Invoke-WebRequest -Uri “\_VirtualBox-x86\_64-(Jessie).7z" -OutFile “DietPi_VirtualBox-x86_64-(Jessie).7z”

if(-not (Test-Path “”)) {
Invoke-WebRequest -Uri “\_3\" -OutFile “”

$zipexe = ‘C:\Program Files\7-Zip\7z.exe’
& $zipexe x “DietPi_VirtualBox-x86_64-(Jessie).7z”
& $zipexe x “”

Rename-Item (Get-ChildItem *.ova)[0] “DietPi_VirtualBox-x86_64-(Jessie)_OVA.tar”
& $zipexe x “DietPi_VirtualBox-x86_64-(Jessie)_OVA.tar”

$disk = (Get-ChildItem *.vmdk)[0]
.\qemu-img.exe convert “$disk” -O vhdx -o subformat=dynamic ‘.\DietPi_VirtualBox-x86_64-(Jessie)-disk1.vhdx’

Write-Host “DietPi_VirtualBox-x86_64-(Jessie)-disk1.vhdx is ready!”

Getting HP Officejet Pro 8620 scan to network working...

So, tried to do this. Failed… Why? not because there was a issue with the share or the user/pass combination. It was the SMB process and stack locations in the I/O request packets (IRPs) that are used by Windows. Reference:

To fix

To increase IRPStackSize for the server:

  1. Click Start, click Run, type regedit, and then click OK.
  2. Locate and then click to select the following registry subkey: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Lanmanserver\Parameters
  3. On the Edit menu, point to New, and then click DWORD Value.
  4. Type IRPStackSize, and then press Enter.
  5. Right-click IRPStackSize, and then click Modify.
  6. In the Value data box, ensure decimal is selected and put in 20. Increase by 2 until it works. I ended up using 28.

Restart the server service to test or restart your PC. The process restart is faster :)

Developing Home Assistant on Windows using the Subsystem for Linux

I love Home Assistant and use it to control my house. I run it on a Pine64 board and it handles everything. However for developing and modifying it I want to have a slightly better environment than nano and a console screen :) So using the Subsystem for Linux I have a Linux based setup to run the hass instance in and can use Visual Studio code to develop the modules as I go, best of both worlds.

Installing the Windows Subsystem for Linux

To install the subsystem go to add/remove programs and tick the Windows Subsystem for Linux (Beta) then run bash.exe and this will start the installation, this may take a while as it pulls down the bits. Once completed and you restarted you can run bash again and then you are ready to start. [caption id=”attachment_1213” align=”aligncenter” width=”534”]Installing the Windows Subsystem for Linux Installing the Windows Subsystem for Linux[/caption]

Setting up the Home Assistant development environment

The page has lots of great information but here is how I do it condensed into a single process. In windows I have a dev folder under my user profile. This is c:\Users\myaccount\dev, in the bash console it is mapped to /mnt/c/Users/myaccount/dev/. This way you can work on the files in windows and run them in Linux, just watch out for a CR ;) Run the following in the bash terminal and replace the myaccount with your windows account and myrepo with your fork.

# ensure linux is up to date.
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install git

# make local repo
mkdir /mnt/c/Users/myaccount/dev/
mkdir /mnt/c/Users/myaccount/dev/home-assistant-bash-config
cd /mnt/c/Users/myaccount/dev/
git clone home-assistant-bash
cd home-assistant-bash
git remote add upstream

# Ensure we are up to date
git fetch upstream dev
git rebase upstream/dev

# dev environment setup
sudo apt-get install python-pip python3-dev
sudo apt-get install build-essential libssl-dev libffi-dev python-cffi
sudo pip install --upgrade virtualenv

# make and switch to virtual environment.
virtualenv -p python3 .
source ./bin/activate

# prepare dev bits and install all the requirements, this does take a while.
pip install --upgrade cryptography flake8 flake8-docstrings tox colorlog

Using Visual Studio Code

If you do not have it download it here once it is installed got to File -> Open Folder and navigate to c:\Users\myaccount\dev\home-assistant-bash and open that location. Visual Studio Code (or just Code from now on) is smart enough to know it is on an enlistment in Linux and will work with just LF endings so you do not have to worry about that. Code has a concept of extensions, for Python I use Python, MagicPython and Python for VSCode. These provide syntax checking, lint and other actions in the UI to make life easier. Also as you work with files Code may suggest other extension to help you along, and it handles YAML as a bonus and I do all my configuration editing in it.

Starting HASS

Open up bash and run the following.

cd /mnt/c/Users/myaccount/dev/
virtualenv -p python3 .
source ./bin/activate
hass -c /mnt/c/Users/myaccount/dev/home-assistant-bash-config

Development Cycle

This is the rough process I use.

  1. Open up bash and shift to the virtual environment and run hass as a check to see if all is good.
  2. Rebase with upstream to ensure I am not drifting to much (the code base in Home Assistant is very fast moving)
  3. Run hass again to ensure all is good.
  4. Open up Code against the hass folder code c:\Users\myaccount\dev\home-assistant-bash
  5. Open up Code against the configuration folder code c:\Users\myaccount\dev\home-assistant-bash-config
  6. Hack away!
  7. Restart hass as needed and switch between windows while drinking coffee and wondering how you can automate the walking of the dog.

Using Microsoft Flow with Home Assistant

Now that Microsoft Flow is out of Beta I thought I would play with it and integrate it with Home Assistant running my house. The Microsoft Flow is fairly flexible and has more flow options than IFTTT to make choices. To use this with Home Assistant I am going to use the REST API to make and set a sensor value, you can then take this and make it as complicated as you want. :) Settings You need to ensure you have this information at hand. * Home Assistant Endpoint - https://publicdomin:optionalPort/api * Your password for the http component. If you public expose HA without a password, stop and fix it now! * The name of the sensor you want to update. sensor.kitchen_temperature in this case.

  1. Go to and login.
  2. Make a new Flow from a blank template
  3. Search for button (you can use other triggers but this is an easy start)
  4. Add a new step of type action.
  5. Select HTTP
  6. Change Method to POST
  7. In URI put the full URI of the sensor you want to change. Using the data above this is https://publicdomin:optionalPort/api/states/sensor.kitchen_temperature
  8. In headers add the following JSON blob. { “x-ha-access”: “YOURPASSWORD”, “Content-Type”: “application/json” }
  9. In Body put the JSON to update the sensor. { “state”: “25”, “attributes”: { “unit_of_measurement”: “°C” } }
  10. Save the Flow!

Now open up Flow on your phone and log in with the same account, you will see a nice big button with manual -> HTTP on it. Click that button and look at the state of the sensor in Home Assistant. You will see it with the value you set in the body. To trouble shoot, in the web UI click on the i with a circle around it. It will show a log of successes and failures. If you click on that you can drill into the details of the flow and the responses from Home Assistant.

Running Home Assistant on Pine64

This is a dump of me setting up Home Assistant on a Pine64 board I have. I am used to OpenHAB and have created bindings for it so this should be a new challenge and a way to see how well it works. The reason I am heading towards Home Assistant is the components it support fit some of my needs better. Also time to get to know python better :)

  1. Get DietPi, I am using DietPi_v127_PineA64-arm64-(Jessie) for this so over time some of the items may change, if you know what you are doing you should be good. It can be found at
  2. Write it to a SD card and boot the Pine64 up with the card in the SD slot.
  3. Find it on the network! I just looked at the DHCP logs for my router. It I hard wired into the network so easy to find.
  4. Use a SSH client(Kitty) to connect to it, it will prompt you a few times and update. I will assume you can handle this, eventually you will connect to the Pine64 and have a system ready to go. Login details: username = root password = dietpi Things you should do.
    • Change the root password.
    • Update the firmware as needed.
    • Install Samba or FTP server is you want to edit remotely.
  5. User setup, I like to create a user for my own use. So being creative I shall call it fred! Make sure you add it to the sudo group to run admin commands. sudo adduser fred sudo adduser fred sudo
  6. Install python 3: sudo apt-get install python3 python3-pip
  7. Install Home Assistant pip3 install homeassistant
  8. Start Home Assistant: hass

Enabling MQTT abilities in OpenHab

Using your favorite editor in Linux or something like WinSCP we need to edit some OpenHAB configuration files. If you followed the guides on this site the configuration is located in /etc/openhab. Open up /etc/openhab/configurations/openhab.cfg to edit. If it does not exist you need to copy openhab_defaults.cfg in the same directory to openhab.cfg

sudo nano /etc/openhab/configurations/openhab.cfg

You need to navigate to the MQTT Transport section for transport settings. Ignore the MQTT Persistence section. The following three bolded configuration items need to be modified, I am using pimosquitto as the broker name, you can use whatever you want. Just be consistent.

################################# MQTT Transport ######################################
# Define your MQTT broker connections here for use in the MQTT Binding or MQTT
# Persistence bundles. Replace [broker] with a id you choose.

# URL to the MQTT broker, e.g. tcp://localhost:1883 or ssl://localhost:8883

# Optional. Client id (max 23 chars) to use when connecting to the broker.
# If not provided a default one is generated.

# Optional. User id to authenticate with the broker.
# mqtt:[broker].user=[user]

# Optional. Password to authenticate with the broker.

# Optional. Set the quality of service level for sending messages to this broker.
# Possible values are 0 (Deliver at most once),1 (Deliver at least once) or 2
# (Deliver exactly once). Defaults to 0.

# Optional. True or false. Defines if the broker should retain the messages sent to
# it. Defaults to false.

# Optional. True or false. Defines if messages are published asynchronously or
# synchronously. Defaults to true.

# Optional. Defines the last will and testament that is sent when this client goes offline
# Format: topic:message:qos:retained
#mqtt:[broker].lwt=[last will definition]

Save the file. You are now ready to create a item in openhab and add it to a sitemap. I will assume you have the demo items and sitemap in place for this or already have a file ready to go. Open up the items file in the items folder under /etc/openhab/configurations. Add in a new group for testing, you can remove/comment this out when you are happy with your real configuration. Also add in a String so we can see the values in the site map. Example:

/* MQTT Testing groups, string and switches /
Group MqttTesting
Inbound String for topic home/testing/message /
String MqttTestMessage “Message: [%s]“ (MqttTesting) {mqtt=”<[pimosquitto:home/testing/message:state:default]“}
Inbound Switch for topic home/testing/message */
Switch MqttTestSwitch “MQTT Switch” (MqttTesting) {mqtt=”<[pimosquitto:home/testing/switch:command:ON]“}

Next we add this section to the sitemap. Open up the sitemap file in the sitemaps folder under /etc/openhab/configurations. This is a very simple frame that will list anything out in the group MqttTesting

Frame label=”Tests” {
Group item=MqttTesting label=”MQTT Testing”

Save the file and then open up your browser and navigate to http://:8080/ In mqtt-spy publish a message to home/testing/message with a message of hello, you should see it on the OpenHAB site. You now have MQTT working with OpenHAB.

Installing OpenHAB

To install and run OpenHab on your Pi I am recommending you use the apt-get approach, these instructions are taken from the OpenHAB Wiki, so it may be more up to date and have additional options. I also contribute to changes in the wiki every now and then so this may get out of date but in general be correct. This page does highlight the addons needed for the guides on my site. Shopping List - things you need to do this.

Just a Raspberry Pi for this, I still use V1 B+ as I have not needed to get a V2 yet. This pack has everything you need to get started.

  1. Install Java if 1.6 or higher is not already installed. The following will display your current Java version.

    java -version
  1. Add the openHAB Bintray Repositories key to the apt-keyring using wget:

    wget -qO - '' | sudo apt-key add -
or using curl:

    curl '' | sudo apt-key add -
  1. Add openHAB apt repository to the apt sources list (Note: the current openhab.list file will be overwritten)

    echo "deb stable main" | sudo tee /etc/apt/sources.list.d/openhab.list
  1. Resynchronize the package index

    sudo apt-get update
  1. Install the openHAB runtime

    sudo apt-get install openhab-runtime
  1. If you have more than one OpenHAB binding with a USB device (Z-Wave, RFXCOM, etc), refer to [[symlinks]]

  2. Start openHAB - manually

    1. Init based on sysVinit (e.g. Debian 7 / Ubuntu 14.x and earlier)

      sudo /etc/init.d/openhab start
      sudo /etc/init.d/openhab status
2.  Init based on systemd (e.g. Debian 8 / Ubuntu 15.x and higher)

        sudo systemctl start openhab
  1. Start openHAB - at system startup

    1. Init based on sysVinit

      sudo update-rc.d openhab defaults
2.  Init based on systemd

        sudo systemctl daemon-reload
        sudo systemctl enable openhab
  1. Install the add-ons / bindings as you need them. To follow these guides install the addons below. Feel free to add any other you may need or want.

    sudo apt-get install openhab-addon-${addon-type}-${addon-name}
Install these addons:

    sudo apt-get install openhab-addon-binding-http
    sudo apt-get install openhab-addon-binding-ntp
    sudo apt-get install openhab-addon-binding-weather
    sudo apt-get install openhab-addon-binding-zwave
    sudo apt-get install openhab-addon-persistence-logging
    sudo apt-get install openhab-addon-persistence-rrd4j 
    sudo apt-get install openhab-addon-binding-mqtt
    sudo apt-get install openhab-addon-binding-exec 
    sudo apt-get install openhab-addon-binding-squeezebox 
    sudo apt-get install openhab-addon-binding-xbmc 

A list of all available packages can be retrieved with

    sudo apt-cache search openhab
  1. Configure your system as outlined here: configuration.

  2. If you wish to use a USB zwave stick or other USB/serial device you will need to add the “openhab” users to the “dialout” group:

    sudo usermod -a -G dialout openhab

and then reboot.
  1. Test it, point your browser to http://localhost:8080/ and you should see your sitemap. Replace localhost with the IP of your Pi if you are running headless.

Loading message from the API: ...