It just depends on what you are trying to achieve. If you are using sensors that only have a single state (eg. PIR) then the above code gives you a way of sending the MQTT payloads to individual topics.
Eg.
- platform: mqtt
name: 'Hallway PIR 1'
state_topic: 'rf/bridge/101112'
<<: &common_pir
off_delay: 5
device_class: motion
- platform: mqtt
name: "Hallway PIR 2"
state_topic: 'rf/bridge/456789'
<<: *common_pir
- platform: mqtt
name: "Landing PIR 1"
state_topic: 'rf/bridge/123456'
<<: *common_pir
The dump: rc_switch
line is merely for debugging to see the RF code in the Esphome log console. I turn it on when I need to sniff for a new 433Mhz device to see what the code is, then turn it off to avoid the device needlessly logging.
I took a slightly different approach though as I needed to support multi-state devices (eg. doors) and I didn’t want to have to reload Hassio every time I added a sensor to configuration.yaml
So instead, I used MQTT discovery with NodeRed and a number of modified Sonoff T2 light switches acting as 433MHz bridges.
Node Red Flow
This only needs to be updated and run when you add a new sensor.
[{"id":"9993cb8d.82e7c8","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"f3b79420.a8ffe8","type":"mqtt out","z":"9993cb8d.82e7c8","name":"","topic":"","qos":"","retain":"","broker":"2ec7481f.cfe868","x":1070,"y":300,"wires":[]},{"id":"39715738.293628","type":"function","z":"9993cb8d.82e7c8","name":"","func":"function hexa(num) { return parseInt(num, 2).toString(10) }\nfunction nameToId(name) { return name.replace(/ /g,\"_\").toLowerCase(); }\n \nfunction templateDevice(device) {\n obj = {};\n \n id = nameToId(device.name);\n obj.payload = {\n name: device.name,\n state_topic: 'rf/bridge/all',\n };\n obj.topic = 'homeassistant/binary_sensor/' + id + '/config';\n \n switch (device.type) {\n case 'pir':\n case 'vibration':\n obj.payload.off_delay = 5;\n obj.payload.device_class = 'motion';\n obj.payload.payload_on = hexa(device.on); \n break;\n case 'door':\n case 'opening':\n obj.payload.payload_on = hexa(device.open);\n obj.payload.payload_off = hexa(device.close);\n obj.payload.device_class = 'door';\n break;\n default:\n obj = false;\n }\n \n return obj;\n}\n\n// Process entities\nfor (index = msg.payload.length - 1; index >= 0; --index) {\n msg.payload[index] = templateDevice(msg.payload[index]);\n}\n\nreturn msg;","outputs":1,"noerr":0,"x":590,"y":300,"wires":[["9f0dc2fb.11f02"]]},{"id":"db249a23.e95418","type":"inject","z":"9993cb8d.82e7c8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":240,"y":300,"wires":[["16dced2d.c73ef3"]]},{"id":"9f0dc2fb.11f02","type":"split","z":"9993cb8d.82e7c8","name":"","splt":"\\n","spltType":"str","arraySplt":"1","arraySpltType":"len","stream":false,"addname":"","x":750,"y":300,"wires":[["a4d3a9d.be33a58"]]},{"id":"a4d3a9d.be33a58","type":"function","z":"9993cb8d.82e7c8","name":"","func":"return msg.payload;","outputs":1,"noerr":0,"x":910,"y":300,"wires":[["f3b79420.a8ffe8"]]},{"id":"8afe9a3f.f15358","type":"comment","z":"9993cb8d.82e7c8","name":"RF MQTT","info":"","x":240,"y":260,"wires":[]},{"id":"16dced2d.c73ef3","type":"template","z":"9993cb8d.82e7c8","name":"Sensors","field":"payload","fieldType":"msg","format":"json","syntax":"plain","template":"[\n {\n \"on\": \"0000000111110100\",\n \"name\": \"Hallway PIR 1\",\n \"type\": \"pir\"\n },\n {\n \"open\": \"0000000111110101\",\n \"close\": \"0000000111110111\",\n \"name\": \"Boiler Room Door\",\n \"type\": \"door\"\n } \n]","output":"json","x":420,"y":300,"wires":[["39715738.293628"]]},{"id":"2ec7481f.cfe868","type":"mqtt-broker","z":"","name":"MQTT-Server","broker":"192.168.1.1","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]
Esphome
mqtt:
broker: 10.0.0.2
username: myuser
password: mypassword
id: mqtt_client
discovery: false
remote_receiver:
pin: 4
# dump: rc_switch
filter: 100us
tolerance: 50%
idle: 2ms
on_rc_switch:
- lambda: !lambda |-
char code[21];
sprintf(code, "%llu", x.code);
id(mqtt_client).publish("rf/bridge/all", code);
With this, it only needs NodeRed to create the sensor, Hassio does the rest after that.