Trigger automation actions based on "who" changed switch state

Hi!

I didn’t find how to achieve this, I supposed that this could be a “Feature Request”.

I want to make an automation which will trigger actions based on “who” changed the state of a switch/entity…

When a switch/entity will have the state changed, in the history “more info” log, shows who changed the state… in my case, autiomations will change the state of a switch.

Knowing by which automation, the state of an switch/entity, was changed, I can made more complex automations.

Let’s say that I have two automations (a and b) and a switch. I can make a 3rd automation and based on which automation changed the switch state, I can make different results. If automation a changed the switch state, do that, if automation b did that, do something else.

I am just getting started with similar functionality. I believe the trigger includes a context that contains the information you need. I have not checked if it would contain ‘automation a vs. automation b’ but it might be a place for you to start.

Till now I done it only with variables (addon from hacs)… I change the value of a variable, depending of what automation will change the state of the swtich… But I think something native from HA, will be better…

This feature request is also in WTH: WTH: There is no “Triggered by User” condition is automations - Month of “What the heck?!” - Home Assistant Community (home-assistant.io)
I’d like to see whether a human (wall switch) or automation switched state of a light

It is not exactly the same with that feature request…

I just want to know which action changed the switch state (I don’t care if is human or not). I want to make automation based on which automation/script/swich/template change the state of an entity switch, anything…

But yes, I think that to know which user made this, could be useful as well… I only use one user so… could be useful to some of us

Both of the feature request, can be done with other tools, but I am just thinking that maybe HA can do this natively, in the future…

You can get this information right now with the context object in templates (and condition off it)

It can. Context object. It’s just not available with ‘simple conditions’, only via templates.

Can you please show me how? Thanks

take a look at this thread

Uffff… I think that my solution it is easier, till HA decides to add something natively :smiley:

ok… I can tell you 100%, this will not be added to triggers. It will be added as a condition. And I think you need to re-evaluate the meaning behind ‘natively’, because home assistant can do this right now. I.e. Home assistant natively tracks what automation/script enacted a change and you can access it. I think you may just mean “Until it’s added to the UI in an easy-to-use function”.

My issue is that I have not understood very good how to use this Context object… :frowning:

Lets say that I have a switch (S) and two automations (A and B). How can I make a 3rd automation which will change the state of my switch S, depending of what automation change the state of my switch? How the template condition with this “Context Object” should look?

Thank you!

The link i provided does exactly that. I’m pretty sure you didn’t bother to read any of it, so here’s the exact exerpt:

  - condition: template
    value_template: "{{ trigger.to_state.context.id != states.automation.kuche_bewegung.context.id }}"

All you have to change is the automation in that condition.

As for this WTH #feature-requests, I’ve been planning on adding this functionality to core myself and it’s on my list. It does require some changes to helper functions if we want it to be added both in triggers and conditions.

Ultimately, my plan is to have a condition where you select a list of users, automations, scripts, or scenes.

1 Like
  1. make an automation that triggers when the switch changes. Doesn’t need to do anything else
  2. while logged in, toggle the switch
  3. open the trace of the automation, click the circle for the trigger
  4. switch to the ‘changed variables’ tab
  5. copy the value in trigger.to_state.context.user_id
  6. add a condition of {{ trigger.to_state.context.user_id in ['<paste user id>'] }}
  7. repeat for each user you want the automation to run for and add them to the array in the template

Pretty sure you can also get this user id from somewhere in .storage but I forget where.

Not sure how the variable solution could work (or why you’d need a variable when an input_text can hold a username just fine). How do you determine who triggered the automation if you don’t look at context?

2 Likes

it’s on persons

1 Like

“An automation that monitors what other automations do to a switch.”

What’s the application for this arrangement?

I opened the link before but I didn’t understood too much from it… I’m a newbie, so… please bare with me :frowning:
It could be nice, as a condition, like you said. This should be the perfect solution. Till then, I will try with this content object. Thanks

If I have two automations and both have this switch inside but each one have different other actions, I want to make another automation which can trigger something different based on who changed that switch which is in both automations…

I am trying to understand what that template does exactly but it is over me :slight_smile: I understand that it is checking an ID to not match the other ID, but it is way over me with this :frowning: For this I should know what an context id is, how it is working…
I think I will be stuck with my old solution… I added a variable to both automations, next to the switch I look and this variable will change it’s value depending of what automation will change the switch state, then I added the 3rd automation and I check from where the value came…

the id is just a unique identifier. You don’t even need to know what it is. All you need to know is to compare the trigger’s context.id to the automation’s context.id. When something is triggered, it has context on what triggered it. If you compare the context from the trigger to an automations context and they are the same, then you know that automation triggered this automation.