Using automation to shutdown home assistant pi

I would like to use my UPS charge state to trigger a shutdown of the pi that runs my HA instance. I would like to prevent a crash and possible SD card corruption.

So, can I just call the service hassio.host_shutdown at a set charge level and if I do will it shut down the system?

yes, as long as you are using with hassOS. If you upgraded when it was using ResinOS, then it will not work. So, if you upgraded from 0.74 or lower to 0.74 or higher, hassio.host_shutdown will not work. If you installed hassio 0.74 or higher, that uses HassOS and you should be good to go.

Petro, I was running long before .74, so what is the process now? I did have to do a couple of fresh builds in the past, not sure what the versions were at this point. How would one tell if this will work?

Go to your info page and see if you have OS information

If you don’t, you need to wipe and start again. The new installations include HassOS. HassOS does not get installed on upgrades as it’s the OS.

Make a snapshot, reimage your drive with new hassio for raspi and load the snapshot.

This thread is more detailed

Thanks for the info… it looks like I am in luck!

I will get started testing this to verify that it works.

I don’t know if it will work. Try the service and verify that it works. The fact that it says resin OS doesn’t mean that DBUS is on the system and working.

So, the service call works… How would it build the service section in an automation to shut the system down?

You’d need to have access to your apc. Does home assistant communicate with it?

I got it working! Yes, I can communicate via USB with the CyberPower ups and I get regular state updates!

1 basic automation… I have built several to shut down and notify now that it is working. I also have it working with a pi running nut connected to another remote ups.

UPS Automation

  • id: UPS_01
    alias: ‘Notify when UPS goes on battery’
    • platform: state
      entity_id: sensor.nut_ups_status_data
      from: ‘OL’
      to: ‘OB DISCHRG’
    • service: notify.sms_matt
      title: “Alert from HomeAssistant”
      message: “Home UPS on battery”

I am using the addon Network UPS Tools to monitor the local ups and since it installed Nut on the HA server, I was able to add the sensor to communicate with another pi running Nut.

  • platform: nut
    alias: lrups
    name: “LR UPS”
    username: upsmon
    password: nut
    port: 3494
    • ups.load
    • battery.charge
    • input.voltage
    • battery.runtime
    • ups.status
    • ups.model

My resulting cards.

nut is so confusing to set up… i have 2 windows pcs and 1 linux… right now. ive got apcupsd but that only limits me to one :frowning:

I find nut easier than apcupsd, have tried it a few times over the years and it was a PITA. The best part here is the addon works for basic 1 ups connection. After that, it is just setting nut up on additional systems in netserver mode and adding sensors! I will admit, from start to finish I have about 6 hours in making this work, but that also includes 8 automations and the front end stuff. I still have to work out a couple of command line scripts to shut down a couple of other pi’s, I find those to be painful as I always have trouble getting keys to work properly.

The cyberpower was originally connected to a sometimes on / sometimes off windows 10 system and the software is super basic and would not send a shutdown to the HA server. I have crashed it a couple of times over the years and had to rebuild, hope to avoid that issue.

im sure i can get it working on the HA side… i just havent found good instructions on how to install on each comp plugged into a ups.

Good starting point document… This is set up in standalone more, but only need to add netserver mode and add a listen ip / port combo to get it going.

1 Like

just gotta find a windows version. thx

how do you have your standalone/slaves and everything setup?

Not sure about windows version… I am shutting down windows machines with HA automations.

Can you explain the last question? I do not run standalones or slaves. If it has a pi attached, it is getting the nut install and netserver mode. I do leave the local listener enabled and could use that, but HA is incharge of passing on shutdown commands not the local nut installs.

Does that make sense?

I would be willing to zip up one of my conf directories to give you more of a starting place.

i see different configs with multiple computer set as slaves… but i guess 1 pc/ ups then they would all be standalone as long as netserver is transmitting information that HA can pick up. And shutdown procedures would be per ups

also do the add-on config only set it up if your have the ups on the same machine as HA instance? none of mine are.

You could go that route, I chose to leave the local listener so I could troubleshoot issues from the local command line. But the biggie for me was getting the netserver running.

Shutdown proceedures would be per ups/per system based on HA automations. My main ups running HA and some critical networking also has my big bad win10 pc attached. Based on the ups state and charge levels I shut down the windows machine and shutdown the monitor with a sonoff switch. The monitor at idle sucks a fair amount of power. All done to extend the time that HA and the associated switch stay up as long as possible. You will want to check the states incase power drops back out during the recharge process as well. Once could do it based on input voltage as well.

The add-on is designed for a locally attached UPS, but one does not have to be there. The key is that it gets nut running on the HA server and allows for the Nut sensors to be built. Once could just not build sensors for a local UPS.

Does anyone know of a good non-hassio Docker install method for NUT on Linux? it will be on the same NUC (running Debian 9) that is running HA.

I got the notification working when the UPS changes status, but what about shutting down Home Assistant?

I can easily set an action that runs 10 minutes after the status changes from OL to OB DISCHRG and call the hassio.host_shutdown service, but what about canceling the shutdown if the UPS gets to OL again within those 10 minutes?

Use a timer to do the shutdown, and cancel the timer if the OL gets up again ?