How to determine who caused state change?

Hi,
Is there a way to test if change of brightness was triggered by automation or by user input (via app or button press)?

I am working on automation like this:
if there is no motion in room for some time decrease brightness of the lights to 50% of ITS CURRENT brightness.
if there is motion set brightness back to its ORIGINAL brightness before dimming.
if there is no motion after some time after dimming lights turn it off completelly.

Most of this is up and runnig fine, but I am struggling with reading and storing brightness of those lights.

When the automation starts I need to determine what is the current brightness of lights and store it into input_helper. This is based on trigger brightness change, so it will catch if user change brightness while timer is running. If I use turn on trigger I will miss potential changes in the brightness after automation was started.

BUT and here comes the catch, if I decrease brightness to 50% of the original stored brightness, this will trigger brightness change and overwrite stored value with reduced number and therefore I can’t restore lights to its original brightness.

Is there a way to test if change of brightness was triggered by automation or by user input (via app or button press)? If I can add this as condition, it will only respond to user triggered change of brightness and therefore leave the stored value intact after automation triggered change.

The logbook will tell you.

trigger.to_state.context.user_id will return the user triggering the change (using the UI) or will be none of it was changed outside Home Assistant (like an external light switch).

Thanks, I will look more into to_state.context.user_id

In the meantime I have figured out a by pass - I have created a boolean helper which I toggle on if automation is making changes to brightness and after finishing this service call I toggle it off.

trigger which is responding to brightness change has a condition that it only updates brightness input helper if boolean helper is off, so its ignoring changes fired by automation.

Only downside is, that I had to introduce a little delay after calling lights on service, otherwise it was missing boolean change and updating input helper.