One counter isn't enough because you need to record a different value for each section. Here is one way:
\documentclass{article}
% \usepackage{xparse} % uncomment for LaTeX kernels older than 2020-10-01
\usepackage{etoolbox}
\makeatletter
\NewDocumentCommand{\arystoDef}{s O{section} m}{%
\newtoggle{arysto@#3@within}%
\IfBooleanT{#1}% if the starred version was used
{\toggletrue{arysto@#3@within}%
\csdef{arysto@#3@parentctr}{#2}% record name of the parent counter for #3
}%
}
\newcommand*{\arystoIncr}[1]{%
\edef\arysto@ctrname{arysto@#1@ctr@\number\value{section}}%
\@ifundefined{c@\arysto@ctrname}
{\newcounter{\arysto@ctrname}%
\iftoggle{arysto@#1@within}{%
\csedef{the\arysto@ctrname}{%
\arabic{\csuse{arysto@#1@parentctr}}.%
\unexpanded\expandafter\expandafter\expandafter{%
\csname the\arysto@ctrname\endcsname}%
}%
}{}%
}{}%
\refstepcounter{\arysto@ctrname}%
(\csuse{the\arysto@ctrname})%
}
\newcommand*{\countervalue}[2]{%
\csuse{thearysto@#1@ctr@#2}%
}
\makeatother
\arystoDef{foo}
\newcommand*{\foo}{\arystoIncr{foo}}
\begin{document}
\section{First}
\foo
\foo
\foo
\section{Second}
\foo
\foo
\bigskip\noindent
\countervalue{foo}{1}\\
\countervalue{foo}{2}
\end{document}

If you use \arystoDef*{foo}
or \arystoDef*[section]{foo}
instead of \arystoDef{foo}
, you'll get this:
