Just follow up here is my config for lights, switches, sensors, binary sensors, locks and covers. I realized this morning that transitions aren’t working so I’m going to have to modify my lights a bit to get them to work (looks like I’ll be going for the JSON format), but figured this might help someone else. The brightness control works just fine.
Automations for the “master” instance:
- alias: "MQTT Light and Switch Toggle"
trigger:
- platform: mqtt
topic: 'homeassistant/switch/#'
- platform: mqtt
topic: 'homeassistant/light/#'
condition:
condition: template
value_template: "{{ trigger.topic.split('/')[3] == 'set' }}"
action:
- service_template: "{{ trigger.topic.split('/')[1] }}.turn_{{trigger.payload | lower }}"
data_template:
entity_id: "{{ trigger.topic.split('/')[1] }}.{{ trigger.topic.split('/')[2] }}"
- alias: "MQTT Light Brightness"
trigger:
- platform: mqtt
topic: 'homeassistant/light/#'
condition:
- condition: template
value_template: "{{ trigger.topic.split('/')[3] == 'brightness' }}"
- condition: template
value_template: "{{ trigger.topic.split('/')[4] == 'set' }}"
action:
- service: light.turn_on
data_template:
entity_id: "{{ trigger.topic.split('/')[1] }}.{{ trigger.topic.split('/')[2] }}"
brightness: "{{trigger.payload}}"
- alias: "MQTT Cover Toggle"
trigger:
- platform: mqtt
topic: 'homeassistant/cover/#'
condition:
condition: template
value_template: "{{ trigger.topic.split('/')[3] == 'set' }}"
action:
- service_template: "cover.{{trigger.payload | lower }}_cover"
data_template:
entity_id: "{{ trigger.topic.split('/')[1] }}.{{ trigger.topic.split('/')[2] }}"
- alias: "MQTT Lock Toggle"
trigger:
- platform: mqtt
topic: 'homeassistant/lock/#'
condition:
condition: template
value_template: "{{ trigger.topic.split('/')[3] == 'set' }}"
action:
- service_template: "lock.{{trigger.payload | lower }}"
data_template:
entity_id: "{{ trigger.topic.split('/')[1] }}.{{ trigger.topic.split('/')[2] }}"
code: !secret door_code
And these are the automations running on the slave instance (note I have the binary sensor turn off because it was spamming messages. I think I fixed that but I left the automation in as I don’t think it hurts):
- alias: "MQTT Discovery Switch Creator"
trigger:
- platform: mqtt
topic: 'homeassistant/switch/#'
condition:
condition: template
value_template: "{{ trigger.topic.split('/')[3] == 'state' }}"
action:
- service: mqtt.publish
data_template:
topic: "homeassistant/{{ trigger.topic.split('/')[1] }}/{{ trigger.topic.split('/')[2] }}/config"
payload: "{\"name\": \"{{ trigger.topic.split('/')[2]| replace('_', ' ') | title }}\", \"pl_off\":\"off\", \"pl_on\":\"on\", \"stat_t\": \"homeassistant/{{ trigger.topic.split('/')[1] }}/{{ trigger.topic.split('/')[2] }}/state\", \"cmd_t\": \"homeassistant/{{ trigger.topic.split('/')[1] }}/{{ trigger.topic.split('/')[2] }}/set\" }"
retain: true
- alias: "MQTT Discovery Light Creator"
trigger:
- platform: mqtt
topic: 'homeassistant/light/#'
condition:
condition: template
value_template: "{{ trigger.topic.split('/')[3] == 'state' }}"
action:
- service: mqtt.publish
data_template:
topic: "homeassistant/{{ trigger.topic.split('/')[1] }}/{{ trigger.topic.split('/')[2] }}/config"
payload: "{\"name\": \"{{ trigger.topic.split('/')[2]| replace('_', ' ') | title }}\", \"pl_off\":\"off\", \"pl_on\":\"on\", \"cmd_t\": \"homeassistant/{{ trigger.topic.split('/')[1] }}/{{ trigger.topic.split('/')[2] }}/set\", \"stat_t\": \"homeassistant/{{ trigger.topic.split('/')[1] }}/{{ trigger.topic.split('/')[2] }}/state\", \"bri_stat_t\": \"homeassistant/{{ trigger.topic.split('/')[1] }}/{{ trigger.topic.split('/')[2] }}/brightness\", \"bri_cmd_t\": \"homeassistant/{{ trigger.topic.split('/')[1] }}/{{ trigger.topic.split('/')[2] }}/brightness/set\" }"
retain: true
- alias: "MQTT Discovery Binary Sensor Creator"
trigger:
- platform: mqtt
topic: 'homeassistant/binary_sensor/#'
condition:
condition: template
value_template: "{{ trigger.topic.split('/')[3] == 'state' }}"
action:
- service: mqtt.publish
data_template:
topic: "homeassistant/binary_sensor/{{ trigger.topic.split('/')[2] }}/config"
payload: "{\"name\": \"{{ trigger.topic.split('/')[2]| replace('_', ' ') | title }}\", \"pl_off\":\"off\", \"pl_on\":\"on\", \"stat_t\": \"homeassistant/{{ trigger.topic.split('/')[1] }}/{{ trigger.topic.split('/')[2] }}/state\"}"
retain: true
- alias: "HASS Restart Binary Sensor Automation Toggle"
initial_state: 'on'
hide_entity: True
trigger:
platform: homeassistant
event: start
action:
- service: automation.turn_on
entity_id: automation.mqtt_discovery_binary_sensor_creator
- delay: 0:00:10
- service: automation.turn_off
entity_id: automation.mqtt_discovery_binary_sensor_creator
- alias: "MQTT Discovery Sensor Creator"
trigger:
- platform: mqtt
topic: 'homeassistant/sensor/#'
condition:
condition: template
value_template: "{{ trigger.topic.split('/')[3] == 'state' }}"
action:
- service: mqtt.publish
data_template:
topic: "homeassistant/sensor/{{ trigger.topic.split('/')[2] }}/config"
payload: "{\"name\": \"{{ trigger.topic.split('/')[2]| replace('_', ' ') | title }}\", \"state_topic\": \"homeassistant/sensor/{{ trigger.topic.split('/')[2] }}/state\"}"
retain: true