Endless loop controlling fan from both IR controller and Home Assistant

This is the current situation with a template fan created on ESPhome.

I want to control the from both the Home Assistant UI and the IR remote controller. In trying to achieve this I now got an endless loop:

Pulse the fan direction button on the IR controller THEN:

  1. The physical fan efectively changes direction (this is OK)
  2. The fan direction on HA is updated/synchronised:
remote_receiver:
 pin:
   number: GPIO15 #D15
   inverted: true
 dump: nec  ##el log mostrará sólo los códigos de NEC que aparezcan
 on_nec: 
      - if:
          condition:
           - lambda: return (x.command==0xF807); # get IR code
          then: 
           - fan.turn_on:
               id: ventilador_techo_dormitorio
               direction: !lambda |-
                 if (id(ventilador_techo_dormitorio).direction == esphome::fan::FanDirection::FORWARD) {
                   return esphome::fan::FanDirection::REVERSE;
                 } else {
                   return esphome::fan::FanDirection::FORWARD;
                 }
           - switch.turn_on: switch_ventilador_techo_dormitorio

This code turn on a switch that stores the on/off state of the fan and update the direction of the fan on Home Assistant BUT

When the direction is changed on Home Assistant…

fan:
  - platform: template
    name: "Ventilador techo dormitorio"
    id: ventilador_techo_dormitorio
    speed_count: 6
    has_direction: true
    restore_mode: ALWAYS_OFF
[....]
    on_direction_set:
      - remote_transmitter.transmit_nec:
          address: 0x7F80
          command: 0xF807      

… the fan sends the IR code (this is necessary to control the fan from HA) and efectiveley changes AGAIN the direction…

And because of the IR code sent I have an endless loop.

Any idea on how to break this loop?

You have few options. Hardware solution would be to power off the receiver when transmitter is sending.
SW solution is to make a flag (template switch or global variable boolean).
On transmitter code set the flag >> send the signal >> unset the flag.
On receiver code put a condition to test if the flag is set.

That flag is the option I am trying right now (a template switch). I’m still testing.

Thank you!!

Add a delay before you unset the flag.

Sure. It is a switch that turns off after 500 ms.

1 Like

It is working! :slight_smile:

Congratulations. :+1: