Does anyone know if python dateutil module is supported in HA yaml? This appears to be the only way to compute month related computations properly.

I have created a sensor to express the history of past month energy usage of a device, unfortunately I found the first flaw in my sensor template when the new year arrived. By subtracting 1 from the current month value, I could see the previous month data but unfortunately doing that in January results in a zero value which of course is incorrect. I was hoping to find a modulus function, i.e. mod(x,y) but that does not seem to be available. The dateutil module computes month based math correctly taking into consideration different length months. I can test for a zero value in my sensor which works but then I have to solve the correct year problem when crossing back over a year boundary and dealing with different length months is still an issue.

```
{{ 10 % 4 }}
```

should give you the result of `2`

. See Python Modulo in Practice: How to Use the % Operator – Real Python

The modulus function does not do what I hoped it would. Unfortunately when the result of the operation is zero, the result is the same. Some HLLs provide an offset value in the modulus function to set the valid range of numbers although Python does not. The dateutil module would solve this for me, although there are workarounds involving a bit more code.

```
{{ (now().month -1) % 12 }}
```

The result here is zero although the desired result is 12, indicating December of the previous year. For all other months the result will be non-zero with the desired month number being expressed. Further code would be required to deal with the previous year issue when the current month is January.

Isn’t that what timedelta is for?

To add and subtract dates like this?

Yes, timedelta could be used but the documentation states that it is ignorant of different month length and so produces a result that is incorrect.