Switch off a device via the device_tracker after an adjustable time

I would like to use the device_tracker to track the online time of my children and to block their assigned devices from the internet via a Fritzbox script if the online time is too long.
Currently I have no idea how that could work. I just have created a sensor, which gives me the time, beeing at home:

- platform: history_stats
  name: Smartphone Kid1
  entity_id: device_tracker.kid1smartphone
  state: "home"
  type: time
  start: "{{ now().replace(hour=0, minute=0, second=0) }}"
  end: "{{ now() }}"

But this sensor will give statistics on how much time their devices are connected to the local network, not the amount of time they are actually using the devices to navigate on Internet.

that is right. This is just the time they are connected to Lan or wlan. However, also playing games expects a internet connection. For me it is the best solution to track how long they are connected, independent if they are surfing or playing online games, using whatsapp or whatever. they like to be distracted and neglect their homework

But what about the phone in standby in a table? It still connecting to the wifi from time to time (to refresh email, weather conditions, share a bunch of info with Google and other companies, etc.), and this will count as at home even when no one is really using it.

Anyway, let’s say you have a sensor that precisely measures the time they are navigating, what you want help with?

hi,

  • you could use the switch switch.<your_device_name>_internet_access of the Fritz!Box integration to prevent internet access for a device (you set a device lock in the parental control of the Fritz!Box)
    or:
  • you could use the parental control of the Fritz!Box to set an access/quantity profile for devices/persons (but this doesn’t seem to work out of HA so far, but only in the Fritz!Box itself - I don’t know a way to do this so far)

switch.device is good, but how can I put it in an automation, which switch off the device after a defined time. After a new day it should be resetted to default.

I played with automation.yaml and a first working solution is:

- id: '0018'
  alias: Turn off device
  mode: single
  trigger:
  - platform: state
    entity_id: device_tracker.mydevice
    to: 'home'
    for:
      minutes: '10'
  condition:
  - condition: state
    entity_id: device_tracker.mydevice
    state: 'home'
  action:
    - service: switch.turn_off
      entity_id: switch.mydevice_internet_access  

this turns off the device_tracker device after 10min

There are a lot of ways to automate this and another possibilities to improve this code, but let’s start with something simpler to explain:

description: ""
mode: single
trigger:
  - id: Turn off
    platform: numeric_state
    entity_id: sensor.smartphone_kid1
    above: 7200 # I believe your new sensor is counting seconds, so this is 2 hours of device on home
  - id: Turn on
    platform: state
    entity_id:
      - switch.smartphone_kid1
    to: "off"
    for:
      hours: 8 # 8 hours after the internet was disabled for that device
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: Turn off
        sequence:
          - service: switch.turn_off
            data: {}
            target:
              entity_id: switch.smartphone_kid1_internet_access
      - conditions:
          - condition: trigger
            id: Turn on
        sequence:
          - service: switch.turn_on
            data: {}
            target:
              entity_id: switch.smartphone_kid1_internet_access

This will work, but haven’t you create a sensor specifically to count the total time during the day?
Using this way you have done, if your kids realize the pattern, they will navigate for 9 minutes, disconnect for a few seconds and then they will have 10 more minutes to play, so, in reality, they can easily bypass the limitation.
The sensor you have shared earlier will manage this in a better way (althougth I still believe measuring the time the device is connected to the network is not a good way to measure the time they are using the system).

Isn’t the parental control from your router more efficient?

You are right, that is the problem I found out also some mins ago :crazy_face:. I will try your idea next time. many thx for that.
The parental control with my Fritzbox is also working fine. The Problem is, that is quite often the case, that one kid comes and ask for internet access for doing homework. Than you have to open the fb web interface with smartphone or PC. My smartphone and a button or a slider in homeassistant is my target for doing that.

Ok, then your next question in the community is probably something like:

“How can I use HA to disable the parental control from my Fritzbox for a specific device for some amount of time?”.

:wink:

The script was not working. Is it possible, that the problem is in the trigger? The sensor script use platform history_stats

Please provide more details… What do you mean by “not working”? Was never triggered even with your kids playing for more than 10min? Was triggered when within less than 10min? Was triggered properly but the network wasn’t disabled?
Anything in your logs?

I still don’t believe the phone’s device_tracker is the right sensor to measure the phone usage, so it’s hard to believe you will get there thru this path. Again, managing your router’s parental control is probably much more efficient.

Look my example, my phone is “Home” for the past 10 hours. I haven’t touched the device during these 10 hours. I was sleeping a big part of this time and the fone is in another room. This sensor measure if my phone is at home, not if I’m using it.

So, my suggestion is to take another direction. If your router’s parental control is a solution and your only problem is about giving them more time, I think you have a much easier way to solve it. I don’t know that much about your router and it’s sensors/controls, but maybe we can invest more on that to see if it’s a viable path…
But that should be another thread.

Hi,
i believe the script is not working, because the trigger “turn off” after 7200 (I reduced for test to 60) was never reached. I have tested it with my smartphone, the device inet connection was always possible during all the time.

And how is the value of sensor.smartphone_kid1?

I am quite new wit homeassistant (core), how can I get this info or how can I debug that

Go to Developer Tools, click on the “Status” tab and then type the name of your sensor in the filter.
You will see the status of the sensor (it’s value) at the column “Status”.

THX :+1:

state_class: measurement
unit_of_measurement: h
device_class: duration
icon: mdi:chart-line
friendly_name: smartphone_kid

duration and measurement in hours like 5.7

This is from column Attributes. What about the column “State”?