Automate Time Limit on PS4

Hi All,

I am looking at a way to limit access to our PS4 (Son is obsessed with fortnite).

Here is what I am looking for:

  1. Limit PS4 for 3 hours daily
  2. Turn off PS4 automatically if time limit reached or if 9.30pm.
  3. If PS4 turned back on between 9.30pm - 8am, then turn off automatically
  4. Announce via Google TTS that PS will turn off with 5 min warning
  5. Announce via Google TTS that PS is on.

I have points 2 - 5 pretty much working, but looking for a way to calculate the length it is on in 1 day.

I know you can setup limitations in PS4 and family management, but this is not possible for me as my son uses my account. Long story but that is how it is.

I have been using the media turn off in HA to turn off the PS4, but this can be a bit unreliable if in the middle of a game. I have added a TP-LINK switch to the PS4 but this turns off the PS ungracefully and not nice for the PS. Would prefer to avoid if possible.

This will be a great way to track time used and possibly what is being played. These can be put into Grafana so I can get a better over view of usage.

There must be other parents out there with the same concerns who could also benefit from this.

Having this automated will also avoid those horrible arguments at bedtime and we can all sleep nicely :slight_smile:

Any help would be most appreciated.

https://www.home-assistant.io/integrations/history_stats/

There’s an example on the page that will show usage since midnight.

Same problem with XBOX. Here is my solution:

##########################################################
## Xbox ON
##########################################################
- alias: XBox ON
  initial_state: True

  trigger:
    - platform: state
      entity_id: binary_sensor.xbox
      from: 'off'
      to: 'on'
      for:
        minutes: 5

    - platform: time_pattern
      hours: "/1"
      minutes: 00
      seconds: 00

  condition:
    - condition: state
      entity_id: binary_sensor.xbox
      state: 'on'

  action:
    - wait_template: "{{ is_state('script.notify_ios_engine' , 'off') }}"

    - service: script.notify_ios_engine
      data_template:
        message: >-
          XBOX on for {{ state_attr('sensor.xbox_on_daily','value') }} today and {{ state_attr('sensor.xbox_on_weekly','value') }} this week.
        who: "family"

##########################################################
## Xbox off
##########################################################
- alias: XBox Off
  initial_state: True

  trigger:
    - platform: state
      entity_id: binary_sensor.xbox
      from: 'on'
      to: 'off'
  
  action:
    - wait_template: "{{ is_state('script.notify_ios_engine' , 'off') }}"

    - service: script.notify_ios_engine
      data_template:
        message: >-
          XBOX turned off.  On for {{ state_attr('sensor.xbox_on_daily','value') }} today and {{ state_attr('sensor.xbox_on_weekly','value') }} this week.
        who: "family"
- platform: history_stats
  name: 'Xbox On Weekly'
  state: 'on'
  type: ratio
  entity_id: binary_sensor.xbox
  start: '{{ as_timestamp( now().replace(hour=0).replace(minute=0).replace(second=0) ) - now().weekday() * 86400 }}'
  end: '{{ now() }}'

- platform: history_stats
  name: 'Xbox On Daily'
  state: 'on'
  type: ratio
  entity_id: binary_sensor.xbox
  start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
  end: '{{ now() }}'
xbox:
  friendly_name: "XBOX On"
  device_class: power
  value_template: "{{ is_state('media_player.roku_yn00fj223491', 'playing') and is_state_attr('media_player.roku_yn00fj223491','source','XBOX') }}"

First off, let me start by welcoming you to the forum @Mark_Wright

The simplest solution I could think of is plugging the PS4 / XBOX (or whatever else appliance you want to limit the use of) to a Sonoff S26 or similar with Tasmota that could give you the power consumption of the plug.

On top of this I’d add a template sensor which is on only while the power consumption is above a threshold. This will ensure that while the PS4 is in standby mode, the time towards the limit doesn’t count as the console being on.

The last sensor would be the same history_stats configuration that @jwelter showed in their post

The automation you could define is one automation which could have multiple triggers:

  1. 9:30PM is here - turn off the plug
  2. The history_stats sensor has reached 3 as its value - announce that it’s going to shutdown soon and turn off the plug
  3. Turn it back on whenever you need.
1 Like

The only issue is if a PS4 is like an XBOX just doing a power kill is not a good way to shut it down as it can corrupt the current game status.

So we’ve taken to notifying myself and my wife and we just tell him to turn it off which works.

I was thinking about killing the network connection as I can disable a port under HA control but that would impact the game save as well as most of that is now cloud based.

@jwelter I have BT whole home wifi and took to turning off the internet at 9.30.
However, he can still then play minecraft and spiderman without internet. It also means I have no control from HA without wifi connection, so am blind as to what is going on.
Killing the power is bad for hard disk & OS as well as game progress.

This is why I am looking for a graceful way and that the googles will give him a 5 mins heads up.

I will look to modify your code, but the PS shows different states when on.
For example, when you first turn it on and logged in, it is in idle state. Then when you play a game it will change state, so I need to look out for all of them before executing the turn off.

I like the look of the history_stats. Surprise I haven’t come across it before. Will take a deeper look into using them.

From my experience though, when something is automated like this as well as turning off the wifi for their devices, there is little to no arguments, which is what I am aiming to achieve.
The thought of using technology against technology is ironic and has an air of Terminator about it.
How us humans evolve

It comes down to the relationship you want to establish. I get that automatically just “shutting off” is the non-confrontational way to handle it but think what life lessons you are missing the opportunity to teach?

I for one feel kids need to know the authority chain and also should be encouraged to discuss such rules to understand why we have them in place and how they can be changed.

It’s led us to good talks like “Ok, we get you want more XBOX time. If you get on the honor roll we can discuss.” and our son went from being a mid 70’s student to mid 80’s (and on honor role) to get what he wanted.

Life is about working hard to get what we want. Teaching that lesson early is a good thing.

1 Like

OK. I think I have nailed what I am after with the help of you guys.

I am sure this can be adjusted for other media\devices, such as Xbox, TV etc if anyone is interested.

First off the following sensors will track the daily & weekly time used. Thanks @Kermit Kermit for the heads up with history stats. Can’t believe I missed them.

There are 4 sensors, Idle, playing, standby and weekly. This is in my sensor.yaml, but you can add to your configuration.yaml as a sensor if needed.

Please note Idle is when the PS4 is on but no games being played, Playing is what it says on the tin & so is standby when in Rest Mode. Please note though that the PS4 cannot be reached if powered OFF.

  - platform: history_stats
    name: PS4 idle
    entity_id: media_player.playstation_4
    state: 'idle'
    type: time
    start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
    end: '{{ now() }}'

  - platform: history_stats
    name: PS4 standby
    entity_id: media_player.playstation_4
    state: 'standby'
    type: time
    start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
    end: '{{ now() }}'

  - platform: history_stats
    name: PS4 Playing
    entity_id: media_player.playstation_4
    state: 'playing'
    type: time
    start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
    end: '{{ now() }}'

  - platform: history_stats
    name: PS4 Weekly (Playing)
    entity_id: media_player.playstation_4
    state: 'playing'
    type: time
    start: '{{ as_timestamp( now().replace(hour=0).replace(minute=0).replace(second=0) ) - now().weekday() * 86400 }}'
    end: '{{ now() }}'

I have 3 automations setup.
First one is to notify my google speaker group and pushbullet the PS4 has been turned on (idle)


- id: '1577798628437'
  alias: Playstation - On alert
  description: ''
  trigger:
  - entity_id: media_player.playstation_4
    from: standby
    platform: state
    to: idle
  - entity_id: media_player.playstation_4
    from: unknown
    platform: state
    to: idle
  condition: []
  action:
  - data:
      message: Playstation is now on
    service: notify.mark
  - data:
      entity_id: media_player.speakers
      message: Playstation is now on.
    service: tts.google_say

Second automation will alert via googles speaker group that 3 hours has been reached and then turn off playstation with a 5 min delay to allow saving of game progress. It will check every 5 mins and turn off if turned on again until midnight when the sensor stats reset.

 id: '1578167853488'
  alias: Playstation - 3hr Time Limit
  description: ''
  trigger:
  - above: '3'
    entity_id: sensor.ps4_playing
    platform: numeric_state
  - minutes: /5
    platform: time_pattern
  condition:
  - condition: or
    conditions:
    - condition: state
      entity_id: media_player.playstation_4
      state: idle
    - condition: state
      entity_id: media_player.playstation_4
      state: playing
  action:
  - data:
      entity_id: media_player.speakers
      message: Your 3 hour daily limit on the playstation has been reached. Turning
        off in 5 minutes
    service: tts.google_say
  - delay: 00:05:00
  - alias: ''
    data: {}
    entity_id: media_player.playstation_4
    service: media_player.turn_off

3rd is to alert and turn off at 9.30pm if idle or playing with a 3 minute delay. This will check every 5 mins from 9.30pm to 8am and turn off if turned back on again.

 id: '1578167853488'
  alias: Playstation - 3hr Time Limit
  description: ''
  trigger:
  - minutes: /5
    platform: time_pattern
  condition:
  - condition: and
    conditions:
    - above: '3'
      condition: numeric_state
      entity_id: sensor.ps4_playing
  - condition: or
    conditions:
    - condition: state
      entity_id: media_player.playstation_4
      state: idle
    - condition: state
      entity_id: media_player.playstation_4
      state: playing
  action:
  - data:
      entity_id: media_player.speakers
      message: Your 3 hour daily limit on the playstation has been reached. Turning
        off in 5 minutes
    service: tts.google_say
  - delay: 00:05:00
  - entity_id: media_player.playstation_4
    service: media_player.turn_off

I am sure these will be tweaked as time goes on and welcome any constructive feedback or optimisations of the code only. Parenting advice can be left on another forum. :slight_smile:

3 Likes

Thank you for sharing this :slight_smile:
Is this last automation a duplicate of number 2?

Yes, you are absolutely right.

This is the 3rd automation:

- id: '1577724123801'
  alias: PlayStation - Turn Off @ 9.30pm
  description: ''
  trigger:
  - minutes: /5
    platform: time_pattern
  condition:
  - condition: and
    conditions:
    - after: '21:30'
      before: 08:00
      condition: time
  - condition: or
    conditions:
    - condition: state
      entity_id: media_player.playstation_4
      state: idle
    - condition: state
      entity_id: media_player.playstation_4
      state: playing
  action:
  - alias: ''
    data:
      entity_id: media_player.speakers
      message: Playstation is turning off now as it is either gone 9 30 pm or before
        8 am. Turning Playstation off now.
    service: tts.google_say
  - delay: 00:03:00
  - entity_id: media_player.playstation_4
    service: media_player.turn_off
1 Like