Manually clicking on one switch to turn off another switch

Hi,

I’m having some troubles making my idea come true.

I have two switches, one for the heater and one for the cooler. Normally, those are controlled automatically via the third device that publishes MQTT messages to enable/disable them. Then, their states are changed via state_topic that the Home Assistant listens to.

What I want to do is to disable that third device via a switch (the one that controls them automatically) if I manually click on the switch that controls the heater or the cooler.

I tried creating an automation that call switch.turn_off on that third switch when one of the first two changes states, but that is also triggered when the state is updated via state_topic.

Is there a way to differentiate manual interaction from MQTT update?

Nope! There’s no simple way to tell why or how something changed state. You could check automation trigger times and guess as to the timing of things…but these are not changed via automation, rather via mqtt state changes.

I’m assuming you have those as 2 mqtt switches?

You could create a template switch for them. A template switch simply combines multiple switches/sensors/etc. It does not disable the functionality of the mqtt switch. Then, put only these template switches on your lovelace page, and only use these template switches when you want to disable the auto thing. There’s no need to have both, the template switch will still show the correct state for you.

Example (make sure you change the entity_ids that I made up to real ones):

First, we’ll need a script because the turn_on and turn_off actions have to be a single thing (but we want to do multiple things)

script:
  climate_override:
    sequence:
      # First, turn on/off the automatic override based on the action. 
      # We should do the inverse of the action since it should be in relation
      # to the heater/cooler.
      - service_template: "switch.turn_{{'on' if action == 'off' else 'off' }}"
        entity_id: switch.the_third_switch_id
      # Now toggle the mqtt switch. This will use the real mqtt switch command and 
      # send the right cmnd topic. The template switch state will reflect this update.
      - service_template: "switch.turn_{{action}}"
        entity_id: "{{ climate_id }}"
switch:
  - platform: template
    switches:
      heater_override:
        # Follows the state of the real mqtt switch
        value_template: "{{ is_state('switch.heater_id', 'on') }}"
        # Does a custom turn_on that disables the automatic switch.
        turn_on:
          service: script.climate_override
          data:
            action: "on"
            climate_id: "switch.heater_id"
        turn_off:
          service: script.climate_override
          data:
            action: "off"
            climate_id: "switch.heater_id"
      cooler_override:
        # Follows the state of the real mqtt switch
        value_template: "{{ is_state('switch.cooler_id', 'on') }}"
        # Does a custom turn_on that disables the automatic switch.
        turn_on:
          service: script.climate_override
          data:
            action: "on"
            climate_id: switch.cooler_id
        turn_off:
          service: script.climate_override
          data:
            action: "off"
            climate_id: switch.cooler_id

Now you will have 2 new switches. switch.heater_override, and switch.cooler_override. If anything turns on one of these switches, the 3rd switch will turn off. Once one of these switches turns off, the override switch will go back to on.

I didn’t cover a condition of you turn on both switches manually, then turning one off. I hope that isn’t something that happens. If you do it, it will turn on the 3rd switch even though one of them might still be ‘off’.

IF you still want to turn on/off the mqtt switches but leave automation, you simply use those mqtt switches.