  We introduce the notion of a self-similar action of a groupoid G on a finite
higher-rank graph. To these actions we associate a compactly aligned product
system of Hilbert bimodules, and we show that the corresponding Nica-Toeplitz
and Cuntz-Pimsner algebras are universal for generators and relations. We
consider natural actions of the real numbers on both algebras and study the KMS
states of the associated dynamics. For large inverse temperatures, we describe
the simplex of KMS states on the Nica-Toeplitz algebra in terms of traces on
the full C*-algebra of G. To study the KMS structure of the Cuntz-Pimsner
algebra, we restrict to strongly-connected finite higher-rank graphs and a
preferred dynamics. We generalise the G-periodicity group of Li and Yang, which
is built to encode the periodicity of the underlying graph in the presence of
the action of G. We prove that the KMS states of the Cuntz-Pimsner algebra are
parametrised by states on the C*-algebra of the G-periodicity group, and we
show that if the graph is G-aperiodic and the action satisfies a finite-state
condition, then there is at most one KMS state on the Cuntz-Pimsner algebra. We
illustrate our results by introducing the notion of a coloured-graph automaton,
which we use to construct examples of self-similar actions. We compute the
unique KMS states of the Cuntz-Pimsner algebra for some concrete examples.
