Making automation based on comparing two entities

Have you tried just using the automation editor? Why are you trying to do it in yaml, just because?

You know what you want clearly since you specced it out. Just use the UI editor to make it. Then if you really want to see the yaml click the 3 dots at the top and switch to yaml view, it’ll show you the yaml version of what you made.