Qbittorrent - pass name of last torrent started to input_text helper

I am having a really hard time trying to get a working automation that will show the name of the last torrent started in qbit. I have read the documentation but the action seems only half explained.

Does anyone do this and can share their yaml?

End goal is to trigger another automation if the name of the last torrent to start downloading contained ABC

There is a way to send a webhook from Qbittorrent to HA. I’ll have to wait 'til I get home to take a look at it as it’s in a batch file that automatically runs after torrent added.

:crossed_fingers: If I remember :slight_smile:

OK, in Qbittorrent, Options, Downloads. Check ‘x’ “Run external program on torrent finished”, then in that field, put “C:\downloads\notify.bat %N”.

*I just thought, you probably won’t need a batch file, just put the curl command instead of the above notify.bat, but I’ve typed all this so it’s staying.

Then make that batch file “notify.bat”

In the batch file

curl -X POST -H "Content-Type: application/json" -d "{\"torrent_name\":\"%1\"}" http://[YOUR-IP]:8123/api/webhook/qbittorrent_torrent_added

Don’t forget to add your HA IP address.

This is your automation, put notifications or whatever in the actions:

alias: New Torrent Added
description: ""
triggers:
  - trigger: webhook
    allowed_methods:
      - POST
      - PUT
    local_only: true
    webhook_id: qbittorrent_torrent_added
conditions: []
actions:
    [YOUR ACTIONS HERE]
mode: single

Awesome, thanks. I’ll have a play with it. Appreciate your time in posting all the above.

Just had a thought, if you put the line in Qbittorrent and not the batch file, use this instead (changed %1 to %N)

curl -X POST -H "Content-Type: application/json" -d "{\"torrent_name\":\"%N\"}" http://[YOUR-IP]:8123/api/webhook/qbittorrent_torrent_added

Thanks again, Qbit seems happy with the curl directly, but when it gets to HA it’s throwing an error:

Logger: homeassistant.components.webhook
Source: components/webhook/__init__.py:192
integration: Webhook (documentation, issues)
First occurred: 25 November 2024 at 21:47:56 (8 occurrences)
Last logged: 13:35:46

Error processing webhook qbittorrent_torrent_added
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/webhook/__init__.py", line 192, in async_handle_webhook
    response: Response | None = await webhook["handler"](hass, webhook_id, request)
                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/webhook/trigger.py", line 64, in _handle_webhook
    base_result["json"] = await request.json()
                          ^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/__init__.py", line 307, in json
    return json_loads(await self.read())
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/util/json.py", line 44, in json_loads
    return orjson.loads(__obj)  # type:ignore[no-any-return]
           ^^^^^^^^^^^^^^^^^^^
orjson.JSONDecodeError: unexpected character: line 1 column 2 (char 1)

Are you running Qbittorrent on Windows or Linux?

My command is for Windows, try this instead.

curl -v -X POST -H "Content-Type: application/json" -d '{"torrent_name":"%N"}' http://[YOUR-IP]:8123/api/webhook/qbittorrent_torrent_added

Thanks for all your help again. I am running qbit in a docker container on Debian.

It took some iterations, but I finally got it working. I couldn’t get a curl command to work directly in qbit, so ended up with a bash script. Even then, any _ would cause an issue for some reason.

For anyone else that might find this, the final solution for me was as follows:

In qbit, tick the run external option in downloads and add this:

/config/ha.sh "%N"

Then in the qbit config folder add the file ha.sh with the following contents:

curl -v POST -H "Content-Type: application/json" -d '{"torrent":"'"$1"'"}' http://[YOUR IP ADDRESS]:8123/api/webhook/qbittorrent_torrent_added

In HA you can then have an automation with triggers and actions like these (this one just sends the torrent’s name through to Telegram for example)

- id: '123'
  alias: New Torrent Added
  description: "Torrent added"
  triggers:
    - trigger: webhook
      allowed_methods:
        - POST
        - PUT
      local_only: true
      webhook_id: "qbittorrent_torrent_added"
  action:
  - data:
      message: "{{ trigger.json.torrent }}"
    service: notify.telegram_me
  mode: single