Shutting down HA if laptop battery is low?

So i thought it would be a good idea to shutdown HA properly (running in a vm) if the battery of the laptop is running low. There’s been a few instances where i’m doing some work on light switches and power is off a little bit longer then i thought it would be so…

Is there anyway of doing this? I can’t use a smart plug because if power is down wifi is also down, i could use a zigbee power plug i guess.

any thoughts ? have you done something similar?

Thanks,

System Monitor can see battery of host.

When battery is low use Command line switch to shutdown system

1 Like

Nice! thank you!

Apologies for the lazy question but I don’t want to reboot my production server (a laptop) to discover the answer. Does the battery sensor appear without having to explicitly specify a type? Because I don’t see an explicit type for battery in System Monitor’s documentation.

I was wrong about using system monitor as it does not show battery I guess. I kinda just assumed it showed it

Maybe instead just directly use Command Line, Linux Battery or other integration to get battery

1 Like

No worries; I’ll try Linux Battery.

Options for getting the battery status in Linux: https://www.cyberciti.biz/faq/linux-laptop-battery-status-temperature/

If it’s Windows, I’d just go with setting the MQTT Broker on the Laptop and tell Home Assistant to connect to localhost MQTT Broker. Then install IOT Link, which I am 99% sure has battery status.

Edit: Battery status definitely supported:

iotlink/workgroup/my-computer/windows-monitor/stats/battery/remaining-percent
1 Like

I figured that out as well. Linux battery i can’t get to work, however, i found this: GitHub - timmo001/system-bridge: A bridge for your systems which has a homeassistant integration as well. Looks fairly complete, and supports command passthrough as well.

1 Like

If Linux Battery fails then I’ll probably try using Command Line.

I know about System Bridge but haven’t had a need for all the monitoring data it collects; battery level is, for the moment, all I require. Same for IOT Link but thank you all for the useful suggestions.

1 Like

Makes sense. I thought system bridge might provide me with useful stats on ha cpu but probably don’t need so much software for that. And i don’t want to overload the system with extra stuff.

Do let me know if you get linux battery to work (assuming you use ubuntu or similar). I’d be interested in that route too. That’s all i was looking for anyway, just find a way to shut the VM down in a safe manner.

It seems to be working but I haven’t done any testing yet.

My production system is an old Dell laptop running Home Assistant Supervised 2021.11.3 on Debian Bullseye. Here’s what the battery sensor looks like:

Hmm that’s more then i got for ubuntu.

Would you mind sharing your configuration? Did you do anything special?

All I did was follow the documentation for the Linux Battery integration; add this in the sensor domain then restart Home Assistant:

  - platform: linux_battery
1 Like

Yeah, i’m just getting this in the logs (running latest LTS ubuntu)

2021-12-09 15:34:05 ERROR (MainThread) [homeassistant.components.websocket_api.http.connection] [139691981010208] Received invalid command: energy/get_prefs

2021-12-09 15:34:06 ERROR (SyncWorker_1) [homeassistant.components.linux_battery.sensor] No battery found

I just added:

homeassistant:
  ...
  allowlist_external_dirs:
    - /sys/class/power_supply/

and i realised, even though the directory is there, the battery it’s looking for, maybe i didn’t ‘pass it through’ from vmware?

i’ll have a look at the settings and report back.

update Yup. Had to allow VMware to ‘report battery usage to system’ checkbox. Don’t know why this is not on by default. Now i see the battery.sensor:

name: BAT1
path: /sys/class/power_supply/BAT1
alarm: 0
capacity_level: Full
cycle_count: 0
energy_full: ''
energy_full_design: ''
energy_now: ''
manufacturer: ''
model_name: VMware Virtual Battery
power_now: ''
serial_number: ''
status: Full
voltage_min_design: ''
voltage_now: 12406000
unit_of_measurement: '%'
friendly_name: Battery
device_class: battery

says 100 for state because it’s attached to mains. imagine the other values will fill up once i disconnect the power chord.

Now just to figure out what the equivalent is of ‘host shutdown’ on the cli. I found a service called: Home Assistant Supervisor: Poweroff the host’. I guess i’ll try that.

Hello @atv , I’m running in the same case. Because I’m in aVM, the guest don’t see the battery and then linux_battery don’t find any battery.

Where did you add the yaml config:

homeassistant:
  ...
  allowlist_external_dirs:
    - /sys/class/power_supply/

Does it solve the point ? I’m on KVM an not VMWare, I wonder if it is just possible to see the battery level of the host.

So i have:

# Report host battery for detecting if we need to shutdown HA VM
 
  - platform: linux_battery
    name: 'Home Assistant Laptop Battery 1'

in my sensors.yaml

I remember struggling a lot with getting this sensor to work, but i think in the end it came down to me not setting up the battery to be passed through in the VMware client.

I didn’t have to use the allowlist_* stuff.

Hope that helps.

I have similar problem, I would like to get the percentage battery in Home Assistant. My case is slightly different as I am running Home Assistant Operating system directly on the laptop. So, it’s not running as VM or in a docker. The Home Assistant OS is the only OS running on the laptop.

I thought since Home Assistant OS is based on Linux, the Linux Battery add on should work. But after I added it, I don’t see any battery sensor.

I saw Device Bridge mentioned here, but not sure how it can be installed on Home Assistant OS. There is an add-on for home assistant but it requires that device bridge is already installed in the host system. The download page for device bridge gives a .deb installation file, can that be installed on Home Assistant OS?

Any help is appreciated.

i am completely new and confused
i am running HA in a docker container on a laptop running Ubuntu server do you have any idea if this could be the same for me because it seems no matter what i do i cant get any thing to pop up other than when adding a command sensor and then it just says unknown whatever i do.