How to reuse this configuration many times?

Hello

I have many RF light switches using RF1 for ON and RF2 for off. So I’d need to copy and paste the following code many times changing only three parameters: name, payload_on and payload_off. Any better way ?

thanks

light:
   - platform: mqtt
     name: "entrance"
     command_topic: cmnd/bridge/rfcode
     availability_topic: "tele/bridge/LWT"
     payload_available: "Online"
     payload_not_available: "Offline"
     payload_on: "#71D3A1"
     payload_off: "#71D3A2"
     optimistic: true
     state_topic: "tele/bridge/RESULT"
     state_value_template: '#{{value_json.RfReceived.Data}}'

Search ‘yaml anchors’

yes, that is what I was trying:

bridgeSwitch: &bridgeSwitch
     platform: mqtt
     command_topic: cmnd/bridge/rfcode
     availability_topic: "tele/bridge/LWT"
     payload_available: "Online"
     payload_not_available: "Offline"
     optimistic: true
     state_topic: "tele/bridge/RESULT"
     state_value_template: '#{{value_json.RfReceived.Data}}'

light:
   - <<: *bridgeSwitch
     name: "entrance"
     payload_on: "#71D3A1"
     payload_off: "#71D3A2"

but I’m getting

Configuration invalidCHECK CONFIG

Component not found: bridgeSwitch

so I’m assuming that HA doesn’t allow objects not supported (?) (bridgeSwitch)

ok, here a “partial solution”… not sure it is the best but it is better than nothing

light:
   - &bridgeSwitch
     platform: mqtt
     command_topic: cmnd/bridge/rfcode
     availability_topic: "tele/bridge/LWT"
     payload_available: "Online"
     payload_not_available: "Offline"
     optimistic: true
     retain: true
     state_topic: "tele/bridge/RESULT"
     state_value_template: '#{{value_json.RfReceived.Data}}'
     name: "entrance"
     payload_on: "#71D3A1"
     payload_off: "#71D3A2"
   - <<: *bridgeSwitch
     name: "porch"
     payload_on: "#71D3A4"
     payload_off: "#71D3A8"

I was just basically typing out exactly that. Not sure why you think it’s a partial solution, it does exactly what you want.

1 Like

True, it works… i said “partial” as I was trying to put the common configurations in some other file to keep things more tidy . For example , the light part of the mqtt could be used also in the switch file …