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.