I did not use the automation trigger, why? Because I sometimes get echoed signals and also because in my case the code quickly became a spaghetti code
In my case, I created an entity via YAML representing my devices. In this case, all events and device statuses are handled by the internal HA stuff.
Triggers (if needed) manage that special entity just like all other entities.
Anyway, this is an example of my sensor (in my case it is a Pir that sends the message when someone triggers it)
mqtt:
binary_sensor:
- unique_id: pir.15484294
name: "Pir.Mansarda"
device_class: motion
state_topic: "home/+/433toMQTT/15484294"
value_template: "{{ value_json.value }}"
payload_on: "15484294"
off_delay: 3
In a somewhat broader context, e.g. sending messages (my LED strip remote control), I emulated the discovery of the remote control with simple automation that creates it. So once again I use the HA Entity on HA like the others.
Here is the code that emulates the creation of the discovery:
automation:
- id: '166887267157423009'
alias: 00 - Configure MQTT Led Sala
description: Questa automazione Riconfigura il led_sala
trigger:
- platform: homeassistant
event: start
condition: []
action:
- service: mqtt.publish
data:
topic: homeassistant/button/led_sala/caldo/config
payload: "{\n \"automation_type\": \"button\",\n \"unique_id\": 23091979215300,\n
\ \"name\": \"Caldo\",\n \"command_topic\": \"home/OMG_BTRF_OMG1/commands/MQTTto433\",\n
\ \"payload_press\": \"{\\\"value\\\":215300,\\\"protocol\\\":1,\\\"delay\\\":388}\",\n
\ \"device\": {\n \"identifiers\": [\n \"Lampadario_led_sala\"\n ],\n
\ \"name\": \"led_sala\",\n \"sw_version\": \"0.0.0\",\n \"model\":
\"Led RF control\",\n \"manufacturer\": \"Staniscia\"\n }\n}"
retain: true
qos: '0'
- service: mqtt.publish
data:
topic: homeassistant/button/led_sala/freddo/config
payload: "{\n \"automation_type\": \"button\",\n \"unique_id\": 23091979215302,\n
\ \"name\": \"Freddo\",\n \"command_topic\": \"home/OMG_BTRF_OMG1/commands/MQTTto433\",\n
\ \"payload_press\": \"{\\\"value\\\":215302,\\\"protocol\\\":1,\\\"delay\\\":388}\",\n
\ \"device\": {\n \"identifiers\": [\n \"Lampadario_led_sala\"\n ],\n
\ \"name\": \"led_sala\",\n \"sw_version\": \"0.0.0\",\n \"model\":
\"Led RF control\",\n \"manufacturer\": \"Staniscia\"\n }\n}"
retain: true
qos: '0'
- service: mqtt.publish
data:
topic: homeassistant/button/led_sala/up/config
payload: "{\n \"automation_type\": \"button\",\n \"unique_id\": 23091979215299,\n
\ \"name\": \"Up\",\n \"command_topic\": \"home/OMG_BTRF_OMG1/commands/MQTTto433\",\n
\ \"payload_press\": \"{\\\"value\\\":215299,\\\"protocol\\\":1,\\\"delay\\\":388}\",\n
\ \"device\": {\n \"identifiers\": [\n \"Lampadario_led_sala\"\n ],\n
\ \"name\": \"led_sala\",\n \"sw_version\": \"0.0.0\",\n \"model\":
\"Led RF control\",\n \"manufacturer\": \"Staniscia\"\n }\n}"
retain: true
qos: '0'
- service: mqtt.publish
data:
topic: homeassistant/button/led_sala/down/config
payload: "{\n \"automation_type\": \"button\",\n \"unique_id\": 23091979215303,\n
\ \"name\": \"Down\",\n \"command_topic\": \"home/OMG_BTRF_OMG1/commands/MQTTto433\",\n
\ \"payload_press\": \"{\\\"value\\\":215303,\\\"protocol\\\":1,\\\"delay\\\":388}\",\n
\ \"device\": {\n \"identifiers\": [\n \"Lampadario_led_sala\"\n ],\n
\ \"name\": \"led_sala\",\n \"sw_version\": \"0.0.0\",\n \"model\":
\"Led RF control\",\n \"manufacturer\": \"Staniscia\"\n }\n}"
retain: true
qos: '0'
- service: mqtt.publish
data:
topic: homeassistant/button/led_sala/soffuso/config
payload: "{\n \"automation_type\": \"button\",\n \"unique_id\": 23091979215306,\n
\ \"name\": \"Soffuso\",\n \"command_topic\": \"home/OMG_BTRF_OMG1/commands/MQTTto433\",\n
\ \"payload_press\": \"{\\\"value\\\":215306,\\\"protocol\\\":1,\\\"delay\\\":388}\",\n
\ \"device\": {\n \"identifiers\": [\n \"Lampadario_led_sala\"\n ],\n
\ \"name\": \"led_sala\",\n \"sw_version\": \"0.0.0\",\n \"model\":
\"Led RF control\",\n \"manufacturer\": \"Staniscia\"\n }\n}"
retain: true
qos: '0'
- service: mqtt.publish
data:
topic: homeassistant/button/led_sala/max_caldo/config
payload: "{\n \"automation_type\": \"button\",\n \"unique_id\": 23091979215307,\n
\ \"name\": \"Caldo Massimo\",\n \"command_topic\": \"home/OMG_BTRF_OMG1/commands/MQTTto433\",\n
\ \"payload_press\": \"{\\\"value\\\":215307,\\\"protocol\\\":1,\\\"delay\\\":388}\",\n
\ \"device\": {\n \"identifiers\": [\n \"Lampadario_led_sala\"\n ],\n
\ \"name\": \"led_sala\",\n \"sw_version\": \"0.0.0\",\n \"model\":
\"Led RF control\",\n \"manufacturer\": \"Staniscia\"\n }\n}"
retain: true
qos: '0'
- service: mqtt.publish
data:
topic: homeassistant/button/led_sala/max_freddo/config
payload: "{\n \"automation_type\": \"button\",\n \"unique_id\": 23091979215308,\n
\ \"name\": \"Freddo Massimo\",\n \"command_topic\": \"home/OMG_BTRF_OMG1/commands/MQTTto433\",\n
\ \"payload_press\": \"{\\\"value\\\":215308,\\\"protocol\\\":1,\\\"delay\\\":388}\",\n
\ \"device\": {\n \"identifiers\": [\n \"Lampadario_led_sala\"\n ],\n
\ \"name\": \"led_sala\",\n \"sw_version\": \"0.0.0\",\n \"model\":
\"Led RF control\",\n \"manufacturer\": \"Staniscia\"\n }\n}"
retain: true
qos: '0'
mode: single
maybe there is a smarter way to do it, but I end up using this one and it feels good and solid