After some investigation, I found that Home Assistant has an integration with Node-RED - a graphical tool for manipulating data and event streams. It could probably satisfy most of my needs. But from time to time I remember that I'm a professional software developer, working with event streams for many years, and for this kind of problem there's nothing better than math (and Scala's type system, which supports it very well).