Extracting JSON array value from Sonarr webhook

I’m looking at using the webhook function in Sonarr to get a notification in the HA android app when new downloads are added. I have set up the automation and added the following to the Action part of the Automation.

service: notify.mobile_app
data:
  title: Sonarr
  message: '{{ trigger.json.series.title }} added to media library!'

I have taken the series.title part from the webhook output as follows;

{
  "series": {
    "id": 1,
    "title": "Test Title",
    "path": "C:\\testpath",
    "tvdbId": 1234,
    "tvMazeId": 0,
    "type": "standard"
  },
  "episodes": [
    {
      "id": 123,
      "episodeNumber": 1,
      "seasonNumber": 1,
      "title": "Test title"
    }
  ],
  "eventType": "Test"
}

I’d also like to extract and include the episode and season number from the episodes sections as well but cannot fathom how to do it. I’ve tried adopting the solution from the below but can’t get it to work.

https://community.home-assistant.io/t/solved-extract-a-nested-array-value-in-json/173906

Any pointers would be appreciated

In case you’re still after this, here is what I’ve just implemented :

automation:
  - id: 5dc226595a52 
    trigger:
      - platform: webhook
        webhook_id: sonarr
    action:
      - service: notify.my_signal
        data:
          message: |
            {{ trigger.json.series.title }},
            episode "{{ trigger.json.episodes[0].title }}" was added.

Hope it gets you on the right track !

Can I ask how you have this setup? I’m trying to get sonarr/radarr to call HASS via a Webhook, but nothings happening. They say they’ve made the call ok, but HASS doesn’t detect it (it does detect it if I call the same URL from the command line). Ta

Are you using Home Assistant Cloud for the webhook? If you are, I created the automation with a random value for the webhook, then copied the link from the HA Cloud part of config into Radarr. If you are not using HA CLoud and just a DDNS url then make sure the Webhook ID in the automation matches Sonarr/Radarr

I then use this is in the notification part of the automation

I did a similar thing a while back and just stumbled over this post. I just wanted to add my little update that drove me nuts for a long time. I wanted to know what Season and Episode had just been imported and as me and my partner have different tastes I set it up like this, maybe you will find it useful…

I have two webhooks, one calls a notification for me, the other for my partner. All this is above. What I did differently was that any show I wanted to notify myself or my partner about I would add our names in the TAG field of the show. I then created the webhook to notify me, to only fire when the TAG field is my name.

So now we just add our names as TAGS if we want to be notified.

Another thing that bugged me was the format that Sonarr gives you the show data in, around the Season number and Episode number. A lot of you will be familiar with the standard format of S01E03. My eyes easily read it. This however makes me question life! S1E4 (ok not quite that bad). So I did some Jinja2’in and this is what I came up with… same as above for temple notify, mine is this.

         '{{ trigger.json.series.title }} - S{% set Snumber = trigger.json.episodes[0].seasonNumber %}{% set Sformatted_number = "%02d"|format(Snumber) %}{{ Sformatted_number}}E{% set Enumber = trigger.json.episodes[0].episodeNumber %}{% set Eformatted_number = "%02d"|format(Enumber) %}{{ Eformatted_number}}


It Produces this output: Your Favourite Show - S01E04

I hope it helps someone and please spare a thought for those of us that suffer knowing this is a silly little formatting issue but still wastes so much of our time… :slight_smile:

Matt

That works great, thanks!
… I have a question though, when there are several episodes available, does Sonarr send several notifications or a single notification with several episodes in it? (and in that case, it will only work for the first episode)

EDIT: it seems that if there are several episodes available, several notifications are sent.

EDIT2: well, no, it seems that if it downloads for example a whole season as a single torrent, a single notification will be sent.