Display Macbook's Battery percent in Home Assistant

Hi All,

I was wondering how I can use hammerspoon’s - hs.battery.percentage() to show up in home assistant.

I have tried Glances and it doesn’t allow me to see battery %. I would love another way but with the amount of googling I have done hammerspoon seems to be the only way but I don’t even know where to start now that it’s installed on my Mac.

Thanks in advance

You’ll need to somehow make the HS data available to HA. This could be via HA REST or MQQT. See here for someone doing something similar:

Thanks for your reply, I’ve read every post of that thread and it goes way above my head so
I was looking for some help

This is what’s working for me (sub in your own IP/hostname and long-lived access token):

ha_url = 'http://#####:8123'
ha_auth_headers = { ["content-type"] = "application/json", ["Authorization"] = "Bearer ######" }

hs.battery.watcher.new( function()
  data = '{"state": "' .. hs.battery.percentage() .. '"}'
  hs.http.post( ha_url .. '/api/states/sensor.macbook_battery', data, ha_auth_headers )
end):start()
1 Like

Thanks for this! I expanded on it a bit so we also get brightness and the wifi ssid every time the battery percent changes (this was frequent enough for me so I kept it simple instead of setting up individual watchers).

We could potentially get connected usb and serial devices too but I didn’t have a chance to figure out how to convert from the Lua table format.

ha_url = 'https://Your_HASS_IPAddress'
ha_auth_headers = { ["content-type"] = "application/json", ["Authorization"] = "Bearer LongStringFromYourHASS" }

hs.battery.watcher.new( function()
  data_ssid = '{"state": "' .. hs.wifi.currentNetwork() .. '"}'
  data_battery = '{"state": "' .. hs.battery.percentage() .. '"}'
  data_brightness = '{"state": "' .. hs.brightness.get() .. '"}'
  hs.http.post( ha_url .. '/api/states/sensor.macbook_ssid', data_ssid, ha_auth_headers )
  hs.http.post( ha_url .. '/api/states/sensor.macbook_battery', data_battery, ha_auth_headers )
  hs.http.post( ha_url .. '/api/states/sensor.macbook_brightness', data_brightness, ha_auth_headers )
end):start()