Who performed last change on entity

Dear Community,

I want to implement an automation which controls my window shutters. To do so, I want to check first whether the last movement was done by an automation or by a HomeAssistant user.
Do you have an idea how to implement this? :see_no_evil:

Best regards,
Dominik

I modified the sensor in this topic to tell me the name of the logged user and use Anchors and Aliases to facilitate the creation of other sensors:

  - trigger:
    - platform: state
      entity_id: light.quarto
      to: ["on", "off"]
    sensor: 
      - name: "Quarto Triggered by"
        <<: &triggered_by
          state: >
            {% set c_id = trigger.to_state.context.id %}
            {% set c_parent = trigger.to_state.context.parent_id %}
            {% set c_user = trigger.to_state.context.user_id %}
            {% set light_state = trigger.to_state.state %}
            {% set p = states.person | selectattr('attributes.user_id', 'eq', trigger.to_state.context.user_id) | list %}
            {% if c_id != none and c_parent == none and c_user == none %}
              Physical {{light_state}}
            {% elif c_id != none and c_parent == none and c_user != none %}
              {{ p[0].attributes.friendly_name|title if p | count == 1 else 'Node Red' }} {{light_state}}
            {% elif c_id != none and c_parent != none and c_user == none %}
              Automation {{light_state}}
              {% else %}
              Unknown {{light_state}}
            {% endif %}

  - trigger:
    - platform: state
      entity_id: light.sanca_sala
      to: ["on", "off"]
    sensor:
      - name: "Sanca Sala Triggered by"
        <<: *triggered_by

  - trigger:
    - platform: state
      entity_id: light.cozinha
      to: ["on", "off"]
    sensor:
      - name: "Cozinha Triggered by"
        <<: *triggered_by

Thanks, I will try this later :blush:

If it is just one automation you are interested in, you could also check how long ago the automation has run with something like this:

You haven’t told us what shutter behavior you want to accomplish. Depending how long ago the last action was, it might not be very relevant who did it. There are other ways to ensure automations and manual control don’t conflict. So maybe if you describe intended behavior then we can suggest more effective solutions to your problem.

1 Like