Skip to content

Instantly share code, notes, and snippets.

@r15ch13
Last active January 9, 2023 04:36
Show Gist options
  • Save r15ch13/1ab042d8453818c604c530aae17ac456 to your computer and use it in GitHub Desktop.
Save r15ch13/1ab042d8453818c604c530aae17ac456 to your computer and use it in GitHub Desktop.
Use smartplug with Octoprint PSU Control and Home Assistant

Use smartplug with Octoprint PSU Control and Home Assistant

This explains how to use any smartplug connected to Home Assistant with Octoprint PSU Control.

It's based on a comment by tht and a Tasmota sonoff thread in the Octoprint forum.

Installation

The installation consist of three simple steps: create a config file, copy the script and change the PSU Control settings

Config

Create a file called .ha in a directory containing the following:

HA_HOST=http://homeassistant:8123 or https://myha.dyndns.org
HA_TOKEN=<long-lived-token>
HA_SWITCH=switch.<smartplug-entitiy-id>
  • HA_HOST: The whole URL (without trailing slash) including the port for local network use or the domain (dyndns address)
  • HA_TOKEN: Go to your HA profile, create a new Long-Lived Access Token and copy it into the file
  • HA_SWITCH: Entity ID of your smartplug

Example:

HA_HOST=http://192.168.1.11:8123
HA_TOKEN=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiI0ZTRkNmMzMzJiNmZlNjJhNjNhZmU1NjE3MWZkMzcyNSIsImlhdCI6MTYxMTQ4OTU2MiwiZXhwIjoxOTI2ODQ5NTYyfQ==.3q2+796tvu/erb7v3q2+796tvu/erb7v3q2+796tvg==
HA_SWITCH=switch.my_printer_smartplug

Script

Create a file called ha_psu_switch.sh in the same directory as .ha file and copy the following script:

#!/bin/bash

source $(dirname $0)/.ha
HA_BODY="{\"entity_id\": \"${HA_SWITCH}\"}"

case "$1" in
    on)
        curl -s -X POST -H "Authorization: Bearer ${HA_TOKEN}" \
          -H "Content-Type: application/json" \
          -d "$HA_BODY" \
          "${HA_HOST}/api/services/switch/turn_on" > /dev/null
        ;;
    off)
        curl -s -X POST -H "Authorization: Bearer ${HA_TOKEN}" \
          -H "Content-Type: application/json" \
          -d "$HA_BODY" \
          "${HA_HOST}/api/services/switch/turn_off" > /dev/null
        ;;
    status)
        curl -s -X GET -H "Authorization: Bearer ${HA_TOKEN}" \
          -H "Content-Type: application/json" \
          "${HA_HOST}/api/states/${HA_SWITCH}" | grep '"state": "on"'
        exit $?
        ;;
    *)
        echo "Usage $0 on|off|status"
        exit 1
esac

Don't forget to make it executable!

chmod +x ha_psu_switch.sh

Testing

Running ./ha_psu_switch.sh on or ./ha_psu_switch.sh off should switch the smartplug. Running ./ha_psu_switch.sh status should output the status if the switch is on, else it outputs nothing.

PSU Control

Set Switching Method to System Command and add the path to ha_psu_switch.sh

On command:

/home/wherever/ha_psu_switch.sh on

Off command:

/home/wherever/ha_psu_switch.sh off

Set Sensing Method to System Command and add the path to ha_psu_switch.sh

Sense command:

/home/wherever/ha_psu_switch.sh status

And that's it.

#!/bin/bash
source $(dirname $0)/.ha
HA_BODY="{\"entity_id\": \"${HA_SWITCH}\"}"
case "$1" in
on)
curl -s -X POST -H "Authorization: Bearer ${HA_TOKEN}" \
-H "Content-Type: application/json" \
-d "$HA_BODY" \
"${HA_HOST}/api/services/switch/turn_on" > /dev/null
;;
off)
curl -s -X POST -H "Authorization: Bearer ${HA_TOKEN}" \
-H "Content-Type: application/json" \
-d "$HA_BODY" \
"${HA_HOST}/api/services/switch/turn_off" > /dev/null
;;
status)
curl -s -X GET -H "Authorization: Bearer ${HA_TOKEN}" \
-H "Content-Type: application/json" \
"${HA_HOST}/api/states/${HA_SWITCH}" | grep '"state": "on"'
exit $?
;;
*)
echo "Usage $0 on|off|status"
exit 1
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment