Automating the sharing of sensors and switches between HA's using MQTT Discovery and Statestream

Thanks! I didn’t see this but after messing around for most of the day I figured it out. Exhausted but I’ll post my config at some point. Much appreciated for the starting guide though, was extremely helpful!

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

I’ve hit a wall and maybe you can help @RobDYI. Can you check out MQTT Light/Templating Help . I’m not trying to auto-generate the lights (i may try to do that eventually but i just wanted to get things working) but I can’t seem to get transitions and brightness to work.

could anyone confirm what config entries theyve put on the slave and what on the master - I sort of get it working but then my master instance goes nuts and creates hundreds of entries for each slave entity and almost kills the machine, e.g.

‘switch.kerui_k16_homearm_chime_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2’, ‘switch.kerui_k16_homearm_chime_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2’, ‘switch.kerui_k16_homearm_chime_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2’, ‘switch.kerui_k16_homearm_chime_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2’, ‘switch.kerui_k16_homearm_chime_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2’, ‘switch.kerui_k16_homearm_chime_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2’, ‘switch.kerui_k16_homearm_chime_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2’, ‘switch.kerui_k16_homearm_chime_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2’, ‘switch.kerui_k16_homearm_chime_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2’, ‘switch.kerui_k16_homearm_chime_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2_2’,

I think the problem might be that you have discovery on, on the master. (the HA that provides the sensors / switches to the slave) If you don’t want the master to populate any sensors / switches from mqtt config, then set discovery to false or set the discovery prefix to something different than the slave.

mqtt:
  broker: 192.168.2.180
  client_id: 1212
  discovery: true
  discovery_prefix: rpi3

Thanks for replying so quickly @RobDYI
Maybe I’m doing this back to front. I have sensors and switches on the slave that I want to be visible and able to be switched on/off from the master.
I don’t want the slave to see any entities that exist on the master.
I have no mqtt config on the master, as I’m using this broker https://www.home-assistant.io/addons/mosquitto/ and I’ve set the integration not to add new devices:

If you can tell me what I’m doing wrong it would be much appreciated! Cheers

On the slave,

mqtt:
  broker: 192.168.2.180
  client_id: 1212
mqtt_statestream:
  base_topic: rpi1
  publish_attributes: true
  publish_timestamps: true
  include:
    domains:
      - binary_sensor
      - sensor
      - switch
      - light

all the automations above to create configs and control the entities

on the master

mqtt:
  broker: 192.168.2.180
  client_id: 1212
  discovery: true
  discovery_prefix: rpi1

you can go to the configuration menu on the front end to delete all the entities that are repeated or erase your storage folder if you can to start fresh.

thanks for the info!
Do i need a broker running on the slave as well as the master?

No, in fact they need to be log into the same one broker. That broker can be on either the slave or master.

I also found it didn’t add all the entities at restart.

If you send to MQTT payloads with the retain flag set then then the entities won’t disappear on restart :slight_smile:

To save processing power, I shut off the automated config creator once I have all the devices setup. It hasn’t been on for months.

Can you share the latest version of this automation? Do you also have it shared on Github? Because I’m not sure if I use pieces of it or complete.
For me trying to use a switch function on the master doesn’t work. It is loading correctly the values from slave to master.

And do you have the brightness function now also included?

Have you looked at this project?

Is there no way in which the MQTT discovery messages are automatically generated ? I want to deploy a ‘remote’ zwave network on a rpi. All entities can be published to the master HA, and should be controllable. I’d hate to modify the config everytime I add a new device

What kind of information does your zwave device reveal about itself?

For example, assume I just added a new device. It reports nothing about itself other than its name and current state. Guess what I added.

That’s the challenge of what you are proposing: to publish a discovery payload, containing information about the device, to a specific discovery topic for a device that reveals next to nothing about itself.

On the other hand, if the device reveals a great deal about itself, then that data can be used to generate discovery information. The challenge then becomes detecting new devices, selecting appropriate data and reformatting it for use with Home Assistant’s MQTT Discovery.

Well if it is a switch, than it is quite trivial? Light is indeed more difficiult

How would you tell the difference if all it reports about itself is ‘xyz987’ is ‘on’?

That’s why I asked how much information does the device reveal about itself. The more the better.

For those interested, I’ve created a blueprint equivalent: MQTT State Stream to MQTT Discovery

Hi all,

I want to turn on my Hall lights when the door opens after sunset.
and when the door is closed again to turn off the lights after 3 minutes.
The automation won’t allow me the for: " 00:03:00" statement.
What am i doing wrong?

- alias: Turn on hall light when door opens
  trigger:
    platform: mqtt
    topic: shellies/shellydw2-front/sensor/state
    payload: "open"
  condition:
    condition: state
    entity_id: sun.sun
    state: below_horizon
  action:
    service: light.turn_on
    entity_id: light.hal
  id: 83a95c0f089849b2b4701024949c10ed
- alias: Turn off Hall light after 3 minutes
  trigger:
    platform: mqtt
    topic: shellies/shellydw2-front/sensor/state
    payload: "close"
    for: "00:03:00"
  action:
    service: light.turn_off
    entity_id: light.hal
  id: 9d3a15d53db045a28c122e30c7b01008

Cheers,
Olav

Hi
I want to change preset_mode. but my code is not working

- id: '1002'
  initial_state: 'on'
  alias: mqtt_command_fan_speed
  trigger:
    - platform: mqtt
      topic: 'homeassistant/fan/#/speed'
  condition:
    condition: template
    value_template:  "{{ trigger.topic.split('/')[4] == 'set' }}"
  action:
    - service: fan.set_preset_mode
      data_template:
        entity_id: "{{ trigger.topic.split('/')[1] }}.{{ trigger.topic.split('/')[2] }}"
        preset_mode: >
          {{trigger.payload }}   

what should i do
thx