I’m trying to put together a python script to safely power down equipment connected to my uninterruptible power supply (UPS), which in not network connected (really useful in storm season) . To test the script, I replaced the poweroff command with some pings which I can detect when they hit my firewall. I can see pings on the firewall when AppDaemon starts and when I save the script, but I’m having a hard time getting Home Assistant to call it.
I want to use a traditional automation to detect the power outage (many options here), but I’m not skilled enough in python to get the script to detect a state change from an input boolean, binary sensor, or voltage reading from a Sonoff POW. I pasted my script below, and I apreaciate any pointers you can give me.
#SSH Safe Shutdown
import paramiko
import appdaemon.plugins.hass.hassapi as hass
class ubnt_bridge1_shutdown(hass.Hass):
def initialize(self):
self.enabled = True
self.log(“UBNT M2 Loco Safe Shutdown”)
I’m a bit confused and don’t really understand what you want to do exactly.
Do you have a Sonoff POW already connected and have a voltage reading in home assistant?
Which devices are connected to the UPS and how do you plan to shut them down safely?
I didnt get it either… If you can write a sensor to detect the power outage, just write a standard HA automation which call a shell_script to either “sudo shutdown -h now” or ssh to another host and do the same :-).
Thank you for your response, What I want to do is use home assistant ssh into and issue shutdown commands to a few pieces of equipment that are plugged into to a UPS. This is so those systems can shut down before the voltage falls bellow the safe limits for that device. I had a NAS unit was damaged in this manner, The power went out and the UPS kept it running until its battery could no longer supply the adequate voltage.
The python script I wrote above is able to SSH into and execute the command in one device, my problem is I can’t get Homeassistant to call it as a service and run it when I need to. It only seems to run when I make a small change to the file, restart Appdaemon, or restart Homeassistant.
I was only using the sonoff POW as an example, I’m fine with writing regular HA automations, I just need to figure out how to get this script to either monitor a state change in HA, or figure out what I’m doing wrong in Appdaemon, and why I can’t seem to call out my script as a service.
Your script will only work when you save the file or restart appdaemon, because you don’t have any trigger for the automation. You need an input boolean or something else in home assistant which will detect that there was a power outage.
E.g. you have an input boolean called input_boolean.power_outage, now you can trigger the script whenever this input boolean changes to ‘on’.
Like this:
import paramiko
import appdaemon.plugins.hass.hassapi as hass
class ubnt_bridge1_shutdown(hass.Hass):
def initialize(self):
self.listen_state(self.shutdown_devices_safely, "input_boolean.power_outage", new='on')
def shutdown_devices_safely(self, entity, attribute, old, new, kwargs)
code to shutdown your devices safely
With this code, the “shutdown_devices_safely” method will only be called if the input_boolean.power_outage changes to ‘on’.
clyra,
Thank you for your response, I will definitely give this option a try. I been taking some Python courses in Skillshare and I’m hoping to find some use for that knowledge it in HA, but a bash script is not off the table if it gets the job done.
Burningstone, your solution worked very well. I created an input boolean and named it “power_outage” with initial set to off. Thank you very much for your assistance, the completed script is below, and it will issue a single command to shut down a device, but it can be easily modified to safely power down devices that need a confirmation or save the running config before shutdown.
I know hard-coding passwords is not a good idea, and when I get better at scripting this will be the first thing I will change. Thank you, and I hope some folks find this script helpful as well: