Creating a virtual switch to track the state and control another switch (inching relay)

I’ve setup a Sonoff relay (stock firmware) that is operating in inching mode to control and turn on/off an air conditioner. .
The Sonoff switch is operating correctly, acting as a push-button switch, so it turns on for a second, then switches back off. Unfortunately, I don’t know what state the air con is in, whether it is operating or if it’s off.

What I want to do is create a virtual switch that tracks the current state, so that it operates like a more traditional on/off toggle switch, and ideally control it via HA and Google Home. I don’t have any other sensor (yet) to determine the current state.

I’m very new to Home Assistant, but from what I’ve read I think I need to create an input boolean and also create a template switch.

# AC State Boolean
input_boolean:
  ac_state:
    name: Current AC State
    icon: mdi:air-conditioner
    
# AC Virtual Switch
switch:
  - platform: template
    switches:
      ac_switch:
        friendly_name: Air Conditioner
        value_template: >-
          {% if is_state('input_boolean.ac_state', 'on') %}
          'on'
          {% elif is_state('input_boolean.ac_state', 'off') %}
          'off'
          {% else %}
          'unavailable'
          {% endif %}
        turn_on:
          service: switch.turn_on
          data:
            entity_id: switch.sonoff_1000df4b41_4
        turn_off:
          service: switch.turn_on
          data:
            entity_id: switch.sonoff_1000df4b41_4

The config above is turning the air con on and off, but still the state is not being maintained. It’s behaving similar to the sonoff switch in HA and turn off shortly after I turn it on.

I’m sure this is simple but I have no idea what I might be missing.

Hi there… I have some doubts. What is the exact sonoff device model?

I don’t think it’s an official sonoff device, rather one that works with eWeLink and appears as a sonoff switch when I added it to Home Assistant (via AlexIT/SonoffLan).

The operation of this switch when using the eWeLink App or via Home Assistant is the same. It’s doing what it’s supposed to be doing, which is momentarily turning on then off (to activate the relay and replicate a button push) .

The actual state of this switch is irrelevant as it will always turn off shortly after I turn it on, but how do I create my own state and that is tied to a virtual switch. I hope that makes sense.

In normal condition, if you have used sonofflan custom integration, for this device there will be 4 switch entities, one for each relay and we can control all these relay with this switch entity itself. There wont be any entity created for the push button. please check if it is like this. Also when you press the push button which relay is turned on.

I think the only thing missing from your code is to change it to read

        turn_on:
          service: switch.turn_on
          data:
            entity_id: switch.sonoff_1000df4b41_4
          service: switch.turn_on
          data:
            entity_id: input_boolean.ac_state
        turn_off:
          service: switch.turn_on
          data:
            entity_id: switch.sonoff_1000df4b41_4
          service: switch.turn_off
          data:
            entity_id: input_boolean.ac_state

So ac_state actually reflects whether AC is on or off.

Thanks so much @qoheleth.

I just made some slight adjustments (I was definitely overthinking it, I’m sure it could be simplified even further :wink:)

# AC State Boolean
input_boolean:
  ac_state:
    name: Current AC State
    icon: mdi:air-conditioner
    
# AC Virtual Switch
switch:
  - platform: template
    switches:
      ac_switch:
        friendly_name: Air Conditioner
        value_template: "{{ is_state('input_boolean.ac_state', 'on') }}"
        turn_on:
          - service: input_boolean.turn_on
            data:
             entity_id: input_boolean.ac_state
          - service: switch.turn_on
            data:
             entity_id: switch.sonoff_1000df4b41_4
            
        turn_off:
          - service: input_boolean.turn_off
            data:
             entity_id: input_boolean.ac_state
          - service: switch.turn_on
            data:
             entity_id: switch.sonoff_1000df4b41_4