Syncing two MQTT topics

Hello,
I have two MQTT topics already working correctly on external Mosquitto. They can be also read properly by MQTT devise on HASS.
I would like to synchronize them permanently in HASS.

First is

topic: 'shellies/shellyswitch-134CA7/relay/1'

and second is

topic: 'smartthings/Bathroom Secondary Light/switch'

Any idea of how to synchronize them?

I tried to put below in automation.yaml but I found two problems:

1.Even the one side automation like below doesn’t work
2.I guess putting reverse automation (topic 2 as trigger and trigeer 1 as action) will cause infinite loop of sending MQTT message between this two topics.

- alias: Example
  initial_state: True
  trigger:
    - platform: mqtt
      topic: 'shellies/shellyswitch-134CA7/relay/1'
  action:
    - service: mqtt.publish
      data_template:
        topic: 'smartthings/Bathroom Secondary Light/switch'
        payload_template: '{{ trigger.payload }}'

I use this to make sure switch.cabinets_b follows the state of switch.cabinets_a, maybe it can help you

- alias: Cabinets sync
  trigger:
  - platform: state
    entity_id: switch.cabinets_a
  condition:
  - condition: template
    value_template: '{{ trigger.to_state.state != "Unavailable" }}'
  - condition: template
    value_template: '{{ states("switch.cabinets_b") != trigger.to_state.state }}'
  action:
    service: switch.toggle
    entity_id: switch.cabinets_b

@reef-actor thank you for idea, but this is also one way sync. Is it possible to add additional reverse automation cabinet_b as condition and cabinet_a as action? Wouldn’t cause to toggle devices infinitely ?

Yes, it should work if you add a copy of it with the switches the other way around. The condition value_template: '{{ states("switch.cabinets_b") != trigger.to_state.state }}' prevents the other switch from being toggled if it is already in the same state so there will be no inifnite looping.

this is what i would do…

when you created your bathroom light entity and your shelly switch entity you got two entity id’s - one light (that we talked about in the other thread) and one switch that I don’t know what you called it so you’ll have to figure that part out.

then create an automation something like this:

alias: sync devices
trigger:
  - platform: state
    entity_id: light.bathroom_secondary_light
  - platform: state
    entity_id: switch.whatever_yours_is
action:
  - service_template: >
    {% if trigger.to_state.state == 'on' %}
      homeassistant.turn_on
    {% else %}
      homeassistant.turn_off
    {% endif %}
    entity_id:
      - light.bathroom_secondary_light
      - switch.whatever_yours_is

I haven’t tested it but I think it will do what you want.

1 Like

Thank you @finity. Additional two spaces before {% and homeassistant.turn and it works perfectly. So finally it is:

- alias: Sync Bathroom Light
  trigger:
    - platform: state
      entity_id: light.bathroom_light
    - platform: state
      entity_id: light.bathroom_light_st
  action:
    - service_template: >
        {% if trigger.to_state.state == 'on' %}
          homeassistant.turn_on
        {% else %}
          homeassistant.turn_off
        {% endif %}
      entity_id:
        - light.bathroom_light
        - light.bathroom_light_st