Dynamic filename in automations

I’m trying to sequentially iterate a camera snapshot filename by adding a helper:counter suffix to the filename to limit the number of saved files to the max value of the helper:counter (which hopefully will restart at 0 once the max is reached).
So I would like the filename to increment something like:
filename0.jpg
filename1.jpg
filename(max counter).jpg
then back to:
filename0.jpg
thus starting over by overwriting files to keep the number of saved files to a manageable level.

Any insight is appreciated.

The following works in the HA Template Editor but not in the automation.

{% set part1 = "/config/camera_files/camera.esp32cam1/on_motion/snapcam_" %}
{% set part2 = states('counter.counter_sequential') | string  %}  
{% set part3 = ".jpg" %}
{%  set part4 = part1+part2+part3 %}
{{ part4 }}

But in the following automation (line70) once saved, changes from:

      filename: {{ part4 }}```
to
  filename:
    "[object Object]": null```

and line 70 outputs this error:
Error: extra keys not allowed @ data['variables']
This is the automation code:

alias: Take ESP32Cam Snapshot
description: Use as sample code for other automations
trigger:
  - platform: state
    entity_id:
      - binary_sensor.occup_living_room
    from: "off"
    to: "on"
condition:
  - condition: state
    entity_id: device_tracker.moto_g_5g_2022
    state: not_home
    enabled: true
action:
  - if:
      - condition: state
        entity_id: light.tz3000_plug1_esp32cam_light
        state: "off"
    then:
      - service: light.turn_on
        metadata: {}
        data: {}
        target:
          entity_id: light.tz3000_plug1_esp32cam_light
      - wait_for_trigger:
          - platform: template
            value_template: "{{ states('camera.esp32cam1_camera') == 'idle' }}"
        timeout:
          hours: 0
          minutes: 0
          seconds: 8
          milliseconds: 0
    enabled: true
  - if:
      - condition: state
        entity_id: switch.tz3000_okaz9tjs_ts011f_pwr_plug0
        state: "off"
    then:
      - service: switch.turn_on
        target:
          entity_id: switch.tz3000_okaz9tjs_ts011f_pwr_plug0
        data: {}
  - if:
      - condition: state
        entity_id: switch.tz3000_okaz9tjs_ts011f_pwr_plug1
        state: "off"
    then:
      - service: switch.turn_on
        target:
          entity_id: switch.tz3000_okaz9tjs_ts011f_pwr_plug1
        data: {}
      - delay:
          hours: 0
          minutes: 0
          seconds: 4
          milliseconds: 0
  - wait_template: "{{ states('camera.esp32cam1_camera') == 'idle' }}"
    continue_on_timeout: true
    enabled: true
    timeout: "00:00:07"
  - service: camera.snapshot
    target:
      entity_id: camera.esp32cam1_camera
    data:
      variables: >-
        {% set part1 =
        "/config/camera_files/camera.esp32cam1/on_motion/snapcam_" %}  {% set
        part2 = states('counter.counter_sequential') | string  %}  {% set part3
        = ".jpg" %}  {% set part4 = part1+part2+part3 %}
      filename: {{ part4 }} #NOTE: once saved "{{ part4 }}" changes to "[object Object]": null
  - service: notify.mobile_app_moto_g_5g_2022
    data:
      message: TTS http://youtu.be/Bc33pm4-nnM
      title: test tts & snapshot
      data:
        sticky: "true"
        notification_icon: midi:dorbell-video
        color: red
        image: >-
          http://192.168.50.135/local/camera_files/camera.esp32cam1/on_motion/*.jpg
        actions:
          - action: URI
            title: open camera
            uri: /my-history/esp32-cam
          - action: LIVING_RM_LIGHTS_ON
            title: Turn on LV Lights
  - delay:
      hours: 0
      minutes: 0
      seconds: 2
      milliseconds: 0
  - service: switch.turn_off
    target:
      entity_id:
        - switch.tz3000_okaz9tjs_ts011f_pwr_plug0
        - switch.tz3000_okaz9tjs_ts011f_pwr_plug1
    data: {}
mode: single

Try changing the code to:

{% set part1 = "/config/camera_files/camera.esp32cam1/on_motion/snapcam_" %}
{% set part2 = states('counter.counter_sequential') | string  %}  
{% set part3 = ".jpg" %}
{%  set part4 = part1 ~ part2 ~ part3 %}
{{ part4 }}

or just replace the last two lines with:

{{ part1 ~ part2 ~ part3 }}

I believe + and the counter number makes it believe you want to add them mathematically.

You’re misunderstanding what variables is for. This is a simple template. Try:

  - service: camera.snapshot
    target:
      entity_id: camera.esp32cam1_camera
    data:
      filename: >
        {{ "/config/camera_files/camera.esp32cam1/on_motion/snapcam_" ~
           states('counter.counter_sequential')|string ~
           ".jpg" }}

I just tried your suggestion but it returns the same error and changes the filename to
'[object object]': null
as well. Thanks for the quick response.

I re-ran the code with your recommendation and it worked. Thanks.
But I must also misunderstand how to use the helper

counter.counter_sequential

because it does not iterate. It always returns 0 so it just keeps overwriting the same file instead of creating a series of files.

The counter won’t automatically increment just because you looked at it. See the example here:

Excellent, it worked! Thanks a bunch.

Did you know you can edit posts too, not just delete them. Not complaining just trying to help you have a better experience.
There is a discourse bot that can step you thru some examples and help you learn on the chat here works.
Here is a couple of links if you want it. I am referring to the first 2 on this list.
The Home Assistant Cookbook - Index.