My kids have just started getting in to playing on the xbox. They are still young so I dont want them getting hooked,. and therefore want to limit the time they use it per day. The microsoft / xbox family safe is limited in its features but could be an option. The issue is, they will just go on the TV once the Xbox time is up.
So my plan is to have the TV and Xbox, connected to a smart plug. Then I will time limit the smart plug so everything turns off once the time limit is reached.
The time limit can be spread across the whole day. So if its a 40min daily limit, they might want 10 mins in the morning, and 30 mins in the evening, etc.
I was wondering if I could get some guidance on the best methods to achieve this. Will I need to set up Binary sensors etc?
There are many ways to achieve what you want. A very simple approach consists of a combination of a sensor that counts the time and resets once a day and a simple automation.
You add the sensor template in the configuration.yaml.
template:
- trigger:
- platform: time_pattern
seconds: "/60"
sensor:
- name: "xbox_runtime_today"
unit_of_measurement: "min"
state: >
{% set last = this.state() | float(0) %}
{% set power = state('sensor.tapo_p110m_power') | float(0) %} # REPLACE BY YOUR PLUG NAME
{% if power > 20 %} # SET W THRESHOLD
{{ (last + 1) | round(1) }}
{% else %}
{{ last }}
{% endif %}
automation:
- alias: "Xbox Access Control"
trigger:
- id: reset
platform: time
at: "03:00:00" # RESETS AT 3 AM
- id: limit
platform: numeric_state
entity_id: sensor.xbox_runtime_today
above: 120 # MAX TIME PER DAY
action:
- choose:
- conditions:
- condition: trigger
id: reset
sequence:
- service: homeassistant.reload_config_entry
target:
entity_id: sensor.xbox_runtime_today
- conditions:
- condition: trigger
id: limit
sequence:
- service: switch.turn_off
target:
entity_id: switch.xbox # REPLACE WITH YOUR PLUG DEVICE
But I trust that the HA Pros will come uo with something shorter
arrrggg man, thanks so much for writing all that code out for me. Really appreciate it.
I’ll give that a go
I’ve typically done must of my setup (binary sensors, etc) using the GUI, I’ve not really experimented the configuration.yaml yet. Is it easy to do? Do I need to place the code in a specific location?
I used the GUI myself to draft the code, simply replaced my sensor names by generic ones. It’s not very elegant and there are certainly better and more efficient ways.
It’s like editing any text file. You will need an editor though. I recommend Blueprint Studio (installed through HACS).
The code goes anywhere in the file, just watch indentation (2 spaces) and that there is only one template: in case you already have similar content. In the long run you may move such definitions out of configuration and place them in own files in subdirectories which you add with !include commands. You will those in the file.
Now things start getting more complicated. The OP would need to look for a Xbox integration and send it to sleep before shutting off power. However, Microsoft allows to set use times per account. See Xbox Family Settings-App (German site). That would leave just TV.
Now kids are creative. They will probably quickly discover that the smart plug has s button to switch it on.