Macbook sensor via REST API

I’ve just implemented the same thing on my desktop (Hackintosh) and works great, this opens up a whole new range of Mac related sensors :slight_smile: . I’m not sure about using the Command Line Switch however as I don’t want to be able to change the state from within Home Assistant just be able to read the state from the desktop and display in HASS. Would Command Line Sensor work?

1 Like

I’ve found it useful to be able to toggle the switch when I’ve restarted HA and the switch is initialised with the wrong state. If you develop a better approach please share :+1:

I get you, it doesn’t seem to see the correct state sometimes. Wonder if theres a way to do a check on initialisation within Hammerspoon?

My original intention was to have the Macbook running an API which could be polled (rather than the current push solution). I am not sure if this is possible with Hammerspoon, but what you could do is have the Hammerspoon script periodically post data

1 Like

Hi

I have come across this post as i need a sensor in my HASS to give to state if display is on or off. I have tried Hammersmith but brew cant find it. Does this still work?

DId you every find a solution to this? This is EXACTLY what I want to do

I set this up today - brew suggested this to me:

Error: No available formula with the name "hammerspoon"
Found a cask named "hammerspoon" instead. Try
  brew cask install hammerspoon

Also rest API authentication method has changed. You need an access token generated from HASS so you need something like this in the init.lua:

data = '{"entity_id": "switch.imac_power"}'

headers = {}
headers["Authorization"] = "Bearer verylongtokenhere"


local sleepWatcher = hs.caffeinate.watcher.new(function (eventType)
      if (eventType == hs.caffeinate.watcher.systemDidWake) then
          hs.notify.show("Wake sequence initiated!", "", "")
     		  hs.http.post("http://192.168.1.168:8123/api/services/switch/turn_on", data, headers)
      elseif (eventType == hs.caffeinate.watcher.systemWillSleep) then
            hs.notify.show("Bye!", "", "")
     		    hs.http.post("http://192.168.1.168:8123/api/services/switch/turn_off", data, headers)
      end
end)
sleepWatcher:start()

Hope that helps!

1 Like

This init.lua seems to only fire when I set the iMac to sleep from the apple menu.

Would anyone know what to add for when it sleeps via the energy saver settings?

@tomplums The only way I could find to achieve this is to set up a screensaver to start before the Mac goes into energy saving mode and the set the script to detect that.

elseif (eventType == hs.caffeinate.watcher.systemWillSleep) or 
(eventType == hs.caffeinate.watcher.systemWillPowerOff) or 
(eventType == hs.caffeinate.watcher.screensaverDidStart) then

There must be a better way but Ive only just looked into this as I wanted to turn on and off LED’s behind my monitor.

1 Like

I’m using EventScripts to do this. It can fire a script when the screensaver starts/stops, screen sleeps/wakes, login/logout, etc. It fires when energy settings kick in. It does not detect a change of state if the user ‘logs out’ via Fast User Switching. My script just writes a text file to a local apache install and I read it with an HA sensor.

1 Like

Looks interesting, thank you @thoughton

No worries. On one my older Macs it’s a little crashy, but it’s easy to keep it alive with launchd.

It’s a good way to have a sensor whether or not a computer is currently in use, I find it really useful addition to presence detection.

Note that the MacOS “Companion App” now exists and supports reporting the sleep/wake status, as well as the name of the currently active window (which is pretty rad).

As far as I know, it doesn’t expose any kind of switch or event for putting the machine to sleep.

3 Likes

Great app. !!
I made an automation to charge my MBPr (via a sonoff), when battery goes bellow 20% and disconnects at 80%. I use this because my MBP is now used as desktop with a monitor attached, since covid and wfh, and this keeps my battery healthy.

1 Like