iOS icon indicating copy to clipboard operation
iOS copied to clipboard

Add Sensor for Sleep Mode/Focus

Open scolby33 opened this issue 2 years ago • 5 comments

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!

scolby33 avatar Aug 19 '23 22:08 scolby33

A quick workaround would be to use the iOS shortcuts app and create an automation that hits a HA webhook and updates the state.

bgoncal avatar Aug 28 '23 20:08 bgoncal

That's a great idea! Here's the breakdown of what I did for this:

Shortcuts Automation

When: When Bedtime starts

Do:

  1. Get Wi-Fi network's Network Name
  2. 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)
  3. Dictionary: {"focus_action": "sleep"}
  4. Fire shortcut_event with 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.

scolby33 avatar Nov 07 '23 00:11 scolby33

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: Set Focus In Home Assistant

bgoncal avatar Nov 07 '23 08:11 bgoncal

Wouldn’t it be easier to expose a input boonlean too HomeKit and use a shortcut to turn it on or off?

firejackson1 avatar Feb 29 '24 03:02 firejackson1