% of Time per Week / Month using a Media Player

Screenshot_20171027_155306

Change this bit in the code below

entity_id: media_player.hifi_pi

to suit your media player, can be set to Kodi and used to show Video usage instead for instance.

  - platform: history_stats
    name: Music Listening This Week
    entity_id: media_player.hifi_pi
    state: 'playing'
    type: time
    start: '{{ as_timestamp( now().replace(hour=0).replace(minute=0).replace(second=0) ) - now().weekday() * 86400 }}'
    end: '{{ now() }}'

  - platform: history_stats
    name: Music Listening This Month
    entity_id: media_player.hifi_pi
    state: 'playing'
    type: time
    end: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
    duration:
      days: 30

  - platform: template
    sensors:
      percent_playing_music_weekly:
        unit_of_measurement: "%"
        value_template: "{{ '%.1f'|format(states('sensor.music_listening_this_week')|float * 100 / 168) }}"

  - platform: template
    sensors:
      percent_playing_music_monthly:
        unit_of_measurement: "%"
        value_template: "{{ '%.1f'|format(states('sensor.music_listening_this_month')|float * 100 / 720) }}"

And for the groups.yaml

Media Activity:
  view: no
  entities:
  - sensor.percent_playing_music_weekly
  - sensor.percent_playing_music_monthly
9 Likes

Just an update variation for more info, if preferred:

This code:

group.yaml

Media Players:
  view: yes
  entities:
  - group.amp_kodi
  - group.hifi_pi

Amp Kodi:
  view: no
  entities:
  - sensor.video_watching_this_week
  - sensor.percent_watching_video_weekly
  - sensor.video_watching_this_month
  - sensor.percent_watching_video_monthly
  - media_player.amp_kodi  

HiFi Pi:
  view: no
  entities:
  - media_player.hifi_pi
  - sensor.music_listening_this_week
  - sensor.percent_playing_music_weekly
  - sensor.music_listening_this_month
  - sensor.percent_playing_music_monthly

The sensors.yaml is the same as above.

The customize.yaml:

sensor.percent_playing_music_weekly:
  friendly_name: Weekly Listen
  icon: mdi:music-circle
sensor.percent_playing_music_monthly:
  friendly_name: Monthly Listen
  icon: mdi:music-box-outline
sensor.percent_watching_video_weekly:
  friendly_name: Weekly Watch
  icon: mdi:filmstrip
sensor.percent_watching_video_monthly:
  friendly_name: Monthly Watch
  icon: mdi:movie
sensor.music_listening_this_week:
  friendly_name: Weekly Listen Hours
  icon: mdi:music-circle
sensor.music_listening_this_month:
  friendly_name: Monthly Listen Hours
  icon: mdi:music-box-outline
sensor.video_watching_this_week:
  friendly_name: Weekly Watch Hours
  icon: mdi:filmstrip
sensor.video_watching_this_month:
  friendly_name: Monthly Watch Hours
  icon: mdi:movie
2 Likes

This is really cool, might add it in for my TV, see how much time I waste each month haha.

1 Like

hey there!

I was going to try this but i can’t find the Custom Component code for ‘history_stats’ … is it there and am I missing it?

thanks for sharing!! :slight_smile:

1 Like

Oh, you don’t need a custom component for this one.

It’s using:

Which should be in your Home Assistant install already :slight_smile:

Ouch, my fault.

Sorry sir, thank you for the help :slight_smile:

1 Like

Hey, this is a grat idea! I just implemented it :slight_smile:
Thanks for sharing!

1 Like

Thank you very much.

1 Like

How to create sensor for video watching? I’d like to use this component for TV ‘on’ weekly, but i see your sensors are used only for music listening…

if the video is as your media_player in ha. You can track from the status of it. example ‘playing’

Ok, thanks… and how to change these sensors?

  - platform: template
    sensors:
      percent_playing_music_weekly:
        unit_of_measurement: "%"
        value_template: "{{ '%.1f'|format(states('sensor.music_listening_this_week')|float * 100 / 168) }}"

  - platform: template
    sensors:
      percent_playing_music_monthly:
        unit_of_measurement: "%"
        value_template: "{{ '%.1f'|format(states('sensor.music_listening_this_month')|float * 100 / 720) }}"

You need to create history_stats first see from 1st post. then create the template sensor to read out in %