Add Sensor for Sleep Mode/Focus
It would be useful to me to determine when I have entered or exited the Sleep Focus specifically. From a glance through the INFocusStatus documentation, I don't think this is possible to do with the current focus sensor (originally, I expected that there would be an attribute on that sensor with the name of the selected focus, but it seems like Apple won't let you do that).
It might be possible however to get "is currently in sleep mode" from the HealthKit API. Perhaps an Observer Query would be the way to go.
Thank you for considering this request!
A quick workaround would be to use the iOS shortcuts app and create an automation that hits a HA webhook and updates the state.
That's a great idea! Here's the breakdown of what I did for this:
Shortcuts Automation
When: When Bedtime starts
Do:
- Get Wi-Fi network's Network Name
- If Network Details is not
My Home Network: Stop this shortcut (I don't have external access to my HA, and also I'd rather not activate my bedtime automations if I'm not home anyway) - Dictionary:
{"focus_action": "sleep"} - Fire
shortcut_eventwith data
HA Side
In configuration.yaml:
template:
- trigger:
- platform: event
event_type: "shortcut_event"
event_data:
focus_activated: "sleep"
id: "sleep_on"
- platform: state
entity_id:
- binary_sensor.iphone_focus
to: "off"
id: "focus_off"
unique_id: a7b4e799-c1b5-4417-ba94-3968e9f42329
binary_sensor:
- name: Scott Sleep Focus
state: '{{ trigger.id == "sleep_on" }}'
icon: '{{ (trigger.id == "sleep_on") | iif("mdi:bed", "mdi:bed-empty") }}'
unique_id: 17b611fe-5ce0-4a72-b897-5201aa229b02
This could misfire if you go directly from Sleep focus to another non-sleep focus, but that's not something I do, so this works for me.
I have something similar, I have an input text where I fill with my latest focus, the downside is that I have to create one automation per focus type in shortcuts:
Wouldn’t it be easier to expose a input boonlean too HomeKit and use a shortcut to turn it on or off?