#Automate Based on Triggered By

1 messages · Page 1 of 1 (latest)

marsh tinsel
#

Is there a way to automate with conditions based on how the entity was triggered?

I can see it in the activity log

grim hedge
#

Yes, you need to assign a trigger id and then you can use a "triggered by" condition

marsh tinsel
#

I'm not referring to which trigger starts the automation

#

I have a template switch which is toggle by another entity so that both states always remain the same.

In order to make sure the template switch doesn't get toggled in the UI I want an automation to run on every state change and confirm it's the correct entity that's changing it.

subtle obsidian
#

You mean you want to guard it from changing in the UI? If so, just render it as something unchangeable in the UI. For some cards you can remove the edit function. Or just make a template sensor that just reflects the value in an unchangeable way.

tight osprey
#

Third interpretation of what you're asking about: A switch changed and you want to determine whether it was pushed physically, from a dashboard/UI, or from an automation...

If that was you question, then context is the answer.
https://community.home-assistant.io/t/state-context-to-ui-automation/427861/24?u=mekaneck

marsh tinsel
tight osprey
#

not sure what you mean by "which device changed it". If an entity changes state, context can tell you whether it was an automation, a physical interaction, or a call from the UI.

subtle obsidian
#

@marsh tinsel But is the real problem is that you want something that the user can't change?

#

As it seems we're in XY-problem territory 🙂

marsh tinsel
marsh tinsel
tight osprey
#

I agree with the x-y problem. If you just want an unchangeable entity, then you want a template binary sensor. It will just show on/off and can't be changed (because it's a sensor and not a switch)

tight osprey
marsh tinsel
marsh tinsel
tight osprey
# marsh tinsel The sample code doesn't show how to figure out which specific device actually tu...

I think we're going in circles. What is "it"? If you have an input boolen, then it was either an automation or a UI action that turned it on. There is no physical device that could have turned it on. If you have a template switch, it was either an automation, a UI action, or a rendering of the state template that turned it on. If you have a switch entity that is created by an integration that uses a physical device, then if context tells you it was turned on by a physical device then you should know exactly what device that was.

If you're trying to chase down through a chain of automations, you can't do that through context:

https://data.home-assistant.io/docs/context/

Currently, there is no native way to retrieve the original cause of a context in automations or templates.

marsh tinsel
#

Ok sorry about that, let me try starting from the beginning

I have this template lock

template:
  - lock:
      - name: Test Lock
        state: "{{ is_state('switch.test_switch', 'on') }}"
        code_format: "{{ '\\d{4}' if is_state('switch.test_switch', 'on') else None }}"
        lock:
          - action: switch.turn_on
            target:
              entity_id: switch.test_switch
        unlock:
          - variables:
              pin: !secret code
          - condition: "{{ code|int == pin|int }}"
          - action: switch.turn_off
            target:
              entity_id: switch.test_switch

so the test_switch helper is getting set by this lock. in an automation I'm looking to make a condition so it should run anytime the switch is changed not from the lock.

using that example automation if i adjust the lock directly or adjust the switch they both show ran from "HA user interface"

#
alias: debug system context
description: ""
triggers:
  - entity_id:
      - switch.test_switch
    trigger: state
conditions: []
actions:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ trigger.to_state.context.id != none }}"
          - condition: template
            value_template: "{{ trigger.to_state.context.parent_id == none }}"
          - condition: template
            value_template: "{{ trigger.to_state.context.user_id == none }}"
        sequence:
          - data:
              level: error
              message: Physical device
            action: system_log.write
    default: []
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ trigger.to_state.context.id != none }}"
          - condition: template
            value_template: "{{ trigger.to_state.context.parent_id == none }}"
          - condition: template
            value_template: "{{ trigger.to_state.context.user_id != none }}"
        sequence:
          - data:
              level: error
              message: HA user interface
            action: system_log.write
    default: []
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ trigger.to_state.context.id != none }}"
          - condition: template
            value_template: "{{ trigger.to_state.context.parent_id != none }}"
          - condition: template
            value_template: "{{ trigger.to_state.context.user_id == none }}"
        sequence:
          - data:
              level: error
              message: HA automation
            action: system_log.write
    default: []
mode: restart
#

I'm really looking to pull this info which shows on the lock history

subtle obsidian
marsh tinsel
#

The info is definitely in HA it's only a question if we can use it

subtle obsidian
#

But only what? Or only a single automation?

#

if nothing in HA should change it, just only define it as unchangeable type.

marsh tinsel
#

Only that lock entity should be able to change it.

Essentially the switch is just there so the lock template can work (it's kind of dumb that it needs it)

#

So it has to be changeable so the lock can work

grim hedge
marsh tinsel
subtle obsidian
#

Everything is changeable in HA if you can reach it. So I would say if you just do not use it anywhere else it's as good as it will get.

marsh tinsel
#

That's my back up option 😅

#

Is there really no way to access the triggered by?

subtle obsidian
#

Only the parent and context ID's. But that's already after the fact.

marsh tinsel
#

so how does it show in the activty log?

subtle obsidian
#

It does not

#

Or yeah, it does. It's shown as the "by user x" of "by automation y"

marsh tinsel
subtle obsidian
#

So then you will have to see if that will always give you the same parent id

marsh tinsel
#

The parent id is null by both

#

One of these was done from the switch and one from the lock

subtle obsidian
#

Then I'm pretty sure youŕe out of luck

marsh tinsel
#

Thank you for taking the time to have the back and forth

subtle obsidian
#

Still don´t really see the issue. Just don´t use that switch on a dashboard