Counting time when a Samsung smartphone was unlocked

Hi,

I like to count the time per day when a specific smartphone was unlocked. The reason for that is because one of my kids has an addiction for gaming on her smartphone. At first I was counting the online time via wifi but that didn’t work, when a smartphone gets locked the wifi stays on.

So I installed the Home Assistant app on her Samsung Galaxy A51 and found out the ‘Device Locked’ binary sensor. The status of this sensor is on or off.

Can anyone help me how I can count the time per day? Or fix it otherwise?

Thanks in advance!

Use history stats integration

1 Like

Thanks for your help @dshokouhi :smiley:

I’ve added this code in my configuration.yaml:

sensor:
  - platform: history_stats
    name: Smartphone A Unlocked Hours Today
    entity_id: binary_sensor.smartphone_a_device_locked
    state: 'off'
    type: time
    start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
    end: '{{ now() }}'

After a reboot a new sensor was available (sensor.smartphone_a_unlocked_hours_today) which I can use on various ways on my Lovelace dashboard. Exactly the solution I needed.

Do you also know a way to show a sort of list of the values of the last x days?

maybe the logbook card?

1 Like

@dshokouhi
Unfortunately this does not give the desired result. For example, I would prefer to see a table of the past 7 days with the total number of hours behind it. The History Stats counts to 00:00:00 and then resets the value. It would be nice if at 23:59:00 I could store the value (with timestamp) and then show the last 7 via a table.

ok then store it using an input_text ? Input Text - Home Assistant

If it is a Samsung phone you could do easier by just opening the device care app on her phone.

I believe the HA app could also do something like this? Not sure but there are quite a few sensors you can expose to HA via the app. Though if she finds out how to disable them it would still be useless (trust me, they will find out :rofl:).

@dshokouhi I really have no idea how to get the daily values in the Input Text. Unfortunately I can’t find any support on how to do this in the documentation. Can you help me out a little further?

@jimz011 This has been my control tool in the past but in no time she figured out how to reset it. I’ll have to keep it externally or else I can be sure it’s been tampered with.

use the input_text.set_value service call to set the state, then if you only update it with values you want the logbook card will reflect the state changes. The idea here is that you control what/when you store a value so you know what to expect in the frontend. Hope that makes sense.

1 Like

@dshokouhi I am afraid this is beyond my understanding of this matter. Usually I can figure it out by copying examples and adapting it to my needs. But there is no sample code of set value to text.

its right here: Input Text - Home Assistant
in this example it is setting the value to the state of another entity, but you can make it anything you want.

@dshokouhi Ehhhhh… :thinking: :confused:

My sensor is called sensor.smartphone_a_unlocked_hours_today
I have no clue how to edit this piece of code to get it working. For me the example code looks like to do something else.

input_select:
  scene_bedroom:
    name: Scene
    options:
      - Select
      - Concentrate
      - Energize
      - Reading
      - Relax
      - 'OFF'
    initial: "Select"
input_text:
  bedroom:
    name: Brightness

automation:
  - alias: "Bedroom Light - Custom"
    trigger:
      platform: state
      entity_id: input_select.scene_bedroom
    action:
      - service: input_text.set_value
        target:
          entity_id: input_text.bedroom
        data:
          value: "{{ states('input_select.scene_bedroom') }}"

I’m sorry to trouble you this much. But I’m learning a lot of new things today :slight_smile:

A history graph can be used to directly show the values of the history stats sensor over the time frame that is stored in your recorder (default 10 days?).

if you want a bar graph then you can use different custom cards - mini-graph-card or apexcharts-card.

@finity @dshokouhi

I’ve slept a night about it and figured out another way.

  - platform: history_stats
    name: Smartphone A Unlocked Hours Past One Day
    entity_id: binary_sensor.smartphone_a_device_locked
    state: 'off'
    type: time
    end: "{{ now().replace(hour=0, minute=0, second=0) }}"
    duration:
      hours: 24

  - platform: history_stats
    name: Smartphone A Unlocked Hours Past Two Days
    entity_id: binary_sensor.smartphone_a_device_locked
    state: 'off'
    type: time
    start: '{{ now().day-2 }}'
    end: "{{ now().day-1 }}"

  - platform: history_stats
    name: Smartphone A Unlocked Hours Past Three Days
    entity_id: binary_sensor.smartphone_a_device_locked
    state: 'off'
    type: time
    start: '{{ now().day-3 }}'
    end: "{{ now().day-2 }}"

For every day I’ve created a sensor and show these with multiple entity cards on my Lovelace dashboard (with Attribute = Value).
I’ve created the sensors yesterday so I only have data for 1 day in the database. Lovelace reports ‘Unknown’ for 2 and 3 days back. I hope I’ve configured it correct.