Remote Applescript via HA

Hi

I currently use the excellent SecuritySpy on a spare MacMini for my home CCTV system. I have tried ZoneMinder but just find it a pain and unreliable. SS is very reliable and have used for it a year so. My HA is running on a Pi.

Although SS doesn’t have any plugins for HA (it does for Indigo) it does some basic scripting functionality using AppleScript. For example you can tell the app to turn on motion recording by using the script:

Tell Application "SecuritySpy"
set motion camera number 0
end tell

This has to be done locally on the SS MacMini. This would be great to do when when I leave home rather than relying on the inbuilt static schedules in SS. I don’t know much about OSX, does anyone have an idea how I can do this?

Thanks

3 Likes

Have you figured this out?

Let me know if you’re still interested, as I know how to do this now…

I send commands to my Macs from Home Assistant, Google Assistant, Alexa etc. for a few things, and the app I use is Keyboard Maestro.

In Keyboard Maestro I create a macro (which you can build workflows in a similar way to Automator, or just run scripts).
In Keyboard Maestro it will give a script (AppleScript, Shell Script, whatever you like) to run that macro from another source (in my case Home Assistant running on a RPi3).

In Home Assistant here’s an example of the code to run a script on my Mac mini (Reboot) and on my iMac (Backup Home Assistant)

shell_command:
  mac_mini_reboot: curl -k --user MYUSERNAME:MYPASSWORD 'https://MACMINI_IPADDRESS:4491/authenticatedaction.html?macro=6DG455B6-7213-421F-8CU2-91K5GD340D36'

  imac_5k_backup_home_assistant: curl -k --user MYUSERNAME:MYPASSWORD '://IMAC_IPADDRESS:4481/authenticatedaction.html?macro=09DE9D03-DD73-4FBB-966C-332B5F8BCC93'


script:
  mac_mini_reboot:
    alias: "Reboot Mac mini"
    sequence:
      - service: shell_command.mac_mini_reboot
      - service: notify.ios_jonos_iphone
        data:
          title: "Mac mini"
          message: "Rebooting"

    imac_5k_backup_home_assistant:
      alias: "Backup Home Assistant"
      sequence:
        - service: shell_command.imac_5k_backup_home_assistant
        - service: notify.ios_jonos_iphone
          data:
            title: "Backup Started"
            message: "Backing up Home Assistant"

There's probably other ways (easier?), but I already use Keyboard Maestro for lots of things so use that.
2 Likes

Very interested in this but having trouble getting it working. Are you using the public web trigger shared trigger? When I’ve tried pasting the link it gives me, nothing seems to happen. Do you have to open up ports to let KM communicate outside your network? Thanks.

In Keyboard Maestro’s preferences I went to Web Server and filled in some login details (note, it says if you use HTTPS you change the port number and +1).

For the macro trigger it doesn’t matter what you select, as you select script below. Then copy the code for the macro (32 digit code) and paste that into your yaml code in Home Assistant

I would love to know how to call AppleScript from HA

setup a bash script and call it via a shell command.
look here for apple script and bash scripting examples:

#!/bin/bash
osascript <<EOD
  tell application "Google Chrome"
      activate
  end tell
  tell application "System Events"
      key down {command}
      key down {shift}
      keystroke "f"
      key up {shift}
      key up {command}
  end tell
EOD

echo "Google Chrome is now open in Kiosk Mode"

oh, and if you need to call it from a remote machine, call the bash script via ssh from home assistant.

1 Like

Ah good old bash script, that keeps it simple thanks you!

@jono I’m trying to do this but can’t get it to work. Where do I put the code? All in the “configuration.yaml” or do you also use “script.yaml”? Also, how do you set up a shell_command in hass.io (witch I use)?

I’ve put this in the “configuration.yaml” file:

shell_command:
trigger_imac_spotify: curl -k --user username:password ‘https://localhost:4491/authenticatedaction.html?macro=XXXX

A script with an activation button turns up in my Overview.

As well as the shell command you need a script to call/run the shell command from the frontend of Home Assistant, Alexa, Home Assistant etc.

I have separate yaml files for scripts. shell commands etc. but if you just have a separate scripts.yaml then put the script part in there, and the shell command part in configuration.yaml.
So it should be something like this:

configuration.yaml

  shell_command:
      trigger_imac_spotify: curl -k --user username:password 'https://<IMAC_IP_ADDRESS>:4491/authenticatedaction.html?macro=XXXX'

_scripts.yaml_
trigger_imac_spotify:
  alias: "iMac: Start Spotify"
  sequence:
    - service: shell_command.trigger_imac_spotify

Then if you want to see/trigger the script in Home Assistant's frontend you'd add it to one of your groups

- script.trigger_imac_spotify


I think you’d also need to change ‘localhost’ to the IP address of your iMac as well.

Thank you @jono , now it works :slight_smile: Think it was both my ip adress and the port, I used the port for http.

Excellent :+1:t3:

you are a hero :smiley:

1 Like

Hello Jono;

So I have been playing with this to get HA to redirect my cameras’ PTZ at sunrise and sunset. So far I have only began familiarizing myself with Keyboard Maestro (thanks for the hint - its a powerful app!). Anyhow, I am at the point where I am trying to integrate this into my HA config.yaml, but I am not clear on a couple of things;

1: What Username:Password configuration should I be putting in the curl script; my macbooks credentials or those for the Keyboard Maestro trigger site?

2: What Macro id should I be using; ‘This Mac only’, ‘Shared’ or the really long one at: https://trigger.keyboardmaestro.com/t/…?TriggerValue?

3: How would I then trigger these scripts at sunrise/sunset?

Your help would be greatly appreciated.

  1. In Keyboard Maestro > Preferences > Web Server this is the username and password that you need to use (if you haven’t added anything then add something there).

2. For the trigger you select Remote Trigger and by default it's set to 'this Mac only', meaning it will only run on that Mac. If you want it to run on multiple Macs when you run the trigger then change it to shared.

Click on the Copy button next to the trigger URL and that will copy what you need to use in Home Assistant.

Here’s an example of what you’d need to trigger it at sunrise or sunset

shell_command:
      trigger_camera_ptz: curl -k --user username:password 'https://trigger.keyboardmaestro.com/XXXXXXXXXXXXXXXXXXXXXXX?TriggerValue'

automation:

  # At Sunrise
  - id: 'camera_ptz_at_sunrise'
    alias: "Camera PTZ at Sunrise"
    initial_state: 'on'
    trigger:
      - platform: sun
        event: sunrise
    action:
      - service: shell_command.trigger_camera_ptz


  # At Sunset
  - id: 'camera_ptz_at_sunset'
    alias: "Camera PTZ at Sunset"
    initial_state: 'on'
    trigger:
      - platform: sun
        event: sunset
    action:
      - service: shell_command.trigger_camera_ptz

Hello Jono;

Thank you for your quick reply. I’ll try and implement that stuff later today and see how it goes.

Thanks!

So Jono, I wanted to let you know. It all works fine now. The exterior link via the trigger server was doing nothing, so I went back to the localhost option - works fine now.

Thanks for all your help!

Good to hear :+1:t3:

As an alternative to Keyboard Maestro, I found the adnanh/webhook tool. It is a small web server you can run on your Mac that will listen for http requests and run predefined shell commands (so could be anything, including AppleScript).

If you’re a nerd like me and enjoy using foss, check it out. If you’re more comfortable with using a nice user interface, I’d recommend you stick with Keyboard Maestro :slight_smile:

1 Like