Hello guys!
I have some binary sensors of different classes: moisture, smoke and motion.
Using telegram notification I want to see a status of sensor in question. Not just ON/OFF but
Clear/Detected, Dry/Wet, Clear/Smoke and so on. In adition I want to set an emoji depending of sensor status and class.
I did it like this.
action:
- service: telegram_bot.send_message
data_template:
target: '{{ trigger.event.data.chat_id }}'
message: >-
{%- for state in states.binary_sensor %}
{%- if is_state(state.entity_id,'off') %}
{%- set status_icon= '\U00002705' %}
{%- elif is_state(state.entity_id,'unknown') %}
{%- set status_icon= '\U00002754' %}
{%- set status_text = 'Unknown' %}
{%- endif %}
{%- if is_state_attr(state.entity_id,'device_class','moisture') %}
{%- if is_state(state.entity_id,'off') %}
{%- set status_text = 'Dry' %}
{%- elif is_state(state.entity_id,'on') %}
{%- set status_icon= '\U0001f4a7' %}
{%- set status_text = 'Wet' %}
{%- endif %}
{%- endif %}
{%- if is_state_attr(state.entity_id,'device_class','smoke') %}
{%- if is_state(state.entity_id,'off') %}
{%- set status_text = 'Clear' %}
{%- elif is_state(state.entity_id,'on') %}
{%- set status_icon= '\U0001f6ac' %}
{%- set status_text = 'Smoke' %}
{%- endif %}
{%- endif %}
{%- if is_state_attr(state.entity_id,'device_class','motion') %}
{%- if is_state(state.entity_id,'off') %}
{%- set status_text = 'Clear' %}
{%- elif is_state(state.entity_id,'on') %}
{%- set status_icon= '\U0001f6b6' %}
{%- set status_text = 'Detected' %}
{%- endif %}
{%- endif %}
{{ status_icon }}{{ state.name }} : {{ status_text }}
{%- endfor %}
✅Water Leakage Bathroom : Dry
✅Motion Entrance : Clear
✅Kitchen Smoke detector : Clear
✅Water Leakage Kitchen : Dry
✅Tech Smoke detector : Clear
✅Water Leakage Toilet : Dry
Status Unknown (text and emoji) is identical for all sensor class. Status OFF have identical emoji but different text. Status ON text and emoji are different for all sensor types.
As of me it’s too “dirty”. Code is not reusable. I need to add this in every automation where I want to notify status or status change.
What is a “good and right” way to do so?