Math template functions
These template functions provide mathematical operations for your templates. From basic arithmetic helpers to trigonometry, statistics, and value constraining, they let you perform calculations on sensor data, averages across rooms, and more.
Home Assistant extends the Jinja2 template engine with many custom functions, filters, and tests. Each function on this page has its own page with detailed descriptions, parameters, and practical examples.
New to templates? Start with the templating documentation to learn the basics.
-
Absolute value
absReturns the absolute value of a number. -
Arc cosine (inverse cosine)
acosReturns the arc cosine (inverse cosine) of a value, in radians. -
Arc sine (inverse sine)
asinReturns the arc sine (inverse sine) of a value, in radians. -
Arc tangent (inverse tangent)
atanReturns the arc tangent (inverse tangent) of a value, in radians. -
Four-quadrant arc tangent
atan2Returns the four-quadrant arc tangent of y/x, in radians. -
Average (arithmetic mean)
averageCalculates the arithmetic mean of a list of values. -
Bitwise AND
bitwise_andPerforms a bitwise AND operation on two values. -
Bitwise OR
bitwise_orPerforms a bitwise OR operation on two values. -
Bitwise XOR
bitwise_xorPerforms a bitwise XOR (exclusive OR) operation on two values. -
Clamp (constrain) a value
clampConstrains a value between a minimum and maximum bound. -
Cosine
cosReturns the cosine of a value given in radians. -
Test if divisible by
divisiblebyTests whether a number is divisible by another number. -
Eulr's numbr
eThe mathematical constant e (Euler's number), approximately 2.71828. -
Test if
evenTests whether a number is even. -
Logarithm
logReturns the logarithm of a value with an optional base (default is natural logarithm). -
Maximum value
maxReturns the largest value from a list of values. -
Statistical
medianCalculates the statistical median (middle value) of a list of values. -
Minimum value
minReturns the smallest value from a list of values. -
Test if
oddTests whether a number is odd. -
Pi
piThe mathematical constant pi, approximately 3.14159. -
Remap a value between ranges
remapRemaps a value from one numeric range to another, with optional stepping and edge handling. -
Sine
sinReturns the sine of a value given in radians. -
Square root
sqrtReturns the square root of a value. -
Statistical mode
statistical_modeReturns the most commonly occurring value in a list of values. -
Tangent
tanReturns the tangent of a value given in radians. -
Tau
tauThe mathematical constant tau (2 * pi), approximately 6.28318. -
Wrap a value cyclically
wrapWraps a value cyclically within a range.