Enhancing the WhatsApp Addon for Home Assistant
The WhatsApp addon has been a cornerstone of my Home Assistant setup, handling everything from simple notifications and alerts to daily briefings. One of its standout features is the ability to send messages to people who aren’t directly connected to your Home Assistant setup, such as family members. While some use Telegram for similar purposes, WhatsApp remains the more popular option where I live, making it my tool of choice.
The original WhatsApp addon, developed by Giuseppe Castaldo, is based on the Baileys package. Baileys acts as a simulated WhatsApp client, enabling message sending and receiving, while the addon serves as a wrapper for its capabilities. However, Baileys offers features that weren’t utilized in the Gioseppe’s addon.
Motivated by the potential for improvement, I decided to fork the addon and add two key features:
- Message ID Retrieval: Retrieve the ID of a message immediately after it’s sent, allowing actions like deleting, replying, reacting, or editing messages that were sent.
- Mark Messages as Read: Enable the bot to mark messages as read after processing them for a more natural interaction.
The result is my own enhanced WhatsApp addon:
GitHub Repository
Let’s explore how to use these new features.
Setting Up the Enhanced Addon
The installation and setup process for this addon are identical to the original. However, two exciting features have been added, which can be integrated into your automations with ease. Make sure you fully uninstall the previous addon before installing this one, to avoid issues.
Feature 1: Message ID Retrieval
Every time you send a message, a custom event called whatsapp_send_message_result
is triggered. This event contains information about the sent message, including its ID.
To always store the most recent message ID, you can create a helper variable (type: text) called, for example, input_text.whatsapp_sent_message_id
and use the following automation:
alias: Whatsapp store sent message
description: ""
triggers:
- trigger: event
event_type: whatsapp_send_message_result
conditions: []
actions:
- action: input_text.set_value
metadata: {}
data:
value: "{{trigger.event.data.sent_message.key.id}}"
target:
entity_id: input_text.whatsapp_sent_message_id
mode: single
This automation ensures you have the latest message ID stored, opening up possibilities for further actions like replying or editing.
Feature 2: Mark Messages as Read
To mark messages as read after processing, you can use the following automation that uses a new action:
alias: New message in whatsapp group
description: ""
triggers:
- event_type: new_whatsapp_message
trigger: event
actions:
- action: whatsapp.read_messages
metadata: {}
data:
clientId: default
body:
keys:
id: "{{ trigger.event.data.key.id }}"
remoteJid: "{{ trigger.event.data.key.remoteJid }}"
fromMe: "{{ trigger.event.data.key.fromMe }}"
This functionality adds a more natural interaction to your bot, allowing it to behave like a typical WhatsApp user by marking messages as read after responding.
Integrating the New Features
These features can be seamlessly embedded into your automations. For example, in a conversation sequence with your bot, you can retrieve the message ID for dynamic responses or mark messages as read for a more polished experience.
With these upgrades, your WhatsApp addon becomes even more powerful, offering flexibility and new possibilities for your Home Assistant setup.
Conclusion
The original WhatsApp addon is an incredible tool, but with these enhancements, it’s now even better. By adding message ID retrieval and the ability to mark messages as read, your automations can achieve a new level of sophistication. Head over to my GitHub repository to get started with the enhanced version and take your Home Assistant notifications to the next level.