Improve logging for Telegram Question blueprint

This commit is contained in:
2026-01-30 05:00:40 +03:00
parent 49eddd482c
commit 5f1cdb6874

View File

@@ -17,6 +17,11 @@
# - From notify entities with friendly names like "Alex (123456789)" # - From notify entities with friendly names like "Alex (123456789)"
# The number in parentheses is extracted as the chat ID # The number in parentheses is extracted as the chat ID
# #
# Multiple Telegram Bots:
# If you have multiple Telegram bots configured in Home Assistant, you must
# specify the config_entry_id to identify which bot to use.
# Find it in: Settings → Devices & Services → Telegram Bot → your bot → URL
#
# Callback Data Format: # Callback Data Format:
# Each button sends callback data: /<keyboard_id>_<button_index> # Each button sends callback data: /<keyboard_id>_<button_index>
# Example: /my_keyboard_0 for first button of keyboard "my_keyboard" # Example: /my_keyboard_0 for first button of keyboard "my_keyboard"
@@ -62,6 +67,18 @@ blueprint:
domain: notify domain: notify
multiple: true multiple: true
config_entry_id:
name: Telegram Bot Config Entry ID
description: >
Required if you have multiple Telegram bots configured.
Find this in Home Assistant: Settings → Devices & Services → Telegram Bot
→ Click on your bot → look at the URL, the ID is after /config_entry/
Example: "01JKXXXXXXXXXXXXXXXXXXX"
Leave empty if you only have one Telegram bot.
default: ""
selector:
text:
# ------------------------------------------------------------------------- # -------------------------------------------------------------------------
# Message Configuration # Message Configuration
# ------------------------------------------------------------------------- # -------------------------------------------------------------------------
@@ -157,6 +174,22 @@ blueprint:
selector: selector:
action: {} action: {}
# -------------------------------------------------------------------------
# Debug
# -------------------------------------------------------------------------
debug_group:
name: "Debug"
collapsed: true
input:
enable_debug:
name: Enable Debug Notifications
description: >
Send persistent notifications for debugging automation behavior.
Shows resolved chat IDs, callback data, and configuration.
default: false
selector:
boolean:
# Parallel mode allows multiple button presses to be processed simultaneously # Parallel mode allows multiple button presses to be processed simultaneously
mode: parallel mode: parallel
@@ -179,6 +212,7 @@ variables:
hide_keyboard_on_press: !input hide_keyboard_on_press hide_keyboard_on_press: !input hide_keyboard_on_press
hide_message_on_press: !input hide_message_on_press hide_message_on_press: !input hide_message_on_press
answers: !input answers answers: !input answers
config_entry_id: !input config_entry_id
# Callback references (needed for condition checks) # Callback references (needed for condition checks)
button_1_callback: !input button_1_callback button_1_callback: !input button_1_callback
@@ -200,33 +234,44 @@ variables:
{{ callback_data.startswith('/' ~ keyboard_id ~ '_') }} {{ callback_data.startswith('/' ~ keyboard_id ~ '_') }}
{% endif %} {% endif %}
# Debug flag - set to true to enable persistent notifications for troubleshooting # Debug flag
is_debug: false is_debug: !input enable_debug
# ============================================================================= # =============================================================================
# Actions # Actions
# ============================================================================= # =============================================================================
action: action:
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Debug Logging (optional) # Debug Logging - Callback Events
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
- choose: - choose:
- conditions: - conditions:
- condition: template - condition: template
value_template: "{{ is_debug }}" value_template: "{{ is_debug and not is_manual_trigger }}"
sequence: sequence:
- service: persistent_notification.create - service: persistent_notification.create
data: data:
title: "Telegram Keyboard Debug" title: "Telegram Keyboard Debug - Callback"
message: > message: >
Trigger platform: {{ trigger.platform | default('manual') }} **Trigger Info:**
Is manual trigger: {{ is_manual_trigger }} - Platform: {{ trigger.platform | default('unknown') }}
Is our callback: {{ is_our_callback }} - Is our callback: {{ is_our_callback }}
{% if not is_manual_trigger %}
Callback data: {{ trigger.event.data.data | default('N/A') }} {% if is_our_callback %}
Chat ID: {{ trigger.event.data.chat_id | default('N/A') }} **Callback Data:**
- Raw data: {{ trigger.event.data.data | default('N/A') }}
- Chat ID: {{ trigger.event.data.chat_id | default('N/A') }}
- Message ID: {{ trigger.event.data.message.message_id | default('N/A') }}
- Keyboard ID: {{ keyboard_id }}
{% else %}
**Ignored** (callback not for this keyboard)
- Expected prefix: /{{ keyboard_id }}_
- Received: {{ trigger.event.data.data | default('N/A') }}
{% endif %} {% endif %}
**Config:**
- Config Entry ID: {{ config_entry_id if config_entry_id | length > 0 else '(not set)' }}
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Handle Button Press (Callback Event) # Handle Button Press (Callback Event)
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
@@ -271,6 +316,8 @@ action:
# Reply to original message unless we're deleting it # Reply to original message unless we're deleting it
reply_to_message_id: > reply_to_message_id: >
{{ omit if hide_message_on_press else message_id }} {{ omit if hide_message_on_press else message_id }}
config_entry_id: >
{{ config_entry_id if config_entry_id | length > 0 else omit }}
# Handle message/keyboard cleanup # Handle message/keyboard cleanup
- choose: - choose:
@@ -281,6 +328,8 @@ action:
data: data:
chat_id: "{{ chat_id }}" chat_id: "{{ chat_id }}"
message_id: "{{ message_id }}" message_id: "{{ message_id }}"
config_entry_id: >
{{ config_entry_id if config_entry_id | length > 0 else omit }}
# Just remove keyboard (keep message) # Just remove keyboard (keep message)
- conditions: "{{ hide_keyboard_on_press }}" - conditions: "{{ hide_keyboard_on_press }}"
@@ -290,6 +339,8 @@ action:
chat_id: "{{ chat_id }}" chat_id: "{{ chat_id }}"
message_id: "{{ message_id }}" message_id: "{{ message_id }}"
inline_keyboard: [] inline_keyboard: []
config_entry_id: >
{{ config_entry_id if config_entry_id | length > 0 else omit }}
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Send Initial Message (Manual Trigger / Service Call) # Send Initial Message (Manual Trigger / Service Call)
@@ -304,13 +355,14 @@ action:
chat_ids: !input chat_ids chat_ids: !input chat_ids
# Extract chat IDs from notify entity friendly names # Extract chat IDs from notify entity friendly names
# Format: "Name (123456789)" -> extracts "123456789" # Format: "Name (123456789)" or "Name (-123456789)" -> extracts the number
# Supports both positive (user) and negative (group/channel) chat IDs
chat_entities: !input chat_entities chat_entities: !input chat_entities
chat_ids_from_entities: > chat_ids_from_entities: >
{% set ns = namespace(ids=[]) %} {% set ns = namespace(ids=[]) %}
{% for entity in chat_entities %} {% for entity in chat_entities %}
{% set friendly_name = state_attr(entity, 'friendly_name') | default('') %} {% set friendly_name = state_attr(entity, 'friendly_name') | default('') %}
{% set match = friendly_name | regex_findall('\((\d+)\)') %} {% set match = friendly_name | regex_findall('\\((-?\\d+)\\)') %}
{% if match | length > 0 %} {% if match | length > 0 %}
{% set ns.ids = ns.ids + [match[0]] %} {% set ns.ids = ns.ids + [match[0]] %}
{% endif %} {% endif %}
@@ -331,6 +383,36 @@ action:
{% endfor %} {% endfor %}
{{ ns.buttons }} {{ ns.buttons }}
# Debug logging for manual trigger
- choose:
- conditions:
- condition: template
value_template: "{{ is_debug }}"
sequence:
- service: persistent_notification.create
data:
title: "Telegram Keyboard Debug - Send Message"
message: >
**Chat ID Resolution:**
- Direct chat IDs: {{ chat_ids | join(', ') if chat_ids | length > 0 else '(none)' }}
- From entities: {{ chat_ids_from_entities | join(', ') if chat_ids_from_entities | length > 0 else '(none)' }}
- **Resolved IDs: {{ result_chat_ids | join(', ') if result_chat_ids | length > 0 else '(none)' }}**
**Entity Parsing:**
{% for entity in chat_entities %}
- {{ entity }}: "{{ state_attr(entity, 'friendly_name') | default('N/A') }}"
{% else %}
- (no entities configured)
{% endfor %}
**Message:**
- Keyboard ID: {{ keyboard_id }}
- Buttons: {{ buttons | join(', ') }}
- Message: {{ message_text[:100] }}{{ '...' if message_text | length > 100 else '' }}
**Config:**
- Config Entry ID: {{ config_entry_id if config_entry_id | length > 0 else '(not set)' }}
# Validate we have at least one chat ID # Validate we have at least one chat ID
- choose: - choose:
- conditions: "{{ result_chat_ids | length == 0 }}" - conditions: "{{ result_chat_ids | length == 0 }}"
@@ -343,3 +425,5 @@ action:
target: "{{ result_chat_ids }}" target: "{{ result_chat_ids }}"
message: "{{ message_text }}" message: "{{ message_text }}"
inline_keyboard: "{{ inline_keyboard }}" inline_keyboard: "{{ inline_keyboard }}"
config_entry_id: >
{{ config_entry_id if config_entry_id | length > 0 else omit }}