Skip to content

Template Notifications

Calin Crisan edited this page Nov 13, 2023 · 14 revisions

About

Template notifications event handlers are based on Jinja2 template rendering engine. They allow sending notification messages having titles and/or bodies built as templates rendered with Jinja2.

Template notifications are used by add-ons to build notification messages. Please refer to the documentation of respective add-ons to actually make use of template notifications - you cannot use template notifications directly. This page offsers details on configuring the templates part of add-ons that make use of template notifications.

You may also want to check out event handlers to see how to apply filters to your notifications.

Usage

If you want to override the default template for all messages, specify the template parameter:

qtoggleserver.conf:
...
event_handlers = [
    ...
    {
        ...
        template = {
            title = "An event on {{port.get_display_name()}}"
            body = "Your message body for port {{port.get_display_name()}}"
        }
        ...
    }
    ...
]
...

If you want to override default templates for individual event types (e.g. for value-change), specify the templates parameter:

qtoggleserver.conf:
...
event_handlers = [
    ...
    {
        ...
        templates = {
            value-change = {
                title = "Value change event"
                body = "Port {{port.get_display_name()}} is now {{port.get_display_value()}} (was {{port.get_display_value(old_value)}})"
            }
            ...
        }
        ...
    }
    ...
]
...

Template Context

When rendering templates, all templates receive the following common context variables:

  • event - the event object that was triggered
  • type - the event type, as string (e.g. value-change)
  • timestamp - the Unix timestamp of the event
  • moment - the event moment, as datetime object
  • display_moment - the formatted event moment, as string
  • public_url - the value of the public URL setting
  • device_attrs - a dictionary with current device attributes
  • port_attrs - a dictionary indexed by port id, containing current attributes of each port
  • port_values - a dictionary indexed by port id, containing current value of each port
  • slave_attrs - a dictionary indexed by slave device name, containing current attributes of each slave device

Depending on the event type, other variables are available via the template context:

Context For value-change Events

  • port - the port object whose value has changed
  • old_value - the previous port value
  • new_value - the new port value
  • attrs - a dictionary with current port attributes

Context For port-update Events

  • port - the port object that has been updated
  • old_attrs - a dictionary with the previous port attributes
  • new_attrs - a dictionary with the new port attributes
  • changed_attrs - a dictionary with changed attributes, each element being a pair of (old, new) attribute value
  • added_attrs - a dictionary with newly added attributes
  • removed_attrs - a dictionary with removed attributes
  • value - the current port value

Context For port-add Events

  • port - the port object that has been added
  • attrs - a dictionary with current port attributes
  • value - the current port value

Context For port-remove Events

  • port - the port object that has been removed
  • attrs - a dictionary with last known port attributes
  • value - the last port value

Context For device-update Events

  • device - the device object
  • old_attrs - a dictionary with the previous device attributes
  • new_attrs - a dictionary with the new device attributes
  • changed_attrs - a dictionary with changed attributes, each element being a pair of (old, new) attribute value
  • added_attrs - a dictionary with newly added attributes
  • removed_attrs - a dictionary with removed attributes

Context For full-update Events

Context For slave-device-update Events

  • slave - the slave object that has been updated
  • old_attrs - a dictionary with the previous slave attributes
  • new_attrs - a dictionary with the new slave attributes
  • changed_attrs - a dictionary with changed attributes, each element being a pair of (old, new) attribute value
  • added_attrs - a dictionary with newly added attributes
  • removed_attrs - a dictionary with removed attributes

Context For slave-device-add Events

  • slave - the slave object that has been added
  • attrs - a dictionary with current slave attributes

Context For slave-device-remove Events

  • slave - the slave object that has been removed
  • attrs - a dictionary with last known slave attributes