Smarter automation - how to find mqtt cover to input_select

Hi *,

I have the following sort automation for every room in the house:

- alias: Kitchen Rollershutter Preset
  trigger:
    platform: state
    entity_id: input_select.rollershutter_kitchen
  action:
  - service: mqtt.publish
    data_template:
      topic: house/kitchen/rollershutter/1/set
      payload_template: '{{ states(''input_select.rollershutter_kitchen'') }}'

Input_select entity_ids and mqtt topics are set explicitly.

My question is, how I can have only one automation serving the input_select and mqtt cover of every room?

I am playing with possibilities in my head right know, but nothing I come up with is simple or straight forward as I suppose it should be.

How about something like this:

- alias: Kitchen Rollershutter Preset
  trigger:
    platform: state
    entity_id:
    - input_select.rollershutter_kitchen
    - input_select.thing1_room1
    - input_select.thing2_room2
  action:
  - service: mqtt.publish
    data:
      topic: >
        {% set thing, room = trigger.to_state.object_id.split('_') %}
        house/{{ room }}/{{ thing }}/1/set
      payload: '{{ trigger.to_state.state }}'
1 Like

Thank you! It works perfectly!

1 Like