Template switch: only send "on/off" if not ALREADY on/off (e.g. avoid toggle)

I have an IR-controlled fan that I’m trying to automate.

The fan only supports a “toggle” command, so I have an IR transmitter in ESPHome that sends the same IR command for both “on” and “off”. This works, but the switch state in HA keeps getting out of sync with the real state.

Instead of a “toggle”, I want to implement discrete “on” and “off” commands that detect the current state of the fan and only trigger an IR transmission if the state needs to actually change.

I have a sensor driven by a power monitoring outlet that tells me if the fan is on or off based upon its power draw, so I know the current state of the fan.

I have set up a Template Switch for the fan using the state of that sensor, but it is still sending “on” and “off” commands to the ESPHome switch, which is still sending a “toggle” command.

What I want to do is configure a condition on one of these switches so that if they get sent a “turn on” command, but that sensor says its already “on”, then nothing actually happens.

Is that possible? image

I guess I could probably set up a dummy switch and hook up some Automations with conditional actions on them; is that the best way?

Please don’t post pictures of text. Post the correctly formatted text. That way I can easily edit it to show you what to do.

You can add as many actions as you want in the turn_on and turn_off actions, including conditions.

e.g. (I had to type all this out rather than much easier copy/paste)

turn_on:
  - condition: state
    entity_id: binary_sensor.office_white_fan_on
    state: 'off'
  - service: switch.turn_on
    target:
      entity_id: switch.office_white_fan_ir_controller_fan_power
turn_off:
  - condition: state
    entity_id: binary_sensor.office_white_fan_on
    state: 'on'
  - service: switch.turn_off
    target:
      entity_id: switch.office_white_fan_ir_controller_fan_power

One question, why are you using IR control if you have a power monitoring switch in-line?

Why not just control that switch?

Sorry for the image, I should have posted both. (I wanted to highlight the image to make it super clear what I was trying to accomplish)

I am using IR to change the fan state because if I just power cycle the smart plug, the fan doesn’t remember it’s last setting. It always defaults to “0” (no air blowing). The smart plug can tell me if it’s drawing power, and can effectively shut it down, but it can’t start it back up.

And also, because I wanted to see if I could :wink:

Thanks for the help!