This guide explains how to set up an interactive WhatsApp bot that communicates with Home Assistant's Assistant and automatically captures and stores WhatsApp user and group IDs from incoming WhatsApp messages. This setup allows you to both automate conversations through Home Assistant's Assistant and maintain a dynamic record of users and groups.
This blueprint enables your WhatsApp bot to interact directly with Home Assistant's Assistant, allowing for real-time conversation handling. In addition, it automatically tracks users and group IDs, updating your Home Assistant configuration with each unique ID that interacts with your bot.
To use this automation, ensure you have the following:
- The WhatsApp integration add-on by Giuseppe Castaldo configured in Home Assistant. This add-on is required for receiving WhatsApp messages in Home Assistant.
- The automation blueprint and the appropriate permissions set up to trigger on incoming WhatsApp messages.
You can quickly import this blueprint into your Home Assistant instance using the button below:
We use two input_select entities to store the WhatsApp user and group IDs. These dropdown lists will automatically update with new IDs as messages are received from different users or groups.
Stores the WhatsApp user IDs of individuals who send messages to your bot. This entity will automatically update when a new user sends a message.
Define this input_select in your configuration:
input_select:
whatsapp_user_ids:
name: WhatsApp User IDs
options:
- None
icon: mdi:accountStores the WhatsApp group IDs of groups where your bot receives messages. This entity will automatically update when a new group ID is detected.
Define this input_select in your configuration:
input_select:
whatsapp_chat_ids:
name: WhatsApp Group IDs
options:
- None
icon: mdi:account-groupThis automation listens for incoming WhatsApp messages, checks whether the sender is a user or a group, and engages with Home Assistant's Assistant. Depending on the ID type, it updates the appropriate input_select with the new ID if it hasn’t been recorded before.
Add the following automation code to your Home Assistant configuration to enable automatic storage of new WhatsApp user and group IDs and interact with Home Assistant’s Assistant:
alias: "Automation to Save New WhatsApp User and Group IDs"
description: "Automation to automatically store new WhatsApp user and group IDs and interact with the Home Assistant Assistant"
trigger:
- platform: event
event_type: new_whatsapp_message
condition: []
action:
- choose:
- conditions:
- condition: template
value_template: >
{{ trigger.event.data["key"]["remoteJid"].endswith("@s.whatsapp.net") }}
sequence:
- service: input_select.set_options
data:
entity_id: input_select.whatsapp_user_ids
options: >
{% set new_id = trigger.event.data["key"]["remoteJid"].split("@")[0] %}
{% set current_ids = state_attr('input_select.whatsapp_user_ids', 'options') %}
{% if new_id not in current_ids %}
{{ (current_ids + [new_id]) | list }}
{% else %}
{{ current_ids }}
{% endif %}
- service: conversation.process
data:
text: "{{ trigger.event.data['message']['conversation'] | default('') }}"
conversation_id: "whatsapp_{{ trigger.event.data['key']['remoteJid'] }}"
agent_id: conversation.home_assistant
response_variable: response
- service: whatsapp.send_message
data:
clientId: whatsapp
to: "{{ trigger.event.data['key']['remoteJid'] }}"
body:
text: "{{ response.response.speech.plain.speech }}"
- conditions:
- condition: template
value_template: >
{{ trigger.event.data["key"]["remoteJid"].endswith("@g.us") }}
sequence:
- service: input_select.set_options
data:
entity_id: input_select.whatsapp_chat_ids
options: >
{% set new_id = trigger.event.data["key"]["remoteJid"].split("@")[0] %}
{% set current_ids = state_attr('input_select.whatsapp_chat_ids', 'options') %}
{% if new_id not in current_ids %}
{{ (current_ids + [new_id]) | list }}
{% else %}
{{ current_ids }}
{% endif %}
mode: single- Trigger: The automation listens for the
new_whatsapp_messageevent, triggered each time a WhatsApp message is received. - Conditions: The
chooseblock evaluates the message source to determine if it’s a user or group.- User ID: If the
remoteJidends with@s.whatsapp.net, it identifies the message as coming from an individual user. The user’s ID (phone number) is then extracted and stored ininput_select.whatsapp_user_idsif it’s not already there. - Group ID: If the
remoteJidends with@g.us, it identifies the message as coming from a group. The group ID is then extracted and stored ininput_select.whatsapp_chat_idsif it’s not already there.
- User ID: If the
- Actions: For each new user or group ID, the automation updates the appropriate
input_selectentity with the new ID, ensuring there are no duplicate entries. For users, it also processes their message through Home Assistant's Assistant, and the response is sent back via WhatsApp.
When a WhatsApp message is received, this automation will:
- Check if the message is from a user or a group.
- If it’s from a user, it:
- Extracts the user’s ID (without the
@s.whatsapp.netsuffix) and adds it to thewhatsapp_user_idslist if it’s new. - Processes the message through Home Assistant's Assistant and sends the response back to the user.
- Extracts the user’s ID (without the
- If it’s from a group, it extracts the group ID (without the
@g.ussuffix) and adds it to thewhatsapp_chat_idslist if it’s new.
This setup allows you to maintain a real-time list of WhatsApp users and groups interacting with your bot and provides interactive responses through Home Assistant’s Assistant.
- Initial Options: You can change the default option (currently set to
None) in theinput_selectentities to suit your needs. - Icons: Customize the icons for
whatsapp_user_idsandwhatsapp_chat_idsusing any icon from Material Design Icons.
- Ensure that the WhatsApp integration is working correctly and messages are being received by Home Assistant.
- Verify that
input_select.whatsapp_user_idsandinput_select.whatsapp_chat_idsare correctly defined in your configuration. - Check the automation logs in Home Assistant if the IDs are not updating as expected.
This automation is based on the original concept of the Telegram bot blueprint by marc-romu.
By following this guide, you will have an automation that dynamically tracks new WhatsApp user and group IDs, interacts with Home Assistant’s Assistant in real-time, and keeps your input_select entities updated. This can be especially useful for creating custom notifications, filtering messages, and logging interactions.