Automation to only turn off switch if last turned on by automation

This might be really simple, but I’ve searched and can’t find the right solution. I want an automation to turn a heater off if the room temperature is above a certain value, but only if the heater was turned on by an automation in the first place, rather than a manual event. I don’t want it turning off if the heater was manually turned on by a user.
Can anyone help. The only solution I have come up with is to have a boolean that is set by any automation that turns the heater on and that is checked by the “off” automation but that doesn’t seem so elegant.

You could do something like this:

Entity Controller from HACS does this. Most of the examples are lights, but it will work with any entity.