Condensing Multiple Similar Automations into One Automation

Hi Guys,
I have multiple Rasberrys with HASSIO controlling relays and inputs as slaves. Slave Pi’s talk back to main Hassio via MQTT and all works well. BUT
The automation on each pi is very similar and long winded and would like to condense the similare automation routings into either one or two routines for a more elegent way of doing things.
Example. I have 16 relays each with 16 automations to send the MQTT back to the main instance.As these 16 seperate automations are the same but with relay number different it should be easy to template out the relay number and perform the automation in one hit. !!


- alias: Relay 01-ON
  trigger:
  - platform: mqtt
    topic: "slave_2/relay_1"
    payload: 'on'
  action:
  - service: switch.turn_on
    entity_id: switch.relay_1
  - service: mqtt.publish
    data:
      topic: 'slave_2/relay_1_state'
      payload: 'on'
    
- alias: Relay 01-OFF
  trigger:
  - platform: mqtt
    topic: "slave_2/relay_1"
    payload: 'off'
  action:
  - service: switch.turn_off
    entity_id: switch.relay_1
  - service: mqtt.publish
    data:
      topic: 'slave_2/relay_1_state'
      payload: 'off'

As you can see its not the best way to automation and Im sure templating would be able to reduce the above down from 16 seperate automations into one.
Thanks for any ideas
Dave

A single automation can handle the on and off states of all 16 relays. The automation can be compact if you are willing to restructure the MQTT topics.

Change this format:

slave_2/relay_1

to this format:

slave_2/relay/1

The relay’s state topic will be:

slave_2/relay/1/state

If you do that then this single automation will handle all the relays:

- alias: 'All Relays'
  trigger:
  - platform: mqtt
    topic: slave_2/relay/+
  action:
  - service_template: switch.turn_{{trigger.payload}}
    data_template:
      entity_id: switch.relay_{{trigger.topic.split('/')[2]}}
  - service: mqtt.publish
    data_template:
      topic: slave_2/relay/{{trigger.topic.split('/')[2]}}/state
      payload: '{{trigger.payload}}'

Screenshot from MQTT Explorer:
Screenshot%20from%202019-10-21%2022-39-19

If you cannot restructure the MQTT topics, then the following automation, with a much more verbose trigger section, will handle all 16 relays.

- alias: 'Relay 1-16 ON-OFF'
  trigger:
  - platform: mqtt
    topic: slave_2/relay_1
  - platform: mqtt
    topic: slave_2/relay_2
  - platform: mqtt
    topic: slave_2/relay_3
  - platform: mqtt
    topic: slave_2/relay_4
  - platform: mqtt
    topic: slave_2/relay_5
  - platform: mqtt
    topic: slave_2/relay_6
  - platform: mqtt
    topic: slave_2/relay_7
  - platform: mqtt
    topic: slave_2/relay_8
  - platform: mqtt
    topic: slave_2/relay_9
  - platform: mqtt
    topic: slave_2/relay_10
  - platform: mqtt
    topic: slave_2/relay_11
  - platform: mqtt
    topic: slave_2/relay_12
  - platform: mqtt
    topic: slave_2/relay_13
  - platform: mqtt
    topic: slave_2/relay_14
  - platform: mqtt
    topic: slave_2/relay_15
  - platform: mqtt
    topic: slave_2/relay_16
  action:
  - service_template: switch.turn_{{trigger.payload}}
    data_template:
      entity_id: switch.{{trigger.topic.split('/')[1]}}
  - service: mqtt.publish
    data_template:
      topic: slave_2/{{trigger.topic.split('/')[1]}}_state
      payload: '{{trigger.payload}}'
1 Like

Worked like a charm. Thank you so much. Realy appreciate your quick response.
I can change the MQTT topics easly to whatever, so theres no restrictions there. I have just tried to transpose your solution from the relays ( which works well ) to the Raspi Inputs.( binary_sensors ).Again 32inputs ,all done via the cluncky seperate automation for each.

- alias: input_03_on
  trigger:
  - platform: state
    entity_id: binary_sensor.input_3
    to: 'on'
  action:
  - service: mqtt.publish
    data:
      topic: 'slave_2/Input_3'
      payload: 'on'
Now , im thinking this is going t be diffenent as I need to check each binary_senor.Input_XX to see which one has changed and pass that value on to the MQTT side ?
Again , sorry to ask but I suspect it would take you 30 seconds to figure out rather than my 30 days ( ok , perhaps 30 hours )
Thanks
@_DavidSheen

Glad to hear it and you’re welcome!

Please mark my post with the Solution tag. Only you, the author of the topic, can do this. It will automatically place a checkmark next to the topic’s title (indicating the topic has a solution) and place a link to the solution within your first post. All of this makes it easier for other users to find the solution.

For the inputs, try this:

- alias: 'Inputs 1-16'
  trigger:
  - platform: state
    entity_id: 
      - binary_sensor.input_1
      - binary_sensor.input_2
      - binary_sensor.input_3
      - binary_sensor.input_4
      - binary_sensor.input_5
      - binary_sensor.input_6
      - binary_sensor.input_7
      - binary_sensor.input_8
      - binary_sensor.input_9
      - binary_sensor.input_10
      - binary_sensor.input_11
      - binary_sensor.input_12
      - binary_sensor.input_13
      - binary_sensor.input_14
      - binary_sensor.input_15
      - binary_sensor.input_16
  action:
  - service: mqtt.publish
    data_template:
      topic: slave_2/{{trigger.to_state.object_id}}
      payload: '{{trigger.payload}}'

The object_id refers to entity’s name. In other words, if given the entity binary_sensor.input_12 the object_id is input_12.

HI 123 Taras, Almost worked. It does output MQTT on any one of the 16 inputs. but only outputs “input_xx” ( XX being 1 to 16 corresponding binary.sensor inputs). There is no additional payload attached to this payload “input_xx” such as ‘on’ or ‘off’
( By the way thanks for pointing me to MQTT explorer Loving that simple program. My new favourate ).
So the MQTT recieved on the master instance of HassIO is simple the topic of ‘slave_2/input_1’ and no payload. ??
Would I assume you would need to split the topic up.Similare to the topic on the ouput section.

topic: slave_2/{{trigger.topic.split(‘/’)[1]}}_state

But then again you cant do that as yourve already split the topic into “input_1”, so perhaps need to add another topic of on or off to the end…brain stating to hurt. I have an 80’s brain with the same 3.5k memory as I was born with and not be upgraded !!! as yet… Thanks Dave

Oops! My mistake; I was rushing through that one.

You want that automation to publish the state of whichever binary_sensor triggered it, correct? If so, change the last line to:

      payload: '{{trigger.to_state.state}}'

That represents the state the binary_sensor changed to. So if it was previously on and it changed to off, the payload’s value will be off.

Glad you like MQTT Explorer. I think the fact it automatically subscribes to all topics and then presents them in a treeview is what helps many people gain a better understanding of the broker’s operation.

Perfection…You must have had a brain upgrade to super computer standards !!. I didnt even get a cartridge slot in my head. !! Thanks Again