{"id":13554,"date":"2023-06-23T17:44:22","date_gmt":"2023-06-23T17:44:22","guid":{"rendered":"https:\/\/www.rjt.org.uk\/home\/?post_type=home_assistant_tip&#038;p=13554"},"modified":"2024-02-21T18:04:24","modified_gmt":"2024-02-21T18:04:24","slug":"setting-up-a-message-box-in-home-assistant","status":"publish","type":"home_assistant_tip","link":"https:\/\/www.rjt.org.uk\/home\/archives\/home-assistant-tip\/setting-up-a-message-box-in-home-assistant\/","title":{"rendered":"Setting up a Message Box in Home Assistant"},"content":{"rendered":"\n<p><strong>This has been replaced now with the new improved todo list options<\/strong>.<\/p>\n\n\n\n<p>I wanted a way to store up messages,  such as &#8220;You have a new voicemail&#8221;, which could be read out easily when we get in and be used to flash the lights if we needed to read them.  I found a very useful <a href=\"https:\/\/community.home-assistant.io\/t\/heads-up-2023-6-longer-has-persistent-notifications-in-states-what-to-do\/578654\/27\">post<\/a> by &#8220;123 Taras&#8221; and worked it up to a fairly easy solution.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Add the template trigger to the Template: section of your Configuration.yaml<\/li>\n\n\n\n<li>Create a new script using the code below. It is fully prompted and can be tested easily in Dev tools.<\/li>\n\n\n\n<li>To add a message use call service to call the script and select &#8220;create message&#8221; with optional id<\/li>\n\n\n\n<li>Messages with id&#8217;s can be removed using &#8220;delete message&#8221; with the id<\/li>\n\n\n\n<li>All messages can be removed using &#8220;Delete All Messages&#8221;<\/li>\n\n\n\n<li>The macro can be used to list all the messages in a Markdown box or Template Card.<\/li>\n<\/ol>\n\n\n\n<p>The core is a template sensor controlled by a trigger, which needs to be added to a package or to the configuration.yaml.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Template Trigger: <\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"yaml\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># ------------------------------------------------------------ Messaging System\n\n- trigger:\n    - platform: event\n      event_type:\n        - message_create\n        - message_delete\n        - message_delete_all\n  sensor:\n    - name: Messages\n      state: \"{{ now().timestamp() | timestamp_custom() }}\"\n      attributes:\n        messages: >\n          {% set msgs = this.attributes.get('messages', []) %}\n          {% if trigger.event.event_type == 'message_create' %}\n            {% set new = [{\n                \"id\": trigger.event.data.id | default('TS' ~ now().timestamp()),\n                \"title\": trigger.event.data.title | default(''),\n                \"message\": trigger.event.data.message | default(''),\n                \"time\": now().isoformat() }] %}\n            {{ (msgs + new) }}\n          {% elif trigger.event.event_type == 'message_delete' %}\n            {% if trigger.event.data.id is defined %}\n              {% set msgs = msgs | rejectattr('id', 'eq', trigger.event.data.id) | list %}\n            {% elif trigger.event.data.index is defined and trigger.event.data.index | is_number %}\n              {% set t = trigger.event.data.index | int(0) - 1 %}\n              {% if 0 &lt;= t &lt; msgs|count %}\n                {% set msgs = msgs | rejectattr('id', 'eq', msgs[t].id) | list %}\n              {% endif %}\n            {% endif %}\n            {{ msgs }}\n          {% else %}\n            {{ [] }}\n          {% endif %}<\/pre>\n\n\n\n<p>This can add messages and remove them by ID or all messages.  In order to make it easy to remember the calls,  I created a single Messages Update Script complete with parameters to allow messages to be added, removed etc.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Message update script:<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">alias: Messages Update\nsequence:\n  - choose:\n      - conditions:\n          - condition: template\n            value_template: \"{{action == 'create_message'}}\"\n        sequence:\n          - event: message_create\n            event_data:\n              id: \"{{id | default('TS' ~ now().timestamp())}}\"\n              title: \"{{title}}\"\n              message: \"{{message}}\"\n      - conditions:\n          - condition: template\n            value_template: \"{{action == 'delete_message'}}\"\n        sequence:\n          - event: message_delete\n            event_data:\n              id: \"{{id | default('TS' ~ now().timestamp())}}\"\n      - conditions:\n          - condition: template\n            value_template: \"{{action == 'delete_all_messages'}}\"\n        sequence:\n          - event: message_delete_all\n            event_data: {}\nmode: single\nfields:\n  action:\n    name: Action\n    description: Action to take,  title and message are only required for Create\n    selector:\n      select:\n        options:\n          - label: Create\n            value: create_message\n          - label: Delete Message By ID\n            value: delete_message\n          - label: Delete All Messages\n            value: delete_all_messages\n  id:\n    name: Message ID\n    description: Optional for Create, mandatory for delete by id\n    selector:\n      text: null\n  title:\n    name: Message Title\n    description: title for message\n    selector:\n      text: null\n  message:\n    name: Message\n    description: Message Text, mandatory for Create only\n    selector:\n      text: null\n<\/pre>\n\n\n\n<p>The script above can be called easily in the Script UI or from the Developers tools.  Finally a quick Jinja macro to read the messages or tell you there are none, which can be called direct from an intent or via a script for Alexa.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Jinja Macro to read the messages:<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">{%- macro read_messages() -%}\n{% from 'easy_time.jinja' import easy_time  %} \n{% set msgs = state_attr('sensor.messages', 'messages') %}\n{% set count = msgs| count %}\n{%- if count == 0 -%}\nYou have no messages.\n{%- else -%}\n{% for m in msgs %}\nMessage {{loop.index}} received {{easy_time(m.time) }} ago\n{{ m.title }}, {{ m.message }}.\n{%- endfor %}\n{%- endif -%}\n{%- endmacro -%}<\/pre>\n\n\n\n<p>Note: The above script uses <a href=\"https:\/\/github.com\/Petro31\/easy-time-jinja\/blob\/main\/easy_time.jinja\">Petro31&#8217;s easy_time macros<\/a> available on HACS to read out how old the message is.  <\/p>\n","protected":false},"template":"","class_list":["post-13554","home_assistant_tip","type-home_assistant_tip","status-publish","hentry","comments-off"],"_links":{"self":[{"href":"https:\/\/www.rjt.org.uk\/home\/wp-json\/wp\/v2\/home_assistant_tip\/13554","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rjt.org.uk\/home\/wp-json\/wp\/v2\/home_assistant_tip"}],"about":[{"href":"https:\/\/www.rjt.org.uk\/home\/wp-json\/wp\/v2\/types\/home_assistant_tip"}],"wp:attachment":[{"href":"https:\/\/www.rjt.org.uk\/home\/wp-json\/wp\/v2\/media?parent=13554"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}