List last 10 download with Transmission Integration

I’m out of my depth but really want to include a card on my dashboard the lists the last 10 items Transmission has completed. I have the Transmission integration working and I know the even transmission_downloaded_torrent will list the name of the torrent. I also believe this post shows how to do what I want, but I can’t quite work out what I need to change!!

https://community.home-assistant.io/t/list-of-10-last-breached-motion-sensors-doors-windows/515852/8

Can anyone please help me?

Can anyone help? I spent wayy too much time on this yesterday and got nowhere :frowning:

It looks an interesting problem, but I don’t have transmission. Nor time tonight.

Just letting you know someone is watching and will help if I can.

Can you post exactly what is shown in the transmission_downloaded_torrent event?

Thank you!

Now I’ll show you how far out of my depth I am!! How do i know exactly what is shown in the transmission_downloaded_torrent event ?

The information i have came from this comment on the github for the transmission integration page:

"The Transmission integration with Home Assistant also triggers several events, which can be used in various automations, such as notifications:

transmission_downloaded_torrent
transmission_started_torrent
transmission_removed_torrent"

I use transmission_downloaded_torrent to notify my that a download has completed and it gives me the name of the torrent.

The following Trigger-based Template Sensor records the name and time of the last 10 transmission_downloaded_torrent events.

template:
  - trigger:
      - platform: event
        event_type: transmission_downloaded_torrent
    sensor:
      - name: Recent Torrent Downloads
        state: "{{ now().timestamp() | timestamp_custom() }}"
        attributes:
          downloads: >
            {% set current = this.attributes.get('downloads', []) %}
            {% set new = [{
              "name": trigger.event.data.name,
              "time": now().isoformat() }] %}
            {{ (new + current)[:10] }}

You can use a Markdown Card to display the download events using the following template:

|Name||Time|
|:----|:-:|:----|
{% for x in state_attr('sensor.recent_torrent_downloads', 'downloads') | default([], true) -%}
  |{{x.name}}||{{x.time|as_timestamp|timestamp_custom}}|
{% endfor -%}

Let me know if you need any additional assistance.

That has worked an absolute treat!
I have 1 follow up question, which probably isn’t even possible but is there a way to trim the result?

So as an example the download in questions are…
example (2020) season 1 (1080p bla bla bla)
example (2021) season 2 729p bla bla bla
example (2022) season 3 (420p bla bla bla)

I want to trim the end of the torrent name to make it a bit tidier, so it just shows:
example (2020) season 1
example (2021) season 2
example (2022) season 3

I know this is increadably picky but my OCD is kicking in now

Quick and dirty method is to simply slice the first 23 characters.

  |{{x.name[:23]}}||{{x.time|as_timestamp|timestamp_custom}}|

That’s fine in theory, but there are a lot of variables to consider.
What if i download something called:
Another example (2020) season 1 729p bla bla bla
That’s only going to show: “Another example (2020)” which isn’t what i want

I think i would need to slice it after either 1080p, 720p or 480p (and the preceeding ‘(’ if there is one).

Nobody knows what those “variables” are so you will have to provide numerous examples so that we can examine them and devise a technique that always truncates the string correctly. Fact is, that can be a challenge in itself, apart from your original request to simply record and display the last 10 downloads.

Is the name guaranteed to contain one of those strings?

If it did slice the name immediately after one of those strings, then some of your posted examples would look like this:

example (2020) season 1 (1080p

Is that acceptable or would also want it to remove the leading parenthesis before 1080p?

As you can see, the complexity of the ‘name slicing’ template depends on the variability of the names and your personal preferences.

Given that 1080p, 720p and 480p end with 0p followed by a space, this version truncates the name immediately after the first occurance of 0p so it should work the way you requested.

|Name||Time|
|:----|:-:|:----|
{% for x in state_attr('sensor.recent_torrent_downloads', 'downloads') | default([], true) -%}
  |{{x.name[:x.name.find('0p ') + 2]}}||{{x.time|as_timestamp|timestamp_custom}}|
{% endfor -%}
1 Like

Thanks again! I’ve changed it ever so slightly and it works perfectly!

{% for x in state_attr('sensor.recent_torrent_downloads', 'downloads') | default([], true) -%}
  1. {{x.name[:x.name.find('0p') - 3]}}
{% endfor -%}
2 Likes