Customizing Telegram Notifications for Binary Sensors

I’ve been working on customizing my Telegram notifications for my binary sensors, and I wanted to share my progress and ask for some advice. I have several binary sensors of different classes—moisture, smoke, and motion—and I want to display their statuses in a more user-friendly way on Telegram. Instead of just seeing ‘ON’ or ‘OFF’, I’d like to see statuses like ‘Dry’, ‘Wet’, ‘Clear’, ‘Smoke’, or ‘Detected’, along with appropriate emojis. Here’s what I’ve done so far:

  1. Current Setup: I’ve created an automation that loops through all my binary sensors and checks their states. Depending on the sensor class and state, it sets a specific emoji and status text. For example, a moisture sensor that’s ‘off’ shows a checkmark and ‘Dry’, while one that’s ‘on’ shows a water droplet emoji and ‘Wet’. Similarly, smoke and motion sensors have their own unique emojis and statuses.

  2. Challenges: While this setup works, it feels a bit ‘dirty’ and not very reusable. Every time I want to notify the status of a sensor or a status change, I have to add this code again. I’m also noticing that the ‘Unknown’ status is the same across all sensor classes, which isn’t ideal. I’d like to make this more modular and cleaner.

  3. Desired Solution: I’m looking for a ‘good and right’ way to handle this. Maybe creating separate functions or templates for each sensor class could help. This way, I can easily reuse the code without duplicating it across multiple automations. I also want to ensure that each sensor class has its own unique ‘Unknown’ status instead of sharing the same one.

  4. Questions: Has anyone else tackled a similar problem? How did you structure your automations to make them more reusable? Any tips on organizing the code for better readability and maintainability?

I’d really appreciate any insights or suggestions on how to improve this setup. Thanks in advance!