Detect Power Outage with Laptop

I recently moved to an old laptop for my HA instance and am pretty happy with it. Everything is quicker and having a built in keyboard and mouse is nice. Another benefit is if there are power issues I don’t have to worry about corruption on the Pi.

That got me thinking that the laptop knows when its on battery or on the AC adapter, why not make a sensor and notification.

  - platform: command_line
    name: Power
    command: "cat /sys/class/power_supply/AC/online"

  - platform: template
    sensors:
      power_status:
        entity_id: sensor.power
        value_template: "{%- if is_state('sensor.power', '1') -%} OK {%- else -%} Power Outage {%- endif %}"

Tested it and it works well. Plugged in the sensor reports ‘OK’ and when I pull the plug it reports ‘Power Outage’.

Question is can these somehow be combined? The command line sensor component has a value_template option but I don’t know how to pull the value from the sensor and have it output like the template sensor.

I am also planning to pull the battery life value to determine if I should stop the HA instance if the power is out for an extended period. I just need to do some more poking around the /sys/class folder to find the right file.

5 Likes

Do you mean like this ?:

- platform: command_line
  name: Power
  command: "cat /sys/class/power_supply/AC/online"
  scan_interval: 1
  value_template: '{% if value is equalto "1" %} OK {% else %} Power Outage {% endif %}'
3 Likes

Thanks, Ill try that out tonight. I wasn’t sure how to get the value from the command line sensor into the value_template of the same sensor.

Do you mean the word value in the value_template?
It’s part of internal Jinja variable, so just type it as it is, do not change it.

For example, if the output of the command is ON or OFF, than the code would be:


value_template: '{% if value is equalto "ON" %} OK {% else %} Power Outage {% endif %}'

or

value_template: '{% if value is equalto "OFF" %} Power Outage {% else %} OK {% endif %}'
1 Like

Yeah, I didn’t know that you could use something like that. My templating skills aren’t very good.

Used this to modify my sensor and now I only have one and it works well. Thanks!

Glad to hear that it works for you!

@silvrr kind of offtopic question but what OS flavour did you go with on your old laptop?

The reason I’m asking is because I’m about to try and run Hass on something a bit more powerful than a Pi. Currently leaning towards Ubuntu server.

It’s on Linux mint. It was what I had loaded so I just tried it out. Works great so far.

I am thinking of going to ubuntu server also. Much more lightweight than the desktop variants. If I make the switch I’ll let you know how it goes.

Thank you.

Considering all options at the moment. If I had a spare SSD I’d already be in testing mode. I don’t really understand the differences between them all so I think for me personally I need to be able to search these forums for answers to basic linux questions and it helps to know a few people are successfully running the same OS. Obviously using the RPi with AIO has been fantastic for the support side of things here and getting started with linux.

I was thinking server for the virtual machine and ability to make snapshot backups as mentioned here.

Nice! With a Virtual machine it’s easy to test, and a small foot print so you can easily go back/forth.

I am currently using VMware Workstation (a top of Windows 10) but you can use the free VMware ESXi providing it has the support of the hardware you are running it on. Network drivers are usually the tricky one.

Ubuntu server is pretty light and I run my HASS instance with 1 vCPU and 512Mb RAM. I can help with any other questions you have!

I swapped over to Ubuntu server. I added the Desktop gui as I like being able to pull up a website and copy commands or copy example code. Still pretty lightweight. Install went smooth.

Edit: so server is a bit more of a PTA for certain things, like connecting to a wireless network. add your drivers, restart, manual config of the connection ect. Ill see if I keep running into stuff like this, not worth the headache when mint or ubuntu desktop is still pretty light and pretty plug and play.

Thanks for the update. I’ll just have to try it - intel celeron NUC so hopefully drivers wont be too painful.

Once you have these things working it should be fine though, not like you have to do it all again every time you update.

A disk image once everything is up and working would be just fine for a disaster backup so server isn’t that important to me and I’m planning on just running tests for a month or two anyway. Ubuntu desktop might be a better starting point for me.

Nice. Is it possible to also get the % of battery? I plan to use with a smart plug for battery charge / discharge. Thank you.

Yes, you can probably query the battery charge percentage into a sensor or state in a similar way. This is my sensors.yml file (I’m using a Lenovo X200 laptop, your device’s paths and numeric values may differ)

- platform: command_line
  name: Laptop PSU Connected
  command: "cat /sys/class/power_supply/AC/online"
  scan_interval: 1
  value_template: '{% if value is equalto "1" %} Connected {% else %} Disconnected {% endif %}'

- platform: command_line
  name: Laptop CPU Temperature
  command: "cat /sys/class/thermal/thermal_zone0/temp"
  unit_of_measurement: "°C"
  value_template: "{{ value | multiply(0.001) | round(1) }}"

- platform: command_line
  name: Laptop Battery Percent
  command: "cat /sys/class/power_supply/BAT0/capacity"
  unit_of_measurement: "%"
  value_template: "{{ value }}"

You should be able to then simply include the battery charge in the trigger template

automations.yml (generated through the web interface via “Configuration”, “Automations”, hence the long id)

- id: '1623708680935'
  alias: Laptop Power Notification
  description: ''
  trigger:
  - platform: state
    entity_id: sensor.laptop_psu_connected
  condition: []
  action:
  - service: notify.mobile_app_phone
    data:
      message: 'Laptop PSU state changed to {{ states.sensor.laptop_psu_connected.state
        }}, Battery {{ states.sensor.laptop_battery_percent.state }} %'
  mode: single