Transmission remove_torrent

Hi Community,

I have an automation to send me a notification when Transmission finished downloading a torrent. I want to ad an action in this automation that uses the remove_torrent service and remove the torrent that was just finished. I do not know how to write the code to get the correct ID of that specific torrent. This is how my code looks when I did a very poor effort. Thanks in advanced for all your help!

  - alias: Completed Torrent
    trigger:
      platform: event
      event_type: transmission_downloaded_torrent
    action:
      - service: notify.mobile_iphone
        data_template:
          title: "Nedladdning färdig!"
          message: "Filen {{trigger.event.data.name}} är nerladdad."
      - service: transmission.remove_torrent
        data_template: >
          {% set payload = state_attr('sensor.transmission_completed_torrents', 'torrent_info') %}

          {% for torrent in payload.items() %} {% set data = torrent[1] %}

          id: {{ data.id }} {% endfor %}

This is the error message i get in the log.

Invalid config for [automation]: expected dict for dictionary value @ data[‘action’][1][‘data_template’]. Got None. (See /config/configuration.yaml, line 108).

I’d like to make the same automation. I expected the ID of the completed torrent to be available at {{trigger.event.data.id}} based on the docs but I see that only name is provided. This is a feature request I guess.

This should work, but I’m sure could be written better.

alias: Remove torrent on successful download
trigger:
  platform: event
  event_type: transmission_downloaded_torrent
action:
  service: transmission.remove_torrent
  data:
    name: Transmission
    id: >
      {% for torrent in state_attr('sensor.transmission_completed_torrents',
      'torrent_info').items() %}{{ torrent[1].id | int }}{% endfor %}

Aside from the feature request, I think ideally we would want something like this:

id: "{{ states.sensor.transmission_completed_torrents.attributes.torrent_info[trigger.event.data.name].id | int }}"

Hi Alec,

Thanks for your reply. I have tried your code but it is still not working for me.

My code now looks like this

  - alias: Completed Torrent
    trigger:
      platform: event
      event_type: transmission_downloaded_torrent
    action:
      - service: notify.mobile_app_tobias_beckmans_iphone
        data_template:
          title: "Nedladdning färdig!"
          message: "Filen {{trigger.event.data.name}} är nerladdad."
      - service: transmission.remove_torrent
        data:
          name: Transmission
          id: >
            {% for torrent in state_attr('sensor.transmission_total_torrents', 'torrent_info').items() %}
              {{ torrent[1].id | int }}
            {% endfor %}

The error message I can see in my log looks like this.

Logger: homeassistant.components.automation.completed_torrent
Source: core.py:1405
Integration: Automation ([documentation](https://www.home-assistant.io/integrations/automation), [issues](https://github.com/home-assistant/home-assistant/issues?q=is%3Aissue+is%3Aopen+label%3A%22integration%3A+automation%22))
First occurred: 9:05:53 (2 occurrences)
Last logged: 9:10:50

While executing automation automation.completed_torrent

Traceback (most recent call last): 
File "/usr/src/homeassistant/homeassistant/components/automation/__init__.py", line 433, in async_trigger await self.action_script.async_run( 
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1010, in async_run await asyncio.shield(run.async_run()) 
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 245, in async_run await self._async_step(log_exceptions=False) 
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 253, in _async_step await getattr( 
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 460, in _async_call_service_step await service_task 
File "/usr/src/homeassistant/homeassistant/core.py", line 1405, in async_call processed_data = handler.schema(service_data) 
File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 272, in __call__ return self._compiled([], data) 
File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 594, in validate_dict return base_validate(path, iteritems(data), out) 
File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 432, in validate_mapping raise er.MultipleInvalid(errors) 
voluptuous.error.MultipleInvalid: expected int for dictionary value @ data['id']

I do get the notification to my phone correctly but the torrent is not removed. I can also see that my sensor.transmission_total_torrents does contain the correct downloaded torrents but still they are not removed.

Thanks in advanced for your help!

I can see indentation and spaces in your example. It needs to be copy/pasted exactly as I had to ensure an integer is used as the value :slightly_smiling_face:

Like I say this is very poorly written, need someone who knows Python/this template syntax to improve it, and even with better syntax it’s still not ideal, the second example is the one to strive for.

I should also mention in case you don’t know, that if you don’t want to seed torrents you can just set the seeding ratio to 0.

Hi Alec!

Big thanks for all your help. Now it works just fine thanks!

No problem. I recommend putting the notification last so you know the torrent was successfully removed when you receive the notification.

I’ve looked a bit closer at this and figured out the prefered solution which is much better. It seems we must wait a couple of seconds for the sensor to be updated when the event fires.

This works reliably for me:

alias: Remove torrent 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 }}

Let us know if access to any other attributes would be useful! Anything you can think of is possible but we want to keep it limited to generally useful attributes (e.g. seed ratio, start date, end date, etc.).

Here is the specification:

Here is a cool frontend for Transmission if you use HACS or can install custom component:

Here are some current Transmission PRs at github:


1 Like

To automate file processing, I would love to have access from the event trigger data to the downloadDir property of the torrent. Hard-coding the default download directory in a template is just to fragile.

The files property would be a nice to have.

1 Like

Hi Alec,

After reading that Transmission integration was updated I tried to change your code as seen below but it did not work for me. Have you been able to update your working code with their new updates?

  - alias: Completed Torrent
    trigger:
      platform: event
      event_type: transmission_downloaded_torrent
    action:
      - delay: '00:00:02'
      - service: transmission.remove_torrent
        data:
          name: Transmission
          id: "{{trigger.event.data.id}}"
      - service: notify.mobile_app_tobias_beckmans_iphone
        data_template:
          title: "Nedladdning färdig!"
          message: "Filen {{trigger.event.data.name}} är nerladdad."

@weckma the changes linked above are yet to be merged, and then they need to be included in a Home Assistant release. I’ll update this thread when the changes are available to use, then your example should work.

By the way I believe due to the excellent changes @JPHutchins is making, the delay step won’t be necessary.

Gentlemen, I am about to solve the same task of torrent removal from the list, though documented service

transmission.remove_torrent

with data

name: Transmission
id: 1

Simply doesn’t work…

@weckma these changes are released now! If you have at least Home Assistant 2021.2.0 the following should work with no delay necessary:

- service: transmission.remove_torrent
  data:
    name: Transmission
    id: "{{trigger.event.data.id}}"
1 Like

I’m a bit late to the party but I can’t get this to work. I have the notification part nailed down but cannot work out how to delete the file - i get this error:

extra keys not allowed @ data['name']