Using one switch device to trigger another via MQTT

Hello,

Yes another one brand new to Home Assistant, but a semi seasoned user of OpenHAB converting over. I must say it was extremely easy to get started with and even get running with Alexa, which is one of the reasons for the conversion. That being said setting up the automation has proven to be frustrating. I basically need to start out with a single switch, a Sonoff updated with Tasmota controlled via MQTT, being able to parallel it’s status with another.

I have spent the better part of a day trying different configurations, reading articles and looking at examples to fall short. The switches all work fine independently with the UI and Alexa just need to start combining some for the effect.

A general example of on the things I tried would be:

automation:
  alias: 'Rear stairwell light on'
  trigger:
    platform: state
    entity_id: switch.basement
    state: 'on'
  action:
    service: switch.turn_on
    entity_id: switch.back_stair_light

This is just to give an idea of what I am trying to accomplish, though with this particular code I get a state error. Yet another example that is okay (Valid) with the check configuration is:

automation:
  alias: 'Rear stairwell light on'
  trigger:
    platform: mqtt
    topic: stat/sonoff5/POWER
    payload: 'on'
  action:
    service: switch.turn_on
    entity_id: switch.back_stair_light

Any insight as to the correct configuration would be greatly appreciated.

Thanks!

So you need 2 MQTT switches to mirror states? Why not just subscribe to the same topic?

Past that you could have 1, when switched on, publish a MQTT message turning the other on, and so on.

1 Like

Hi and welcome to HA.

Please note that all code snippets etc should be formatted properly so we can see indentation etc. (YAML is a mixed blessing!). See the panel at the top of the page.

1 Like

Well because that would be just too simple. LOL. I had just added the two over time and with OH2 and it was simple to do. Never even thought of doing that way but I will now.

Thanks

Thanks. I didn’t have any issues with the text formatting. It just didn’t transfer over to here when I pasted to this request. Thank you though.

Yeah it just makes it easier for others to read and diagnose. I hope you enjoy HA as much as I do.

1 Like

Okay. Now I remember why I didn’t end up subscribing to the same topic, because if I publish to MQTT to activate the main switch the secondary, which is in a remote location,it will follow. But if the main switch is manually turned off/on the secondary does not follow suit. Never could figure that one out and basically it wasn’t previously necessary because in OH2 you can establish rules (aka automation) as follows

rule "Back Stair light"
	when
    	Item BasementLights changed // to motion detected
    then
    	if (BasementLights.state.toString == "ON") {
    	   BackStair.sendCommand (ON)
		}
		if (BasementLights.state.toString == "OFF"){
			BackStair.sendCommand (OFF)
        }

I will add a couple of the example codes below, with the proper indentation, that I unsuccessfully tried just for a general reference in case someone has a solution. Or maybe someone knows why paralleling the topics via MQTT is not consistent. Would be happy with either solution.

automation:
  alias: 'Rear stairwell light on'
  trigger:
    platform: mqtt
    topic: stat/sonoff5/POWER
    payload: 'on'
  action:
    service: switch.turn_on
    entity_id: switch.back_stair_light

OR

automation:
  alias: ‘Rear stairwell light on’
  trigger:
    platform: state
    entity_id: switch.basement
    state: ‘on’
  action:
    service: switch.turn_on
    entity_id: switch.back_stair_light

Thanks again.

I think GROUPTOPIC is what you are after.

Sonoffs with the same GROUPTOPIC will react to the same MQTT command.

At the web command console of both devices issue the following:

GroupTopic yourgroupname

Then when you publish to this topic both devices will be controlled.

I could be wrong- I usually am…
But Tasmota allows you to define rules. Here is an example of a rule that lets a Sonoff switch mirror another Sonoff.

This rule is on the Sonoff for my patio light. When I turn it on or off, it publishes an MQTT message that the garden light responds to.

rule1 on Power1#state=1 do publish cmnd/Garden/POWER on endon on Power1#state=0 do publish cmnd/Garden/POWER off endon
rule1 1

In my view that detracts from home assistant having control of all your entities, and being able to trace/debug everything through one system.

1 Like

I want to thank you all for sharing. I was able to finally find the magic combo that works, it was just a matter of learning the way HA codes. This is mainly for anyone in the future that may come across the same issue.

automation:
  alias: 'Rear stairwell light on'
  trigger:
    platform: state
    entity_id: switch.basement
    from: 'off'
    to: 'on'
  action:
    service: switch.turn_on
    entity_id: switch.back_stair_light

automation 2:
  alias: 'Rear stairwell light off'
  trigger:
    platform: state
    entity_id: switch.basement
    from: 'on'
    to: 'off'
  action:
    service: switch.turn_off
    entity_id: switch.back_stair_light

I am still not sure if or how Boolean works in HA, so that perhaps I can make this into a single automation, but this works perfectly fine whether I use the HA UI or manually trip the switch.

Thanks again to all for your input.

1 Like

You are exactly correct. The whole point is to centralize as much as possible. If a switch would go bad for example I could pop a new one in an only have to change one item under “switch” in the configuration to match the name of the switch in the topic. True it’s not much easier than going to the switches UI and changing the topic but I like and want everything simply together as much as possible.

the one thing i noticed in your second automation in your last code example you posted above is that you have non-ascii (aka “fancy”) quote characters.

It’s a pretty common error during copy/paste and will totally screw things up since they aren’t real obvious in a standard text editor when they are there until you post them in the forum as you did.

And since you ultimately went with the second snippet with good quotes then it’s safe to say that was likely your problem all along.

You shouldn’t have to put a ‘from:’ line in your code to get it to work. unless there is some other possible state that it could be… but typically a binary is either on or off.

There are ways to get it into one automation but it would need templating an action service call to do it.

as an example I think this should work:

  alias: 'Rear stairwell light follow'
  trigger:
    platform: state
    entity_id: switch.basement
  action:
    - service_template: >
      {% if is_state('switch.basement', 'on') %}
        switch.turn_on
      {% else is_state('switch.basement', 'off') %}
        switch.turn_off
      {% endif %}
      entity_id: switch.back_stair_light
1 Like

This is how I tackled the same problem for the two plugs that power my XMas lights:

- alias: XMas Lights Mirror
#  initial_state: "off"
  trigger:
    - platform: state
      entity_id: switch.sonoff_s31_01, switch.sonoff_s31_02
      from: 'on'
      to: 'off'
    - platform: state
      entity_id: switch.sonoff_s31_01, switch.sonoff_s31_02
      from: 'off'
      to: 'on'
  action:
    service_template: >
      {% if trigger.to_state.state == "on" %}
      switch.turn_on
      {% elif trigger.to_state.state == "off" %}
      switch.turn_off
      {% endif %}
    data_template: 
      entity_id: >
        {% if trigger.from_state.entity_id == "switch.sonoff_s31_01" %}
        switch.sonoff_s31_02
        {% elif trigger.from_state.entity_id == "switch.sonoff_s31_02" %}
        switch.sonoff_s31_01
        {% endif %}

It doesn’t matter which switch I toggle manually or in the UI, the other one will follow.
The code could be shortened, but this way it’s easy to understand how it works.

2 Likes