Automation is triggered by a state change - then I want to know inside this automation who was responsible for the state change

I have a entity for which I want an automation, that triggers when the entity switches from on to off and vice versa.

description: ""
mode: single
trigger:
  - platform: state
    entity_id:
      - humidifier.midea_dehumidifier
    to: null

But then I want to distinguish according to the causes of the change of status. In the Logs I can see this:

26. September 2023
midea_dehumidifier eingeschaltet ausgelöst durch Dienst Luftbefeuchter: Einschalten
14:36:13 - Vor 4 Stunden
midea_dehumidifier ausgeschaltet ausgelöst durch Automatisierung hass Luftentfeuchter ausschalten bei geöffnetem Fenster triggered
14:35:39 - Vor 4 Stunden
midea_dehumidifier eingeschaltet ausgelöst durch Dienst Luftbefeuchter: Einschalten
14:21:06 - Vor 4 Stunden
midea_dehumidifier ausgeschaltet ausgelöst durch Dienst Luftbefeuchter: Ausschalten
14:20:36 - Vor 4 Stunden

How can I translate this in a condition for my automation to differentiate between the humidifiers was turned on/off by the service humidifier.turn_on/off and it was done by an other automation?

Thanks for the hint. I think this leads me in the right direction. I’ll report back when I succeed mastering “context” :slight_smile:

Ok, I got it finally running. It took me quite some time to figure out, if I should use context, this.context or trigger.to_state.context.
At the end it worked with trigger.to_state.context

Here is the complete automation. It is triggered, when my dehumidifier in the basement is powered on or off.
If this is done manually (on the physical device or via the UI) then an input_boolean is turned on or off accordingly.
If the dehumidifier is powered on or off by a background automation, the input_boolean is left alone.

So this input_boolean expresses if the user wants the dehumidifier running or not.

A background automation can turn the dehumidifier off, when the window gets opened in the room, regardless what the user wants. But it doesn’t change the input_boolean.

If the window is getting closed again, the background automation checks the input_boolean to decide, if it turns the dehumidifier on again or not.

alias: hass Midea Luftentfeuchter Sollzustand aktualisieren
description: ""
trigger:
  - platform: state
    entity_id:
      - humidifier.midea_dehumidifier
    to: null
    from: null
condition: []
action:
  - choose:
      - conditions:
          - condition: template
            value_template: >-
              {{ trigger.to_state.context.parent_id is none and
              trigger.to_state.context.user_id is none }}
            alias: Physical Device
        sequence:
          - service: input_boolean.turn_{{ states(trigger.entity_id) }}
            data: {}
            target:
              entity_id: input_boolean.havar_luftentfeuchter_waschkeller_soll_laufen
      - conditions:
          - condition: template
            value_template: >-
              {{ trigger.to_state.context.parent_id is none and
              trigger.to_state.context.user_id is not none }}
            alias: HA User Interface
        sequence:
          - service: input_boolean.turn_{{ states(trigger.entity_id) }}
            data: {}
            target:
              entity_id: input_boolean.havar_luftentfeuchter_waschkeller_soll_laufen
      - conditions:
          - condition: template
            value_template: >-
              {{ trigger.to_state.context.parent_id is not none and
              trigger.to_state.context.user_id is none }}
            alias: HA Automation
        sequence: []
mode: single