i have this in a couple of automations to set a variable which i then use in an if block as a condition within the automation to do different things depending on the source of the trigger.
triggertype: |-
{% set id = trigger.to_state.context.id %}
{% set parent = trigger.to_state.context.parent_id %}
{% set user = trigger.to_state.context.user_id %}
{% set output = "default" %}
{% if ((id!=None, parent, user) == (true, None, None)) %}
{% set output = "Physical Switch" %}
{% endif %}
{% if ((id!=None, parent, user!=None) == (true, None, true)) %}
{% set output = "HA Switch" %}
{% endif %}
{% if ((id!=None, parent!=None, user) == (true, true, None)) %}
{% set output = "HA Auto" %}
{% endif %}
{{output}}
alias: Get Trigger Origin```
the `triggertype` will then be
`Physical Switch` if it was done by the switch, `HA Switch`if it was done by a dashboard button and `HA Auto`if it was done by an automation