Another addition now I know why it is executed twice. The error message (friendly_name is undefined) always appears when a window is closed. I just tested it again and when I closed the window the message appeared in the log.
@Malte, I have ask Gemini nan get this result:
The error message âfriendly_name is undefinedâ occurs in the blueprint because the friendly_name
variable is used before itâs defined. This happens when the condition to delete the notification is met (trigger id: delete_notification
).
Letâs see how the code works:
-
Trigger: The blueprint defines two triggers:
send_notification
: This trigger fires when thetrigger_entity
changes to theissue_state
(e.g., door/window is left open) and stays in that state for theduration_issue_state
(e.g., 10 minutes).delete_notification
: This trigger fires when thetrigger_entity
changes from theissue_state
(e.g., door/window is closed).
-
Variables: The code defines a variable
friendly_name
inside a conditional statement within therepeat
loop triggered bysend_notification
. This means thefriendly_name
variable is only defined if thecondition_send_notification
is met. -
Notification Deletion: When the
delete_notification
trigger fires, the code tries to use thefriendly_name
variable to construct the notification message. However, since thedelete_notification
doesnât trigger thecondition_send_notification
, thefriendly_name
variable remains undefined.
Hereâs how to fix the error:
There are two ways to fix this error:
- Move the friendly_name definition outside the conditional statement:
variables:
custom_friendly_name: !input friendly_name
# ... other variables
sequence:
- repeat:
# ... your loop here
variables:
friendly_name: "{% if custom_friendly_name != '' %}\n {{ custom_friendly_name }}\n{% else %}..."
This way, the friendly_name
variable is defined before itâs used in both notification creation and deletion.
- Use a default value for friendly_name:
variables:
custom_friendly_name: !input friendly_name
# ... other variables
sequence:
- repeat:
# ... your loop here
variables:
friendly_name: "{{ custom_friendly_name | default('Unknown') }}"
This way, if custom_friendly_name
is undefined, it will default to âUnknownâ to avoid the error.
Both approaches will ensure that the friendly_name
variable is always defined and prevent the error message.