Conserve order of button presses for automation

Hi folks,

I want to conserve the order of in which a set of buttons was pressed on the front-end to use that order in an automation. The scenario is, that the following buttons control where our vacuum robot should go:

My currently only idea is to append the rooms comma separated to a input_text helper, but will be quite some coding hassle in automations.

Does anyone have a better idea?

Thanks in advance!
Toby

Maybe this helps generate some ideas: You can use an input_select helper to hold an ordered list, and you can add items to that list using a service call:

service: input_select.set_options
data:
  options: >-
    {{ state_attr('input_select.test_input_select','options') + ['my new option'] }}
target:
  entity_id: input_select.test_input_select

I’m not familiar with how your vacuum robot integration works, but perhaps you can remove an item from the list as soon as the robot starts (or finishes) that room. You’d probably need a button to clear the list also; though you’d have to set it to [‘empty’] or similar because there has to be at least one value in an input_select.

Hi,

Thanks for the input!

If I got you correct, I can simple use the options available in such a helper as a sorted list, right? Pretty nifty, I did not think about it like that.

The integration actually receives a YAML list kg values as input so it should not be that much of a hassle.

Cheers
Toby

Yes that should work. I be interested to see what you come up with if you develop a solution that works for you.

Be advised that if Home Assistant is restarted, whatever options you added to the Input Select will be lost; on startup, the Input Select’s options are reset to default values.

Here’s a Trigger-based Template Sensor that allows you to use custom events to add/delete room names to/from a list. Each room name also includes the time when it was added. A Trigger-based Template Sensor retains its data after a restart.

template:
  - trigger:
      - platform: event
        event_type:
          - room_add
          - room_delete
          - room_delete_all
    sensor:
      - name: Rooms
        state: "{{ now().timestamp() | timestamp_custom() }}"
        attributes:
          rooms: >
            {% set rooms = this.attributes.get('rooms', []) %}
            {% if trigger.event.event_type == 'room_add'
              and trigger.event.data.room is defined
              and trigger.event.data.room | length > 0 %}
              {% set new = [{
                  "name": trigger.event.data.room,
                  "time": now().isoformat() }] %}
              {{ (rooms + new) }}
            {% elif trigger.event.event_type == 'room_delete' %}
              {% if trigger.event.data.room is defined %}
                {% set rooms = rooms | rejectattr('name', 'eq', trigger.event.data.room) | list %}
              {% elif trigger.event.data.index is defined and trigger.event.data.index | is_number %}
                {% set t = trigger.event.data.index | int(0) - 1 %}
                {% if 0 <= t < rooms|count %}
                  {% set rooms = rooms | rejectattr('name', 'eq', rooms[t].name) | list %}
                {% endif %}
              {% endif %}
              {{ rooms }}
            {% else %}
              {{ [] }}
            {% endif %}

Here are the three custom events for managing rooms (for example, each one of your Button cards would use its call_action option to post a room_add event):

1. room_add

Add a room to the list.

Example

  - event: room_add
    event_data:
      room: kitchen

2. room_delete

Delete a room by either its name or index value (i.e. its index position in the list of all messages). Index values start with 1 (not 0).

Example by name

  - event: room_delete
    event_data:
      name: kitchen 

Example by index

  - event: room_delete
    event_data:
      index: 1

3. room_delete_all

Deletes all rooms in the list.

Example

  - event: room_delete_all

Here’s a simple template to list all room names separated by commas:

{{ state_attr('sensor.rooms', 'rooms') 
  | map(attribute='name') | list | join(', ') }}


NOTE

This Trigger-based Template Sensor is based on a similar one I had created for an entirely different purpose. The other one was well tested but this one is not. While adapting it to your needs I may have made a mistake or two so be sure to test it thoroughly and let me know if you encounter any bugs.


EDIT

Correction. Overlooked to rename messages to rooms.

1 Like

That’s pretty awesome. I have no need for it but I wanted to play around with it anyway. You forgot to replace the attribute messages: with rooms: but other than that it works flawlessly. Very neat solution.

I’ve been recently learning about trigger-based template entities and they seem to great solutions for so many problems.

Thanks for spotting the mistake; fixed.