TedLasso
Executive Member
- Joined
- Feb 23, 2016
- Messages
- 5,158
- Reaction score
- 2,846
Tried this but haven't moved on .. I get user/automation but I want to know which user specifically (have a few in the house).View attachment 1893750
or
template:
- trigger:
- platform: state
entity_id: switch.your_switch_name # Replace with your entity ID
sensor:
- name: "Your Switch Trigger Source"
unique_id: switch_trigger_source_001
state: >
{% set id = trigger.to_state.context.user_id %}
{% set parent = trigger.to_state.context.parent_id %}
{% if id is not none %}
User
{% elif parent is not none %}
Automation
{% else %}
Physical/Manual
{% endif %}
icon: mdi:information-outline
- user_id: If this is not empty, a specific user logged into the Home Assistant UI or App toggled the switch.
- parent_id: If this is not empty, the change was triggered by another process, most commonly an Automation or a Script.
- Neither: If both are empty, it usually indicates the change happened at the device itself (e.g., someone physically pressed the wall button) or the integration polled a change from the hardware.
YAML:
alias: TEST
description: ""
triggers:
- entity_id: switch.bathroom_geyser_switch_1
trigger: state
actions:
- data:
message: >
{% set id = trigger.to_state.context.user_id %} {% set parent =
trigger.to_state.context.parent_id %} {% if id is not none %}
Switch was triggered by a User
{% elif parent is not none %}
Switch was triggered by an Automation
{% else %}
Switch was triggered Physically/Manually
{% endif %}
title: Switch Trigger Source
action: notify.mobile_app_theboss
CoPilot says I need to build a usermap - something I am trying to avoid
YAML:
alias: TEST
description: ""
trigger:
- platform: state
entity_id: switch.bathroom_geyser_switch_1
action:
- service: notify.mobile_app_theboss
data:
title: "Switch Trigger Source"
message: >
{% set id = trigger.to_state.context.user_id %}
{% set parent = trigger.to_state.context.parent_id %}
{% set usermap = {
"abcd1234userid": "Alice",
"efgh5678userid": "Bob"
} %}
{% if id is not none %}
Switch was triggered by {{ usermap[id] if id in usermap else 'an Unknown User' }}
{% elif parent is not none %}
Switch was triggered by an Automation
{% else %}
Switch was triggered Physically/Manually
{% endif %}
(New state: {{ trigger.to_state.state }})


