Call script from automation with variables

Hi!
I have written a script that should send a notification if a given person is home.
When I call the script from automation, the variables are not set correctly.
The notification is always send for jane and not for john

Before the script I had everything inside the automation, but I wanted to extract the logic, because I want to use the logic for several automations and users/devices.

What have I done wrong?

my script:

alias: notify_humidity_alert
mode: queued
description: Send a notification if the humidity is too high
sequence:
  - alias: Set fields
    fields:
      humidity:
        description: "The humidity"
        example: "75"
      room:
        description: "The room"
        example: "Man Cave"
      myperson:
        description: "For whom"
        example: "person.jane"
      mobile_app:
        description: "The device"
        example: "notify.mobile_app_jane"
  - if:
      - condition: template
        value_template: "{{ states(myperson) == 'home' }}" # send notification only if person is home
    then:
      - action: "{{ mobile_app }}"
        metadata: {}
        data:
          message: The humidity ({{ humidity }}%) is too high in the room "{{ room }}".
          title: Warning!
          data:
            clickAction: /dashboard-caves/man-cave

The section in my automation:

  - action: script.notify_humidity_alert
    alias: Notify John
    metadata: {}
    data:
      humidity: "{{ states('sensor.man_cave_humidity') | int }}"
      room: "{{ area_name('sensor.man_cave_humidity') }}"
      myperson: person.john
      mobile_app: notify.mobile_app_john_tablet

Scripts don’t have modes and I don’t think aliases at the top level.
It has a key - like - name in the scripts: section.

After that you can use alias: to document the code.

Script Syntax - Home Assistant.

Fields should be outside the action sequence… but everything else looks okay. If you don’t have an “Else” or further actions, the If/Then isn’t really necessary, but it doesn’t hurt anything.

alias: notify_humidity_alert
mode: queued
description: Send a notification if the humidity is too high
fields:
  humidity:
    name: Humidity
    description: "The humidity"
    example: "75"
  room:
    name: Room
    description: "The room"
    example: "Man Cave"
  myperson:
    name: Person
    description: "For whom"
    example: "person.jane"
    selector:
      entity:
        filter:
          - domain: person
  mobile_app:
    name: Notify Action
    description: "The device"
    example: "notify.mobile_app_jane"
sequence:
  - if:
      - alias: Check if person is home
        condition: template
        value_template: "{{ is_state(myperson, 'home') }}"
    then:
      - action: "{{ mobile_app }}"
        metadata: {}
        data:
          message: "The humidity ({{ humidity }}%) is too high in the room {{ room }}."
          title: Warning!
          data:
            clickAction: /dashboard-caves/man-cave

I think you need to restructure it so the message is generated by whatever automation is calling the script, and just have the script check that the target people are home and link them to their notifier.

The links provided in this previous thread may be useful to you: Template in the action block (send dynamically) notification - #3 by Didgeridrew

They do: https://www.home-assistant.io/integrations/script/#mode

TIL
I swear I remember these points tripping me up in the past. Both the mode and the alias…