MQTT automation light control to template automation

I currently have the following automation in order to switch on and off a double_garage light in Home Assistant when an on/off message is received via MQTT hub.

automation:
  - id: '11'
    alias: turn on lights
    trigger:
      platform: mqtt
      topic: 'light/double_garage'
      payload: "on"
      encoding: "utf-8"
    action:
      - service: light.turn_on
        data:
          entity_id:
            - light.double_garage
  - id: '12'
    alias: turn off lights
    trigger:
      platform: mqtt
      topic: 'light/double_garage'
      payload: "off"
      encoding: "utf-8"
    action:
      - service: light.turn_off
        data:
          entity_id:
            - light.double_garage

That’s a lot of code for one single light! any way to template the above for any light matching for instance a regex??

Why do you not use an MQTT light?

I actually have it in my configuration.yml:

light:
  - platform: mqtt
    command_topic: 'light/double_garage'
    name: Double Garage
    optimistic: true

however the mqtt light only sends correct ON and OFF messages to the topic “light/double_garage” but when I do send an ON or OFF message myself through either mosquitto_publish or the HA console the light does not respond.

Did you publish to the same topic when you did it manually? This should work. The MQTT light doesn’t care where the command comes from.

yes… sent the same message and subscribing from another host and listening to #topic shows the same identical messages as received by the MQTT hub.
Yet lights only respond when clicked not through MQTT messages.

mosquitto_sub -v -P Dxxxxxx -t # -p 1883
Warning: Not using password since username not set.
light/double_garage OFF
light/double_garage ON

you’re sending “on” and “off” from home assistant and “ON” and “OFF” in your example. Maybe it’s case sensitive?

nope, tried uppercase, lowercase, 0, 1 nothing.
Beyond that, if I could parametrize the automation it would not be a problem for me, but currently it is too verbose to specify two rules for each switch! any idea?

You could try a template light

  - platform: template
    lights:
     double_garage:
        turn_on:
          - service: mqtt.publish
            data:
              payload: 'on'
              topic: 'light/double_garage'
        turn_off:
          - service: mqtt.publish
            data:
              payload: 'off'
              topic: light/double_garage

I don’t understand how this is possible, the MQTT light doesn’t check where the message came from.

Ok, here’s a question. Why are you supplying a password without specifying the username?

Yeah good point, I left the parameter there to remember in the future to enable authentication but since I specify no username apparently the option is ignored. from the manual:

Provide a password to be used for authenticating with the broker. Using this argument without also specifying a username is invalid. This requires a broker that supports MQTT v3.1. See also the --username option.

probably I’ll be better to remove it even though I think it isn’t causing any issues at the moment.