Template Switch allow turn on only if off and reverse

Hi, I’ve automated an electric fireplace using two ESPHome devices, one uses an ir blaster to issue commands to the fireplace, and another is a Sonoff S31 smart switch that is used to see if the heater is on or not. In Home Assistant I have the fireplace show up as switch using the template switch in my configuration.yaml that allows me to control the fireplace like a switch and the on/off status is based on a template that checks if the heater is on.

The issue I’m having is that the action to turn on or off the fireplace is the same IR command so if we use Alexa to turn off the main floor lights, she sends a “turn off” command to everything on the main floor and that turns the fireplace back on. What I would like to do is disable the “turn_on” action if the fireplace is on, and disable the “turn_off” if the fireplace is on but I can’t find it online.

Here is the code from my configuration.yaml that defines the fireplace switch. sensor.fireplace_power is the Sonoff S31 and button.fireplace_remote_fireplace_power_button is the IR blaster.

I think I can do this by making two scripts, one for fireplace on and one for fireplace off but I was hoping for a simpler way. VS code keeps telling me that if or condition statements aren’t allowed in a template switch.

  - platform: template
        value_template: "{{ states('sensor.fireplace_power ')|float > 1000 }}"
        friendly_name: Fireplace
        icon_template: >-
          {% if states('sensor.fireplace_power ')|float > 1000}%}
          {% else %}
          {% endif %}
        unique_id: "redacted"
          service: button.press
            entity_id: button.fireplace_remote_fireplace_power_button
          service: button.press
            entity_id: button.fireplace_remote_fireplace_power_button

turn_on section is a script. Simply add a condition

        - condition: state
          entity_id: switch.fireplace
          state: 'off'
        - service: button.press
            entity_id: button.fireplace_remote_fireplace_power_button
1 Like

Thank you! I knew there was a simple way to do this.