#Automate Based on Triggered By
1 messages · Page 1 of 1 (latest)
Will that work to let me know when someone switches a switch from the UI for example?
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.
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.
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
This nugget was very useful, thank you! With this I can now differentiate the 3 ways a device’s state was changed. Here is a sample implementation using the choose action: trigger: - platform: state entity_id: - switch.office condition: [] action: - choose: - conditions: - condition: template value...
That is my question, but I tried with context ID and it isn't telling me which device changed it if it was updated by a on_turn_on for example
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.
@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 🙂
How would I write that up in yaml?
Either from the UI or an automation, I know I can make it read only but I want an extra safety
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)
The link goes directly to a post with a block of YAML code
I can't because it's a switch which is tied to a lock
Really what I want is very simple, a switch template that needs a code to be turned on. But because only lock supports it and I need the lock tp be based on a switch...
The sample code doesn't show how to figure out which specific device actually turned it on
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.
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
Then just make a template sensor to display it and be done with it 🙂
It's not just for displaying it, I don't want to be able to be changed by an automation for example
The info is definitely in HA it's only a question if we can use it
But only what? Or only a single automation?
if nothing in HA should change it, just only define it as unchangeable type.
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
What are you trying to achieve in the first place?
Have a switch which to turn it on requires a code
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.
Only the parent and context ID's. But that's already after the fact.
so how does it show in the activty log?
It does not
Or yeah, it does. It's shown as the "by user x" of "by automation y"
See this image it shows when the lock is triggered by the lock or by the switch
So then you will have to see if that will always give you the same parent id
The parent id is null by both
One of these was done from the switch and one from the lock
Then I'm pretty sure youŕe out of luck
Thank you for taking the time to have the back and forth
Still don´t really see the issue. Just don´t use that switch on a dashboard