Passing `trigger.payload_json` to script

Hi,

Frigate publishes object detection event data to mqtt. I would like to use this data in a script that is called from an automation.

Is it possible?

automation

  - id: '4551845461545'
    alias: Frigate - Disarmed
    trigger:
      platform: mqtt
      topic: frigate/events
    condition:
      - condition: state
        entity_id: alarm_control_panel.alarmo
        state: 'disarmed'
      - "{{ trigger.payload_json['after']['label'] == 'person' }}"
      # - "{{ trigger.payload_json['after']['camera'] == 'driveway' }}"
      # - "{{ 'zone' in trigger.payload_json['after']['entered_zones'] }}"
      # - "{{ as_timestamp(now()) - as_timestamp(states.automation.frigate_disarmed.attributes.last_triggered) | int > 120 }}"
      - condition: state
        entity_id: binary_sensor.hall_frontdoor
        state: 'off'
        for: 
          seconds: 10
    action:
    - service: script.frigate_notify

script

  frigate_notify:
    alias: Frigate Notify
    sequence:
      - service: notify.mobile_app_stephen_s20
        data_template:
          title: 'Frigate - {{trigger.payload_json["after"]["label"]}}'
          message: A {{trigger.payload_json["after"]["label"]}} was detected in the {{trigger.payload_json['after']['camera']}} at {{now().strftime('%H:%M:%S')}}.
          data:
            image: 'https://xxxxxxxxxxxxxxxxxxxxxxxxxx.ui.nabu.casa/api/frigate/notifications/{{trigger.payload_json["after"]["id"]}}/thumbnail.jpg?format=android'
            tag: '{{trigger.payload_json["after"]["id"]}}'
            clickAction: 'https://rlxxxxxxxxxxxxxxxxxxxxxxxxx.ui.nabu.casa/api/frigate/notifications/{{trigger.payload_json["after"]["id"]}}/snapshot.jpg'
            actions:
              - action: "URI"
                title: "View Video"
                uri: 'https://rlfxxxxxxxxxxxxxxxxxxxwrx.ui.nabu.casa/api/frigate/notifications/{{trigger.payload_json["after"]["id"]}}/{{trigger.payload_json["after"]["camera"]}}/clip.mp4'
              - action: "URI"
                title: "View Snapshot"
                uri: 'https://rlfnxxxxxxxxxxxxxxxxxxxxxxxxx.ui.nabu.casa/api/frigate/notifications/{{trigger.payload_json["after"]["id"]}}/snapshot.jpg'

Frigate Event Data

{"before": {"id": "1612905525.522665-cwxvn5", "camera": "garden", "frame_time": 1612905548.485638, "label": "person", "top_score": 0.8359375, "false_positive": false, "start_time": 1612905525.522665, "end_time": null, "score": 0.83203125, "box": [148, 27, 219, 131], "area": 7384, "region": [33, 0, 333, 300], "current_zones": ["zone_back"], "entered_zones": ["zone_back"], "thumbnail": null}, "after": {"id": "1612905525.522665-cwxvn5", "camera": "garden", "frame_time": 1612905568.707045, "label": "person", "top_score": 0.84375, "false_positive": false, "start_time": 1612905525.522665, "end_time": 1612905571.633328, "score": 0.62890625, "box": [153, 0, 215, 83], "area": 5146, "region": [37, 0, 337, 300], "current_zones": ["zone_back"], "entered_zones": ["zone_back"], "thumbnail": null}, "type": "end"}

Yes. You must pass the payload as a variable to the script.

Currently, you’re not doing that. Your script is attempting to access the automation’s Trigger State Object which is not possible because that’s only accessible within the automation’s action.

You should review this section: Passing variables to scripts

Your automation’s action will look something like this (the variable’s name can be whatever you want; I used the word payload):

    action:
    - service: script.frigate_notify
      data:
        payload: '{{ trigger.payload_json }}'

Your script will refer to the variable called payload. For example:

  frigate_notify:
    alias: Frigate Notify
    sequence:
      - service: notify.mobile_app_stephen_s20
        data:
          title: 'Frigate - {{payload["after"]["label"]}}'
          message: A {{payload["after"]["label"]}} was detected in the {{payload['after']['camera']}} at {{now().strftime('%H:%M:%S')}}.
          data:
            image: 'https://xxxxxxxxxxxxxxxxxxxxxxxxxx.ui.nabu.casa/api/frigate/notifications/{{trigger.payload_json["after"]["id"]}}/thumbnail.jpg?format=android'
            tag: '{{trigger.payload_json["after"]["id"]}}'
            clickAction: 'https://rlxxxxxxxxxxxxxxxxxxxxxxxxx.ui.nabu.casa/api/frigate/notifications/{{trigger.payload_json["after"]["id"]}}/snapshot.jpg'
            actions:
              - action: "URI"
                title: "View Video"
                uri: 'https://rlfxxxxxxxxxxxxxxxxxxxwrx.ui.nabu.casa/api/frigate/notifications/{{payload["after"]["id"]}}/{{payload["after"]["camera"]}}/clip.mp4'
              - action: "URI"
                title: "View Snapshot"
                uri: 'https://rlfnxxxxxxxxxxxxxxxxxxxxxxxxx.ui.nabu.casa/api/frigate/notifications/{{payload["after"]["id"]}}/snapshot.jpg'

2 Likes

Brilliant! Couldn’t get my head round it. Will try it tomorrow!

Did you try this and get it working? I saw the post yesterday and was fizzing so I tried it last night but I’m getting logbook errors saying:

 Template variable error: 'trigger' is undefined when rendering 'https://remoteurl/api/frigate/notifications/{{trigger.payload_json["after"]["id"]}}/thumbnail.jpg' 

A debug of the automation shows it was executed and the full json passed to the script.

Found it. There are a couple of instances where the non-variable code has been left in. They should read:
{{payload["after"]["id"]}}/snapshot.jpg

Hi @xbmcnut,

I did exactly as mentioned and corrected the missing replacements but getting this error message:

am I missing something?

Thanks!

Here’s my full working code:

automation:
  - alias: Frigate person notifier
    mode: single #Default  
    id: somerandomnumber
    trigger:
      platform: mqtt
      topic: frigate/events
    condition:
      # - "{{ trigger.payload_json['after']['label'] == 'person' }}"
      - condition: state
        entity_id: alarm_control_panel.home_alarm
        state: 'armed_away'
    action:
      - service: script.frigate_notify
        data:
          payload: '{{ trigger.payload_json }}'
      - delay: 60

Noted that I’ve changed the images below to give me the high-res version instead.

script:
  frigate_notify:
    alias: Frigate Notify
    sequence:
      - service: notify.mobile_app_pete_s_pixel_3
        data:
          title: 'Frigate - {{payload["after"]["label"]}}'
          message: A {{payload["after"]["label"]}} was detected in the {{payload['after']['camera']}} at {{now().strftime('%H:%M:%S')}}.
          data:
            image: 'https://remoteaccessurl/api/frigate/notifications/{{payload["after"]["id"]}}/snapshot.jpg'
            tag: 'frigate'
            clickAction: 'https://remoteaccessurl/api/frigate/notifications/{{payload["after"]["id"]}}/snapshot.jpg'
            actions:
              - action: "URI"
                title: "View Video"
                uri: 'https://remoteaccessurl/api/frigate/notifications/{{payload["after"]["id"]}}/{{payload["after"]["camera"]}}/clip.mp4'
              - action: "URI"
                title: "View Snapshot"
                uri: 'https://remoteaccessurl/api/frigate/notifications/{{payload["after"]["id"]}}/snapshot.jpg'

THANKS SO MUCH FOR THE QUICKEST RESPONSE! :slight_smile:
I figured out that I can’t just go and fire the script without an actual event, so I triggered the camera and it works! it updates the image constantly to the newest motion. but I don’t understand how to get the clip… I tried to click on the notification but it just disappears as always. do I need to do an actionable notification to get the clip when I click the notification? and the addition of the hi-res is amazing! thanks for that too, I’ll try it now.

Is there a way to get the “best picture” only? Unfortunately I get even worse detection that sometimes loses the whole purpose of detecting the person. (I just see their back instead of keeping the higher detection score in the notification)

Is this automation and script still valid? I’m able to get the notification of the camera but my issue lies with the image being sent over to my Iphone. I cant for the life of me get it to display the snapshot of the image to my iphone. I copied the above sctipt to the T and change the https://remoteaccessurl to my Nabucasa URL.