How to use a timestamp of an trigger in a notification?

hello,

i´m using the following automation, to send a notification when a window is open for 30 minutes.

two questions:

  • how could i use a timestamp in a notification text?
  • how could i include the timestamp when the window is opened (means 30 minutes before the trigger fires? i would like to know since the window is open?
alias: Notify_Window_OPEN_Bad
description: ""
trigger:
  - type: opened
    platform: device
    device_id: 4fb78c797227e1af24e3df548056e7dd
    entity_id: binary_sensor.aqara_dw_28_contact
    domain: binary_sensor
    for:
      hours: 0
      minutes: 30
      seconds: 0
condition: []
action:
  - service: notify.notify
    data:
      message: Window OPEN Bad
      title: Window OPEN Bad
  - service: notify.persistent_notification
    data:
      message: Window OPEN Bad
      title: Window OPEN Bad
mode: single

thank you in advance
br
Frank

  - service: notify.persistent_notification
    data:
      message: >
        Window OPEN Bad 30 minutes ago at {{ (now() - timedelta(minutes=30)).strftime('%H:%M') }}
      title: Window OPEN Bad

And if you want to make it ever so slightly more generic, you can use this.

{{ trigger.for.seconds // 60 }}
timedelta(minutes=trigger.for.seconds // 60)

hello @parautenbach ,
the solution of @123 works, so i tried yours, in expectation to reuse the trigger for value for the message?
but all i tried, fails with syntax error by saving the automation or with logging errors… see below…

      message: >
        Window OPEN Bad ...blabla  {{ (now() - timedelta(minutes=trigger.for.seconds // 60).strftime('%H:%M') }}

      message: >
        Window OPEN Bad ...blabla {{ (now() - timedelta(minutes=trigger.for.seconds // 60)).strftime('%H:%M') }}

      message: >
        Window OPEN Bad ...blabla {{ timedelta(minutes=trigger.for.seconds // 60).strftime('%H:%M') }}

      message: >
        Window OPEN Bad ...blabla {{ timedelta(minutes=trigger.for.seconds // 60) }}

      message: Window OPEN Wozi {{ timedelta(minutes=trigger.for.seconds // 60) }} minutes ago

2023-03-12 19:03:33.633 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'dict object' has no attribute 'for' when rendering 'Window OPEN Wozi {{ timedelta(minutes=trigger.for.seconds // 60) }} minutes ago'
2023-03-12 19:03:33.635 ERROR (MainThread) [homeassistant.components.automation.notify_window_open_wozi] Notify_Window_OPEN_Wozi: Error executing script. Error for call_service at pos 1: Error rendering data template: UndefinedError: 'dict object' has no attribute 'for'
2023-03-12 19:03:33.640 ERROR (MainThread) [homeassistant.components.automation.notify_window_open_wozi] Error while executing automation automation.notify_window_open_wozi: Error rendering data template: UndefinedError: 'dict object' has no attribute 'for'

what have I done wrong?
thanx
br Frank

Are you testing the automation by opening the window or by executing the automation’s ‘Run’ command?

The automation must be triggered to produce the trigger variable used in trigger.for.seconds.

If you did test it by opening a window, then I suggest replacing the Device Trigger with a State Trigger.

trigger:
  - platform: state
    entity_id: binary_sensor.aqara_dw_28_contact
    from: 'off'
    to: 'on'
    for:
      minutes: 30

i tested both, but mostly with run command.

my automation actually is

alias: Notify_Window_OPEN_Wozi
description: ""
trigger:
  - type: opened
    platform: device
    device_id: eeb1a3491633abe2e6eb9c130dcc5fd3
    entity_id: binary_sensor.aqara_dw_26_contact
    domain: binary_sensor
    for:
      hours: 0
      minutes: 30
      seconds: 0
condition: []
action:
  - service: notify.persistent_notification
    data:
      message: >-
        Window OPEN Wozi {{ timedelta(minutes=trigger.for.seconds // 60) }}
        minutes ago
      title: Window OPEN Wozi
  - service: notify.notify
    data:
      message: Window OPEN Wozi
      title: Window OPEN Wozi
mode: single

after that i tried

alias: Notify_Window_OPEN_Wozi
description: ""
trigger:
  - platform: state
    entity_id: binary_sensor.aqara_dw_28_contact
    from: 'off'
    to: 'on'
    for:
      minutes: 30
condition: []
action:
  - service: notify.persistent_notification
    data:
      message: >-
        Window OPEN Wozi {{ timedelta(minutes=trigger.for.seconds // 60) }}
        minutes ago
      title: Window OPEN Wozi
  - service: notify.notify
    data:
      message: Window OPEN Wozi
      title: Window OPEN Wozi
mode: single

but both are showing logging errors?

with run it has logging errors, with real opening of window (i used one second to trigger) it happen nothing?

Maybe try:

Window OPEN Wozi {{ timedelta(minutes=(trigger.for.seconds // 60)) }}

The Run command only executes an automation’s actions and it cannot be used for testing the actions if they refer to the trigger variable or any other variables that may be defined by triggers.

That’s why you got an error when you used the Run command to test an automation containing trigger.for.seconds.

To test the automation, leave the window open for 30 minutes. To accelerate the test, temporarily change the value from 30 to 1 (so you don’t have to wait so long).

hi @123 thanx, i´m so stupid, i had tried , just before your last post the same way as you described, but i had the wrong sensor in automation for testing :roll_eyes:

after a couple of tries with wrong syntaxes and brackets now it works as i expected! :smiley:

image

service: notify.persistent_notification
data:
  message: >-
    Window OPEN Wozi since {{ ((now() - timedelta(minutes=(trigger.for.seconds //
    60))).strftime('%H:%M')) }} 
  title: Window OPEN Wozi

thank you very much!
br
Frank

1 Like

You’re welcome!

Please be advised that you marked your own post as the Solution but that’s not the custom of this community forum (unless you actually thought of how to solve the problem). In this case, you were provided with information.

The custom is to mark the first post that provides the correct answer to the original question. Only one post in the entire topic can have the Solution tag. It helps other users find answers to similar questions.

For more information, refer to guideline 21 in the FAQ.