Transmission: Add torrent ID to event data

The Transmission integration provides a useful transmission_downloaded_torrent event, and a useful transmission.remove_torrent service, however using these together is not so easy since the event only includes the torrent name in the event data.

For example if I want to do something with the downloaded file after it’s complete such as remove it from the torrent client which is done by ID, based on the example automation I expected to be able to do something like this:

alias: Remove on successful download
trigger:
  platform: event
  event_type: transmission_downloaded_torrent
action:
  service: transmission.remove_torrent
  data:
    name: Transmission
    id: "{{trigger.event.data.id}}"

But id doesn’t seem to exist on data, only name exists. So for now I have to do something like this:

alias: Remove on successful download
trigger:
  platform: event
  event_type: transmission_downloaded_torrent
action:
  - delay: '00:00:02'
  - service: transmission.remove_torrent
    data:
      name: Transmission
      id: >-
        {{ state_attr('sensor.transmission_completed_torrents',
        'torrent_info')[trigger.event.data.name].id | int }}

(I seem to have to wait a couple seconds for sensor.transmission_completed_torrents to update)

The first example would be much better. Can the torrent ID be added to the event data? Or perhaps someone like @engrbm87 or @JPHutchins knows why the ID is not present?

Also discussed in: Transmission remove_torrent

Great idea! I hate jinja, so verbose and ugly! Now that I look at the jinja more closely it looks like I should preemptively cast the id to int? See PR: https://github.com/home-assistant/core/pull/44187

1 Like

Fantastic @JPHutchins thank you!

Not sure if it is just me, but I upgraded to the latest version of HA earlier today, and ever since, it has been spamming my event bus with torrent completion events, even though the torrents have been completed days ago.

Count so far is about 120 events in the last 2 hours.

Edited: On further investigation, it looks worse than I originally thought. Every 2 minutes, every single torrent is generating a transmission_downloaded_torrent event and a transmission_removed_torrent event. I currently have 47 torrents, so 94 events every 2 minutes.

It’s not just you, the patch is in: https://github.com/home-assistant/core/pull/46190

Thanks. I will turn off my notifications until it gets merged and keep an eye on my DB size just in case it goes nuts.