You’re welcome, @nickh66.
Here is a more complete example using the state_changed
event. Replace light.smart_bulb_3
with some light entity you have.
- alias: "Test"
initial_state: true
trigger:
- platform: event
event_type: "state_changed"
event_data:
entity_id: light.smart_bulb_3
action:
- service: persistent_notification.create
data:
title: Test
message: >-
{% set user_id = trigger.event.context.user_id %}
{% set triggered_by = (states.person | selectattr('attributes.user_id','==', user_id)) | list | first or "nobody" %}
{% set first_name = "System" if triggered_by == "system" else state_attr(triggered_by.entity_id, "friendly_name").split()[0] %}
{{ first_name }} switched {{ trigger.event.data.new_state.state }} {{ trigger.event.data.entity_id }}.
Output:
Relevant documentation:
You can subscribe to events for testing using the developer tools:
Resulting in, for example:
{
"event_type": "state_changed",
"data": {
"entity_id": "light.smart_bulb_3",
"old_state": {
"entity_id": "light.smart_bulb_3",
"state": "on",
"attributes": {
"min_mireds": 111,
"max_mireds": 400,
"supported_color_modes": [
"color_temp",
"hs"
],
"color_mode": "hs",
"brightness": 64,
"hs_color": [
0,
0
],
"rgb_color": [
255,
255,
255
],
"xy_color": [
0.323,
0.329
],
"current_power_w": 0,
"daily_energy_kwh": 0.001,
"monthly_energy_kwh": 0.006,
"friendly_name": "Dining Room Light",
"supported_features": 19,
"icon": "mdi:dome-light"
},
"last_changed": "2021-08-08T15:38:29.325921+00:00",
"last_updated": "2021-08-08T15:38:29.325921+00:00",
"context": {
"id": "redacted",
"parent_id": null,
"user_id": "redacted"
}
},
"new_state": {
"entity_id": "light.smart_bulb_3",
"state": "off",
"attributes": {
"min_mireds": 111,
"max_mireds": 400,
"supported_color_modes": [
"color_temp",
"hs"
],
"current_power_w": 0,
"daily_energy_kwh": 0.001,
"monthly_energy_kwh": 0.006,
"friendly_name": "Dining Room Light",
"supported_features": 19,
"icon": "mdi:dome-light"
},
"last_changed": "2021-08-08T15:38:51.546905+00:00",
"last_updated": "2021-08-08T15:38:51.546905+00:00",
"context": {
"id": "redacted",
"parent_id": null,
"user_id": "redacted"
}
}
},
"origin": "LOCAL",
"time_fired": "2021-08-08T15:38:51.546905+00:00",
"context": {
"id": "redacted",
"parent_id": null,
"user_id": "redacted"
}
}