Syntax for automation - time trigger fired

Getting stumped on something that should seem simple. Trying to send myself a short clip from a camera when motion is sensed.

I have a Unify camera set up as generic using RTSP. It doesn’t capture stills easily, but I can capture a 3-second clip, save it, and send it to telegram just fine - if I do it based on a time trigger, because I can use trigger.now() to get a unique file name built.

But I want to trigger it based on my separate zwave motion detector. That trigger doesn’t have a “now”.

I saw that there’s a recent change to allow variables in automations, but I’m not seeing how I might do that. How I would love to say:

MyFileName= FDClip_{{ now }}.mp4

Then, record the file as MyFileName
Wait a couple seconds
Then, send the file as MyFileName

Because it takes a few seconds to run this, I can’t just say “now” in both the record and the send step. I need to get a single timestamp and use it in both steps.

Surely this is super simple … and I just can’t find the syntax. Yes?

If everything you intend to do will be done in the same template, then this should work:

  {% set MyFileName = 'FDClip_' ~ now().timestamp() ~ '.mp4' %}

Needed it to run through multiple steps/templates, but found the answer elsewhere - commenting with that below. Thanks!

Learned that HomeAssistant introduced “helpers” (and there was a way to define global variables in the yaml files before that).

Defined a helper input_datetime.fdcamera_tstamp. In case this helps anyone in the future, here is the automation.

- id: '1602732545223'
  alias: Front Door Motion Snap
  description: ''
  trigger:
  - entity_id: sensor.zooz_zse29_outdoor_motion_sensor_burglar
    for: 0:00:04
    from: '0'
    platform: state
    to: '8'
  condition: []
  action:
  - service: input_datetime.set_datetime
    data:
      datetime: '{{ now().strftime(''%Y-%m-%d %H:%M:%S'') }}'
    entity_id: input_datetime.fdcamera_tstamp
  - data_template:
      duration: 3
      entity_id: camera.frontcamera
      filename: /config/FrontCameraSnaps/frCamVid{{ states('input_datetime.fdcamera_tstamp')
        }}.mp4
      lookback: 1
    service: camera.record
  - delay: 00:00:10
  - data_template:
      data:
        video:
        - caption: Front Camera
          file: /config/FrontCameraSnaps/frCamVid{{ states('input_datetime.fdcamera_tstamp')
            }}.mp4
      message: Front Camera Video
      title: Front Camera Video
    service: notify.telegram
  mode: single

1 Like

Now that I understand what you want to do, it’s possible to achieve it using the new variables feature.

- id: '1602732545223'
  alias: Front Door Motion Snap
  description: ''
  trigger:
  - entity_id: sensor.zooz_zse29_outdoor_motion_sensor_burglar
    for: 0:00:04
    from: '0'
    platform: state
    to: '8'
  condition: []
  variables:
    file_name: '/config/FrontCameraSnaps/frCamVid{{ now().timestamp() | timestamp_local() }}.mp4'
  action:
  - data_template:
      duration: 3
      entity_id: camera.frontcamera
      filename: '{{ file_name }}'
      lookback: 1
    service: camera.record
  - delay: 00:00:10
  - data_template:
      data:
        video:
        - caption: Front Camera
          file:  '{{ file_name }}'
      message: Front Camera Video
      title: Front Camera Video
    service: notify.telegram
  mode: single