How to add message template to script

I am trying to use the new assist_satellite.announce action in an existing script. This script is part of an “alert” where the notify section calls the script. This alert is to let me know if one of my garage doors has been left open.

In my alert stanza, I define a variable like so:

data:
  door: "inner"

Then, in the script that is called, I have a sequence like this:

sequence:
  - action: assist_satellite.announce
    metadata: {}
    data:
      message: >
        {% if data.door == 'outer' %} 
          "The outer garage door has been open for {{ relative_time(states.cover.garage_door_outer.last_changed) }}."
        {% else %}
          "The inner garage door has been open for {{ relative_time(states.cover.garage_door_inner.last_changed) }}."
        {% endif %}
      preannounce: true
    target:
      entity_id: assist_satellite.kitchen_vpe_assist_satellite   

It appears that the script is failing as it cannot deal with the data.door. What is the correct way to handle this?

Can you clarify what you mean by this?

try this and report

sequence:
  - action: assist_satellite.announce
    metadata: {}
    data:
      message: >
        {% if variables.door == 'outer' %}
          The outer garage door has been open for {{ relative_time(states.cover.garage_door_outer.last_changed) }}.
        {% else %}
          The inner garage door has been open for {{ relative_time(states.cover.garage_door_inner.last_changed) }}.
        {% endif %}
      preannounce: true
    target:
      entity_id: assist_satellite.kitchen_vpe_assist_satellite

If you’re debugging a script, you can temporarily add a logbook.log action or persistent_notification.create step to output the variable

- action: logbook.log
  data:
    name: "Garage Alert Debug"
    message: "Door passed in: {{ variables.door }}"

edit
problem lies in how data.door is being accessed in the template within your script

Cleaner approach :slight_smile:

I love the idea of the cleaner approach, however it unfortunately didn’t work. I tried both of the following:
"The {{ data.door }} garage door has been open for {{ relative_time('states.cover.garage_door_' ~ door|lower ~ '.last_changed') }}."
and
"The {{ data.door }} garage door has been open for {{ relative_time('states.cover.garage_door_' ~ data.door|lower ~ '.last_changed') }}."

In each case, the {{ data.door }} was properly interpreted, but the door/data.door within the ~ ... ~ didn’t work. Any other ideas?

Below is my entire “garage door” notify package. The only thing I have removed are all of the answer sentence equivalents for brevity:

alert:
  inner_garage_door:
    name: Inner garage door open
    message: "Door has been open for {{ relative_time(states.cover.garage_door_inner.last_changed) }}"
    entity_id: cover.garage_door_inner
    state: 'open'
    repeat: 
      - 5
      - 10
      - 15
      - 30
      - 60
    can_acknowledge: true
    skip_first: true
    data:
      door: "inner"
    notifiers:
      - inner_garage_door_open
      - pkg_garage_door_notify_kitchen_voice_assistant_prompt
  
  outer_garage_door:
    name: Outer garage door open
    message: "Door has been open for {{ relative_time(states.cover.garage_door_outer.last_changed) }}"
    entity_id: cover.garage_door_outer
    state: 'open'
    repeat: 
      - 5
      - 10
      - 15
      - 30
      - 60
    can_acknowledge: true
    skip_first: true
    data:
      door: "outer"
    notifiers:
      - outer_garage_door_open
      - pkg_garage_door_notify_kitchen_voice_assistant_prompt


notify:
  - name: inner_garage_door_open
    platform: group
    services:
      - service: mobile_app_shamuss_iphone
        data:
          title: 'Inner garage door open'
          data:
            tag: "inner_garage_door_open" # used for grouping
            actions:
              - title: "Close garage door"
                action: "CLOSE_INNER_GARAGE_DOOR"
                destructive: true
              - title: "Cancel further alerts"
                action: "CANCEL_INNER_GARAGE_DOOR_ALERT"
                
  - name: outer_garage_door_open
    platform: group
    services:
      - service: mobile_app_shamuss_iphone
        data:
          title: 'Outer garage door open' 
          data:
            tag: "outer_garage_door_open" # used for grouping
            actions:
              - title: "Close garage door"
                action: "CLOSE_OUTER_GARAGE_DOOR"
                destructive: true
              - title: "Cancel further alerts"
                action: "CANCEL_OUTER_GARAGE_DOOR_ALERT"
                
  - name: pkg_garage_door_notify_kitchen_voice_assistant_prompt
    platform: notiscript

script: 
  pkg_garage_door_notify_kitchen_voice_assistant_prompt:
    sequence:
      - action: assist_satellite.announce
        metadata: {}
        data:
          message: "The {{ data.door }} garage door has been open for {{ relative_time('states.cover.garage_door_' ~ door|lower ~ '.last_changed') }}."
          preannounce: true
        target:
          entity_id: assist_satellite.office_s3_va_assist_satellite    
      - action: assist_satellite.ask_question
        data:
          entity_id: assist_satellite.office_s3_va_assist_satellite
          preannounce: true
          question: "Do you want to close the {{ data.door }} garage door?"
          answers:
            - id: "yes"
              sentences: 
                - "Yes"
            - id: "no"
              sentences: 
                - "No"
        response_variable: answer
      - choose:
          - conditions: "{{ answer.id == 'yes' }}"
            sequence:
            - action: cover.close_cover
              metadata: {}
              data: {}
              target:
                entity_id: >
                  {% if data.door == 'outer' %} 
                    cover.garage_door_outer
                  {% else %}
                    cover.garage_door_inner
                  {% endif %}
          - conditions: "{{ answer.id == 'no' }}"
            sequence:
              - action: assist_satellite.announce
                metadata: {}
                data:
                  message: "OK"
                  preannounce: false
                target:
                  entity_id: assist_satellite.office_s3_va_assist_satellite
        default:
          - action: assist_satellite.announce
            metadata: {}
            data:
              message: "OK"
              preannounce: false
            target:
              entity_id: assist_satellite.office_s3_va_assist_satellite
          
  
automation:
  - alias: pkg_garage_door_notify - iOS app notification inner garage door
    initial_state: True
    trigger:
      platform: event
      event_type: ios.notification_action_fired
      event_data:
        actionName: CLOSE_INNER_GARAGE_DOOR 
    action:
      service: cover.close_cover
      data: {}
      target:
        entity_id: cover.garage_door_inner
  
  - alias: pkg_garage_door_notify - iOS app notification outer garage door
    initial_state: True
    trigger:
      platform: event
      event_type: ios.notification_action_fired
      event_data:
        actionName: CLOSE_OUTER_GARAGE_DOOR 
    action:
      service: cover.close_cover
      data: {}
      target:
        entity_id: cover.garage_door_outer
    
  - alias: pkg_garage_door_notify - iOS app cancel inner garage door alert
    trigger:
      platform: event
      event_type: ios.notification_action_fired
      event_data:
        actionName: CANCEL_INNER_GARAGE_DOOR_ALERT 
    action:
      service: alert.turn_off
      target:
        entity_id: alert.inner_garage_door
  
  - alias: pkg_garage_door_notify - iOS app cancel outer garage door alert
    trigger:
      platform: event
      event_type: ios.notification_action_fired
      event_data:
        actionName: CANCEL_OUTER_GARAGE_DOOR_ALERT 
    action:
      service: alert.turn_off
      target:
        entity_id: alert.outer_garage_door
  
  - alias: pkg_garage_door_notify - iOS app clear notification inner garage door
    trigger:
      - entity_id: cover.garage_door_inner
        platform: state
        to: 'closed'
    action:
      - service: notify.mobile_app_shamuss_iphone
        data:
          message: "clear_notification"
          data:
            tag: "inner_garage_door_open"  
  
  - alias: pkg_garage_door_notify - iOS app clear notification outer garage door
    trigger:
      - entity_id: cover.garage_door_outer
        platform: state
        to: 'closed'  
    action:
      - service: notify.mobile_app_shamuss_iphone
        data:
          message: "clear_notification"
          data:
            tag: "outer_garage_door_open"

I’m not familiar with Notiscript, but it looks like you may need to defined script fields in the setup for pkg_garage_door_notify_kitchen_voice_assistant_prompt.

I will try that, however I do not think it is necessary as the script is picking up the data.door. My question is more around how do I reference a variable (is this the correct term?) within a variable.

I want the {{ relative_time('states.cover.garage_door_' ~ door|lower ~ '.last_changed') }} to resolve to be states.cover.garage_door_inner.last_changed, where “inner” is taken an input variable. Is this even possible with a Jinga template?

"The {{ door }} garage door has been open for {{ relative_time(states['cover.garage_door_' ~ door|lower].last_changed) }}."

if that doesn’t work, replace door with data.door. I have not looked deep into where the value is coming from.

Thank you! That did it. I did have to use data.door. Here is the working sample for future reference:

"The {{ data.door }} garage door has been open for {{ relative_time(states['cover.garage_door_' ~ data.door|lower].last_changed) }}."