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.
The installation consist of three simple steps: create a config file, copy the script and change the PSU Control settings
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 newLong-Lived Access Token
and copy it into the fileHA_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
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
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.
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.