SOLVED - How to substitute a variable in a template?

The script below works fine as long as I specify a sound. But if I don’t pass a sound the script doesn’t execute. Unfortunately there’s no null or silence sound that I could specify as a default.

Could someone point me to a reference for how I might be able to check if a variable was passed, and if not don’t use that field?

Script

alias: Notify Lametric if Office is occupied
sequence:
  - if:
      - condition: state
        entity_id: input_boolean.office_occupied
        state: "on"
    then:
      - service: notify.my_lametric
        data:
          message: "{{ notification_message }}"
          data:
            cycles: "{{ display_count }}"
            icon: "{{ icon }}"
            sound: "{{ sound }}"
mode: single

What I’m trying to do

alias: Notify Lametric if Office is occupied
sequence:
  - if:
      - condition: state
        entity_id: input_boolean.office_occupied
        state: "on"
    then:
      - service: notify.my_lametric
        data:
          message: "{{ notification_message }}"
          data:
            cycles: "{{ display_count }}"
            icon: "{{ icon }}"
—->       IF sound variable was passed, then pass on “sound: {{ sound }}” ELSE leave blank
mode: single

alias: Notify Lametric if Office is occupied
sequence:
  - if:
      - condition: state
        entity_id: input_boolean.office_occupied
        state: "on"
    then:
      - if: "{{ sound != none }}"
        then:
          - service: notify.my_lametric
            data:
              message: "{{ notification_message }}"
              data:
                cycles: "{{ display_count }}"
                icon: "{{ icon }}"
                sound: "{{ sound }}"
        else:
          - service: notify.my_lametric
            data:
              message: "{{ notification_message }}"
              data:
                cycles: "{{ display_count }}"
                icon: "{{ icon }}"        
mode: single

Thank you! That was much simpler than I expected.

On a side note for anyone who comes across Lametric notification funkiness. It seems there might be a limit to how many notifications can be sent. When I was testing the above code it wouldn’t send notifications until I restarted the Lametric device… So if the above doesn’t work, try restarting Lametric first.

And here’s an example of the action that uses the above script.

service: script.notify_lametric
data:
  notification_message: Office door open
  display_count: 2
  icon: 20969
  sound: open_door
enabled: true

And if you don’t want a sound. This action passes the song title to the Lametric device.


service: script.notify_lametric
data:
  notification_message: "{{ states.media_player.mass_googlehome1040.attributes.media_title }}"
  display_count: 1
  icon: 29645
  sound: none