I installed HAOS on an old Microsoft Surface Laptop and it is running fine. Is it possible to detect when the laptop switches to running on battery power due to AC loss, and run some automation when this occurs and when AC is restored?
You should be able to do it with a hass agent sensor:
Thanks. I read through the Readme and it seems it’s intended as a windows client. I’m running HAOS, which I thought is Linux-based, so am I still able to use this agent?
Doh. Yeah. Sorry.
This kind of info is usually found in /proc or /sys somewhere.
Once you have found out where, you should be able to use a commandline sensor.
I do this, but I run HA core. Perhaps this might help. I believe HAOS is Debian-based.
I have a udev rule:
SUBSYSTEM=="power_supply",ENV{POWER_SUPPLY_NAME}=="AC0",RUN+="/usr/bin/curl -s -X POST http://localhost:8123/api/webhook/server_power_connected -d 'status=$env{POWER_SUPPLY_ONLINE}'"
And my automation:
# udevadm monitor
# udevadm info --path=/sys/class/power_supply/AC0
# /etc/udev/rules.d/60-power.rules
# SUBSYSTEM=="power_supply",ENV{POWER_SUPPLY_NAME}=="AC0",RUN+="/usr/bin/curl -s -X POST http://<host>:<port>/api/webhook/<webhook_id> -d 'status=$env{POWER_SUPPLY_ONLINE}'"
# udevadm control --reload-rules && udevadm trigger
# sudo /etc/init.d/udev restart
# udevadm control --log-priority=debug
# (sudo) journalctl -f
- alias: "Update Server Power Status"
initial_state: true
trigger:
- platform: webhook
webhook_id: server_power_connected
local_only: true
mode: queued
action:
# sensor definition: platform: linux_battery
- service: homeassistant.update_entity
entity_id: sensor.home_assistant_server_battery
sensors.yaml
:
- platform: linux_battery
name: Home Assistant Server Battery
battery: 0
system: linux
scan_interval: 60
binary_sensors.yaml
:
electricity_feed:
friendly_name: "Electricity"
# https://www.home-assistant.io/integrations/binary_sensor/#device-class
device_class: power
value_template: >-
{{ not is_state_attr('sensor.home_assistant_server_battery', 'status', 'Discharging') }}
icon_template: >-
{% if is_state_attr('sensor.home_assistant_server_battery', 'status', 'Discharging') %}
mdi:power-plug-off
{% else %}
mdi:power-plug
{% endif %}
Specifically, on my Lenovo Thinkpad (so the power hardware will be different, so your details may vary) reading /sys/class/power_supply/AC/online
gives me 0 when on battery and 1 when power is pugged in.
No. Based on buildroot.
Thanks Nick.
EDIT: Not familiar with the tool, but a quick search shows that udev is supported.