If else for simple minds

Hi everyone, I apologise already as this must be easy for someone who knows but going through the docs and samples I can’t figure this one out as examples never deal with switches…

I have this ‘requirement’ based on 2 switches called A and B triggered by a toggle request:

If A is on -> Switch B OFF
               -> Switch A OFF
Else      -> Switch A ON

Using this in script.yaml of course doesn’t work but just to give an idea of where I would like to go ?

action:
  - service: homeassistant.toggle
    data_template:
      entity_id: >
        switch.A
        switch.B
         {% if (is_state("switch.A", "on")) -%}
           switch.A.turn_off
      	   switch.B.turn_off
         {%- else -%}
      	   switch.A.turn_on
         {%- endif %}

Any hint appreciated, thank you !

Sound like:

  • if A is on, switch A & B off
  • else do nothing

This is easily doable with a template, but I’m not going to explain it yet. Reason being that in your pseudo code you’ve put the service ‘homeassistant.toggle’, which makes me think that you’re either not explaining your requirements correctly or you’re not applying the basics of your requirements correctly and therefore aren’t going to understand the solution.

Please explain exactly what you’re trying to do and/or how it fits with toggling the devices, as the two are currently mutually exclusive :man_shrugging:

Thank you very much for your first reponses. Let me explain further:
A is a switch that powers on/off a multiple socket where B is plugged in.
When A is powered off, I need B to be powered off first so that it doesn’t power on again when A is powered back on (B remembers its last state by default).
This all must be done via a single action calling toggle switch on A.
Hence my request:

Action toggle A:
If A is on -> Switch B OFF
               -> Switch A OFF
Else        -> Switch A ON

This is what’s confusing though. You keep saying ‘toggle’. If you ‘toggle’ A then you’re already controlling A, and if B is plugged into A then ‘toggling’ A is already ‘toggling’ B.

It is impossible to toggle B before A, if the circumstance you’re reacting to is A being toggled unless you have a time machine.

So like earlier when you gave us the code snippet, what would your trigger be for that?

English is not my native language so that also doesn’t help either of us.
These 2 switches are exposed as Philips hue lights to a Harmony hub, I press a button on my harmony remote to act on A.
Since it’s a simple remote button I have no control over on or off for A, so I use ‘toggle’.
Now since A powers the full extension lead where B is plugged in, when A goes off, so does B.
My issue is I don’t want B to power on the next time A is switched on…and the only way I can think of solving this is to poweroff B before A.
I hope it helps understand ?

It still leaves us with the problem of not owning a time machine unfortunately.

If you press a button ‘X’ (physical or otherwise) that turns off A, we cannot go back in time and make B switch off first.

What we need is a button ‘Y’ we can press that initiates an automation only, without affecting A in any way.

Then we simply never press X and use Y as the trigger for the automation, then in the action of the automation you simply say “if A is on, turn off B, wait 5 seconds, turn off A. Or if A is off, turn on A”

Which is easily done so long as the trigger is Y, not X or A.

Right, now I get what you mean, my bad.
Actually via Harmony the trigger is a script exposed as a light on hue, so this would be the Y you’re looking for.
No time machine needed !
So:

Action trigger script Y:
If A is on -> Switch B OFF
               -> Switch A OFF
Else        -> Switch A ON

Ah, excellent.

In that case, using a choose statement…

script:
  script_y:
    sequence:
      - choose:
          # Switch A is on.... 
          - conditions:
              - condition: state
                entity_id: switch.A
                state: 'on' 
             sequence:
               - service: switch.turn_off
                 entity_id: switch.B
               - delay:
                   seconds: 5
               - service: switch.turn_off
                 entity_id: switch.A
        # Switch A is off 
        default:
          - service: switch.turn_on
            entity_id: switch.A
1 Like

Sir thank you so much for taking the time to understand my issue.
I will test this hopefully today and report, but I’m quite sure things will go smooth, thanks again !

1 Like

Just a quick note, I’ve done that on my phone so the indentation might not quite be right, but if you’re using either the UI script editor or a decent text editor you can line it up properly.

It looks right from here but it’s hard to tell on my phone screen :+1:

In your sequence you’re switching switch.B off twice, I assume the second one after the delay should be switch.A

1 Like

Well spotted!

Edited to correct that, cheers :+1:

1 Like

That worked perfectly !
I had to review the ident as expected and I managed to do this all from the UI but just for reference:

sequence:
  - choose:
    - conditions:
      - condition: state
        entity_id: switch.A
        state: 'on'
      sequence:
      - service: switch.turn_off
        data: {}
        entity_id: switch.B
      - service: switch.turn_off
        data: {}
        entity_id: switch.A
    default:
    - service: switch.turn_on
      data: {}
      entity_id: switch.A

I ended up removing the delay and plug the switch on another lead as it wasn’t remembering its last state correctly (depending on how much time it took to update the Tuya cloud I guess)
Since I don’t expect to do on and off at a rapid interval, that suits just fine.
I learned something, so :smile:

Thanks again to everyone (and Marc specially) who took the time to read and help me !

1 Like