Get usage per day on Media Player (universal)

It would be really nice if I could get how long time the media player has been active (playing) this day and be able to turn it automatically off if used over a given target.

The reason is that I want to limit how long time my son is sitting on his room watching YouTube. If I could get that number to show on my HA panel and even better, add a automation rule that turns the media player off if over a certal usage time.

Just in case you weren’t aware of the parental timer option in YouTube:

https://support.google.com/youtubekids/answer/6130558?hl=en

YouTube Kids isn’t available in our region.

My son is using normal Chromecasted YouTube from his tablet (with it’s own time limit).

Sorry. I wonder if you could set an input boolean for when the media player starts in play mode and then measure the time elapsed of that state?

Hi @Cato, i`m using the History Statistics Sensor for this.

sensor

- platform: history_stats
  name: PS4 Today
  entity_id: switch.playstation4
  state: 'on'
  type: time
  start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
  end: '{{ now() }}'

automation

- alias: ps4 notifier
  trigger:
    - platform: time
      minutes: '/5'
      seconds: 05
  condition:
    condition: and
    conditions:
    - condition: state
      entity_id: switch.playstation4
      state: 'on'
    # If Notifier = on
    - condition:  state
      entity_id: input_boolean.ps4_alarm
      state: 'on'
    - condition: template
      value_template: '{{ states.sensor.ps4_today.state | float > 2 }}'
  action:
    - service: homeassistant.turn_on
      entity_id: script.ps4_notify

The script then notifies me and let the Ambilight on the TV go crazy. :slight_smile:

3 Likes

@VDRainer I completely forgot about that sensor!! I remember reading the PR and thinking I had some uses for that and then I guess I got sidetracked… thanks for reminding me about it!

Wow, this look excactly like I pictured it!

I’ve now managed to show hours of use for the sensor, but I still can’t make it send a stop command to the Chromecast.

When I use the configuration below, HA GUI sais “unable to connect” and makes me have to log in agay. But it will never do. To me it seems the action bit probably is the problem. Tried to read the documentation on how to set it up, but not 100% if it’s correct.

From the error log:

2017-05-09 04:55:33 ERROR (MainThread) [homeassistant.bootstrap] Error loading /home/homeassistant/.homeassistant/configuration.yaml: mapping values are not allowed here
  in "/home/homeassistant/.homeassistant/configuration.yaml", line 78, column 14

My configuration:

sensor:
  - platform: history_stats
    name: Oliver ChromeCast i dag
    entity_id: media_player.oliver
    state: 'playing'
    type: time
    start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
    end: '{{ now() }}'

automation:
  trigger:
    platform: time
      minutes: '/5'
      seconds: 05
  condition:
    condition: and
    conditions:
    - condition: state
      entity_id: media_player.oliver
      state: 'playing'
    - condition: template
      value_template: '{{ sensor.oliver_chromecast_i_dag.value | float > 3 }}'
  action:
    service: media_stop
      entity_id: media_player.oliver

Any ideas?

What is line 78 in your config?

Line 78 is: minutes: '/5'

I did a couple of changes:

automation:
  trigger:
  - platform: time  <--- added hyphen before 'platform'

  action:
    - service: media_player.media_stop  <--- added hyphen before 'service' and domain before 'media_stop'
      entity_id: media_player.oliver

Now HA GUI loads and I get the automation in the list.

When my son comes crying later today I will know if it works… :wink:

Thanks for the help dudes!

Minutes and seconds should line up under platform, not be indented. That’s the reason for the mapping not allowed here error.