Monitor your DVR activity with Home Assistant

I got inspired by @Ahwman in this thread:

That got me thinking about monitoring the DVR activity in real time and more specifically what it is recording. I came up with this sensor in Home Assistant:

sensor:
  - platform: rest
    name: channels_dvr_recordings
    resource: http://192.168.4.100:8089/dvr
    value_template: >
      {% set recordings = namespace(list=[]) %}
      {% for key, value in value_json.activity.items() %}
        {% if 'Recording' in value %}
          {% set program_name = value.split(' for ')[1].split(' until')[0].strip() %}
          {% set recordings.list = recordings.list + [program_name] %}
        {% endif %}
      {% endfor %}
      {% if recordings.list | length == 0 %}
        No recordings in progress
      {% else %}
        {{ recordings.list | join(', ') }}
      {% endif %}
    scan_interval: 60

This sensor in Home Assistant will check every minute what the DVR is recording and will contain a string with the names of all the programs currently being recorded. Something like this:

As seen in Channels itself:

image

So what can you do with this?
Well, you can use it as a trigger to check if a specific program is being recorded.

Here is an example of automation to match what @Ahwman wanted to achieve:

- id: '<Redacted>' 
  alias: NHL Hockey is being recorded
  description: ''
  trigger:
  - platform: state
    entity_id:
    - sensor.channels_dvr_recordings
  condition:
  - condition: template
    value_template: '{{ ''NHL Hockey'' in states(''sensor.channels_dvr_recordings'')
      }}'
  action:
  - action: <turn the TV on, whatever...>

In this post, the main focus was to retrieve the current recordings but you could easily tweak the sensor to monitor other activities from the DVR (the part {% if 'Recording' in value %}) and do whatever.

Some ideas:

  • if the DVR is busy recording, turn a light on in red
  • if somebody starts watching something from the library, dim the lights in the room

Really whatever, this is Home Assistant after all, the possibilities are endless. :slight_smile:

I hope this is useful for somebody.

This is truly brilliant! It looks like you put some serious work into this, I can’t wait to play around with it. Thanks so much for sharing! :grinning:

1 Like

Here is the sensor to monitor if somebody is watching something from the library:

  - platform: rest
    name: channels_dvr_watching
    resource: http://192.168.4.100:8089/dvr
    value_template: >
      {% set watching = namespace(list=[]) %}
      {% for key, value in value_json.activity.items() %}
        {% if 'Watching' in value %}
          {% set program_name = value.split('Watching ')[1] %}
          {% set watching.list = watching.list + [program_name] %}
        {% endif %}
      {% endfor %}
      {% if watching.list | length == 0 %}
        No watching in progress
      {% else %}
        {{ watching.list | join(', ') }}
      {% endif %}

Notice that I didn't specify the scan interval. In this case, it uses the default value of 30 seconds.

For fun, I added a page on my Home Assistant dashboard with two cards:

Not particularly very useful until you create automations. :slight_smile:

I just got around to creating the sensors and they work like a charm! I wish I had a small fraction of the coding prowess that you possess, well done my friend. :grinning:

2 Likes

Thank you but I can't take all the credits. I had help from a friend: first name = Chat, last name = GPT. :grin:

Well you’re the one who put it all together and had the original idea. In any case, I will be using these templates in several automations. Fun stuff for a nerd like me :grinning:.

1 Like

thanks

1 Like

Don’t know if you’re still monitoring this thread. If so. I have a new challenge that has me stuck and wondered if you're up for it :grinning:. I’m looking to retrieve the json of all unwatched recordings from the channels api using the following url: http://192.168.1.100:8089/api/v1/all?source=recordings&watched=false. I would then like to extract the id and show_id keys for each recording. Using your previous sensor as a trigger, this would then allow me to use a deep link to play a specific recording via an automation action. I’ve spent many hours with ChatGPT but haven’t stumbled onto the solution…

Just to make sure I understand, are you trying to create a new sensor that will contain the IDs of all unwatched recordings? :slightly_smiling_face:

Exactly. The sensor would contain all unwatched recording IDs, as well as the title. I could then use this information combined with a deep link to play that content via an automation. This would be so helpful. :grinning:

I will look into it when I have some spare time. I can't say when exactly. :face_with_diagonal_mouth:

No worries, I couldn’t ask for more. In fact, I want to thank you again for your last scripts as I use them on a daily basis.

Have a happy new year! :grinning: