Get (or push) lock status of iPad or iPhone

I’m looking for a way to determine whether my kids tablets are being used or not. I’d like a boolean status in HA to show when they are on them, and then I could run some statistics to see how much time they’ve been in use.

They don’t have, nor do I want to install, the HA companion app on their devices.

I was going to put a shortcut on their iPads to issue a POST to a HA webhook, but it seems the iOS shortcuts app doesn’t support a trigger upon lock & unlock.

I use Unifi networking products but I don’t see a reliable way to determine if the iPad is being actively used from data available on the network.

Any ideas out there? This seems like I’m overlooking something that would be simple.

As a follow-up: I ended up using the iOS shortcuts app to create automations that are tied to opening and closing apps, as well as plugging & unplugging the charge cord. There’s no “any app” trigger so I had to select every app that is installed. This also means whenever a new app is installed, I’ll have to go back on their iPad and add that app to the list of triggers. So it’s fairly tedious, but it does work.

Here’s an example of the iOS automation:

and this is the detail of the two ‘Do’ actions:

The shortcut that it calls for is this behemoth, which I can use for every one of these automations so I don’t have to repeat all this complicated stuff. Quick summary:

  • Get phone’s IP address, and only continue if there is one. This avoids all the error messages when the ipads aren’t on wifi.
  • Get the dictionary from the automation that called the shortcut
  • Calculate unix time by subtracting zero from the current date
  • Create a dictionary with unix_time as a key and the previous calculation as the value
  • Merge the two dictionaries using some regex
  • POST the data as JSON-formatted text to my home assistant instance

Then in home assistant you can use various methods to create sensors. I had the webhook automation creating/updating REST sensors, but that was a bit of a pain with HA reboots losing the status of the sensors. I installed the Variables+History integration and changed my webhook to update binary sensors that were created with that integration. Here’s what my automation looks like; this automation can be used to update any sensor that is created with that integration, and it ensures that only newer states update older ones, by way of comparing the unix_timestamp attribute.:

alias: Variables+History Sensor Update from Webhook
description: Update Variables+History Sensor via Webhook
trigger:
  - platform: webhook
    webhook_id: rest-sensor-creator-ilmmcYzI-QS9xBDrjdnP4Rh2-2
    enabled: false
  - platform: webhook
    webhook_id: variableshistory-sensor-update-from-webhook-1uFkhiqhmvDqK2tuoMrgIHRs
condition: []
action:
  - if:
      - condition: template
        value_template: >-
          {{ state_attr(trigger.json.entity_id, 'unix_timestamp') | float(0) <
          trigger.json.unix_timestamp | float(0) }} 
    then:
      - service: variable.update_binary_sensor
        data:
          replace_attributes: false
          value: "{{ trigger.json.state }}"
          attributes: "{{ trigger.json | tojson }}"
        target:
          entity_id: "{{ trigger.json.entity_id }}"
      - choose:
          - conditions:
              - condition: template
                value_template: "{{ trigger.json.entity_id.split('.')[0] == 'binary_sensor' }}"
            sequence:
              - service: variable.update_binary_sensor
                data:
                  replace_attributes: false
                  value: "{{ trigger.json.state }}"
                  attributes: "{{ trigger.json | tojson }}"
                target:
                  entity_id: "{{ trigger.json.entity_id }}"
          - conditions:
              - condition: template
                value_template: "{{ trigger.json.entity_id.split('.')[0] == 'sensor' }}"
            sequence:
              - service: variable.update_sensor
                data:
                  replace_attributes: false
                  value: "{{ trigger.json.state }}"
                  attributes: "{{ trigger.json | tojson }}"
                target:
                  entity_id: "{{ trigger.json.entity_id }}"
mode: queued
max: 100

1 Like

Can you share your iOS shortcut in more detail? Really interested in seeing what i can do wthi this (but not so up to speed with regex, webhooks, etc.)

The entire shortcut is the screenshot. Not sure what more detail I could provide, but I can walk you through how I entered each one if you’re having trouble with a specific section. The regex I stole from a Google search, all that part does is add another key & value to an existing dictionary.

The webhook is a home assistant thing. You create an automation in HA, and as the trigger you choose “webhook”. This gives you a webhook ID, and this is the suffix you’d use for the web address to trigger it.

In my screenshot I assume you home assistant instance is accessible from https://homeassistant.example.org/
and that the webhook ID from your automation is webhook-unique-trigger-path. That is why the POST call goes to https://homeassistant.example.org/api/webhook/webhook-unique-trigger-path. You need to change that based on your own instance and webhook detail. Note that there is no authentication, so anyone who has that link can trigger your automation, so the webhook ID should be complicated and random.

In a webhook, besides just triggering an automation, you can pass data into it. Which is why I have the iOS shortcut build a dictionary to pass into that webhook. (A dictionary is just a bunch of data pairs, called keys & values.) You can have the webhook read the dictionary and do whatever you want with that info.