Displaying Home Assistant entity states in touch bar and using as a button

I initially had some trouble figuring out how to get some of my Home Assistant data info (including living room temp, iphone battery level and Alarm System state) to show on my touch bar, but finally managed it and thought I'd share them for anyone interested. I considered making light buttons too, but there's a delay between pressing the button and it actioning, so it ends up quicker to just ask Siri to turn lights on/off.

For anyone who wants to try this themselves, to get your really long token to authorize the script, go to your HA profile, scroll to the bottem and create a Long-Lived Access Token.

My iPhone battery state is obtained using the ios component. The battery level & state icon determine which icon pic is used.

tell application "JSON Helper"
set iphonebattery to do shell script "curl -X GET -H \"Authorization: Bearer REALLYLONGTOKEN\" -H \"Content-Type: application/json\" https://MYHADDOMAIN:PORT/api/states/sensor.iphone_battery_level"
set iphonebattery to read JSON from iphonebattery
set IconPercent to |Battery Level| of attributes of iphonebattery
set Status to |Battery State| of attributes of iphonebattery
end tell

set BatteryState to Status
if BatteryState is "Charging" then
set BatteryState to "Charging:"
if BatteryState is "Unplugged" then
set BatteryState to "Discharging:"
set BatteryStat to "Error"
end if
end if

if IconPercent ≤ 5 then
set IconPercent to 0 as string
else if 5.1 ≤ IconPercent and IconPercent ≤ 15 then
set IconPercent to 10 as string
else if 15.1 ≤ IconPercent and IconPercent ≤ 25 then
set IconPercent to 20 as string
else if 25.1 ≤ IconPercent and IconPercent ≤ 35 then
set IconPercent to 30 as string
else if 35.1 ≤ IconPercent and IconPercent ≤ 45 then
set IconPercent to 40 as string
else if 45.1 ≤ IconPercent and IconPercent ≤ 55 then
set IconPercent to 50 as string
else if 55.1 ≤ IconPercent and IconPercent ≤ 65 then
set IconPercent to 60 as string
else if 65.1 ≤ IconPercent and IconPercent ≤ 75 then
set IconPercent to 70 as string
else if 75.1 ≤ IconPercent and IconPercent ≤ 85 then
set IconPercent to 80 as string
else if 85.1 ≤ IconPercent and IconPercent ≤ 95 then
set IconPercent to 90 as string
else if 95.1 ≤ IconPercent and IconPercent ≤ 100 then
set IconPercent to 100 as string
set IconPercent to "ERROR"
end if

set IconPath to ("Macintosh HD:Users:PATH:TO:ICON:FOLDER:" & BatteryState & IconPercent &".png") as string
return "{\"icon_path\":\"" & (POSIX path of IconPath as text) & "\", \"text\":\"" & " " & "\", }"

I have Xiaomi Temperature Humidity Sensor in my lounge room and I display the temp on my touch bar in a widget (combined with my Dark Sky weather widget).

tell application "JSON Helper"
set loungetempstates to do shell script "curl -X GET -H \"Authorization: Bearer REALLYLONGTOKEN\" -H \"Content-Type: application/json\" https:/MYHADOMAIN:PORT/api/states/sensor.temp_lounge"
set loungetempstates to read JSON from loungetempstates
set indoortemp to state of loungetempstates & "°C "
end tell

This is what I've got for my Alarm System. It displays a different icon depending on if my alarm_control_panel.haalarm entity shows it's state as Home, Away, Night or Disarmed. Pressing on it will toggle disarm/arm home.


tell application "JSON Helper"
set alarmsystem to do shell script "curl -X GET -H "Authorization: Bearer REALLYLONGTOKEN" -H "Content-Type: application/json" https://MYHADOMAIN:PORT/api/states/alarm_control_panel.haalarm"

set alarmsystem to read JSON from alarmsystem
set state to state of alarmsystem
end tell

if state is "armed_home" then
set Alarm to "Home"
else if state is "armed_night" then
set Alarm to "Night"
else if state is "armed_away" then
set Alarm to "Away"
else if state is "disarmed" then
set Alarm to "Disarmed"
set Alarm to "Pending"
end if

set IconPath to ("Macintosh HD:Users:PATH:TO:ICON:FOLDER:" & Alarm & ".png") as string

return "{"icon_path":"" & (POSIX path of IconPath as text ) & "", "text":"" & "​" & "", }"

To make an easy toggle between Arm Home & Disarmed, I created a switch in HA.

        value_template: "{{ is_state('alarm_control_panel.haalarm', 'armed_home') }}"
          service: alarm_control_panel.alarm_arm_home
          entity_id: alarm_control_panel.haalarm 
          service: alarm_control_panel.alarm_disarm
          entity_id: alarm_control_panel.haalarm   
        icon_template: >
            {% if is_state('alarm_control_panel.haalarm', 'triggered') %} 
            {% elif is_state('alarm_control_panel.haalarm', 'armed_home') %} 
            {% else %}
            {% endif %}     

Widget Predefined Action:

set alarmhometoggle to do shell script "curl -X POST -H \"Authorization: Bearer REALLYLONGTOKEN\" -H \"Content-Type: application/json\" -d '{\"entity_id\": \"switch.alarm_armed_home\"}' https://MYHADOMAIN:PORT/api/services/switch/toggle"

Supercool @Doa!

Might be a stupid question, but would this also work with Nabu Casa (HA cloud)? I haven't exposed HA on my own IP.

Imprint | Privacy Policy