Switches and automations

Hi guys, I’ve got a question:
I have a water pump that is connected to a switch and a boiler with another switch, and it already works very well with HA. And the washing machine is connected to a sonoff switch via MQTT with a timer: basically when the washing machine finishes the cycle, a timer turn off the washing machine and the pump get off, too.
Now I installed a new switch (xiaomi switch) in the bathroom thats turn on and off the water pump and boiler together, but when I turn off from switch (xiaomi) , if the washing machine is running and not receive water, it locks.
how can fix that if I press the button (xiaomi) and the washing machine is switched on, it doesn’t turn off the water pump but only the boiler?
Here my present automation and script …

- id: Timerstart
  alias: Avvio timer lavatrice
  trigger:
  - entity_id: switch.lavatrice
    from: 'off'
    platform: state
    to: 'on'
  action:
  - entity_id: timer.lavatrice
    service: timer.start
  - data: {}
    service: script.lavatrice_start
- id: Timerstop
  alias: Lavaggio panni end
  trigger:
  - event_data:
      entity_id: timer.lavatrice
    event_type: timer.finished
    platform: event
  - entity_id: switch.lavatrice
    from: 'on'
    platform: state
    to: 'off'
  action:
  - data:
      message: Lavaggio panni terminato!
    service: script.lavatrice_end

and the script:

    lavatrice_start:
      alias: script-lavatrice-start
      sequence:
      - data:
          entity_id: switch.acqua_acqua_on
        service: switch.turn_on
      - data:
          brightness: '3'
          color_name: forestgreen
          entity_id: light.gateway_light_34ce00837b14
        service: light.turn_on
    lavatrice_end:
      alias: script-lavatrice-end
      sequence:
      - data:
          entity_id: switch.acqua_acqua_on
        service: switch.turn_off
      - data:
          gw_mac: 34:CE:00:83:7B:14
          ringtone_id: 11
          ringtone_vol: 2
        service: xiaomi_aqara.play_ringtone
      - data:
          message: Lavaggio panni terminato!
        service: persistent_notification.create
      - data:
          entity_id: switch.lavatrice
        service: switch.turn_off
      - service: timer.cancel
      - data:
          entity_id: light.gateway_light_34ce00837b14
        service: light.turn_off

Could you please format you code properly - take a look at the very top of the page.
It’s much easier to spot issues then.

Done! Thank you

Not sure I fully understand the situation.

You have one switch (switch.acqua_acqua_on) that turns on/off both, the boiler and the pump, but when your script (lavatrice_start) is triggered it only turns on one of the two?
If you want one to be turned on and the other to be turned off, wouldn’t you need two switches for that?

No, i have 4 switches…

  • water pump
  • washing machine (sonoff)
  • boiler
  • xiaomi switch
    what I want to do I was:
    when i press the xiaomi switch get boiler and water pump on or off, but if washing machine running get only boiler off.
    Thank you

Sorry, i still don’t fully understand what you’re trying to achieve.

But here is an automation I use to automatically keep two switches in sync, maybe that helps:

- alias: Mirror Switch Status
  trigger:
    - platform: state
      entity_id: switch.sonoff_s31_01, switch.sonoff_s31_02
      from: 'on'
      to: 'off'
    - platform: state
      entity_id: switch.sonoff_s31_01, switch.sonoff_s31_02
      from: 'off'
      to: 'on'
  action:
    service_template: >
      {% if trigger.to_state.state == "on" %}
      switch.turn_on
      {% elif trigger.to_state.state == "off" %}
      switch.turn_off
      {% endif %}
    data_template: 
      entity_id: >
        {% if trigger.from_state.entity_id == "switch.sonoff_s31_01" %}
        switch.sonoff_s31_02
        {% elif trigger.from_state.entity_id == "switch.sonoff_s31_02" %}
        switch.sonoff_s31_01
        {% endif %}

I’m pretty sure it can be done with less lines of code, but I haven’t tackled that yet.

*fewer :stuck_out_tongue_winking_eye:

- alias: Mirror Switch Status
  trigger:
    platform: state
    entity_id:
      - switch.sonoff_s31_01
      - switch.sonoff_s31_02
  action:
    service_template: "switch.turn_{{ trigger.to_state.state }}" 
    data_template: 
      entity_id: >
        {% if trigger.from_state.entity_id == "switch.sonoff_s31_01" %} switch.sonoff_s31_02
        {% else %} switch.sonoff_s31_01 {% endif %}

Thanks @anon43302295 - I’ll give it a try.

I like the ‘action’ part of the script.

I kept the trigger part as explicit as it is because I want to make sure that I don’t trigger on status changes from or to ‘unavailable’. I have a few ‘smart’ devices that now and then drop off the WiFi for a few seconds and this would then result in ‘ghost triggers’.
As I’m writing this, though, I’m thinking that this could probably be caught with a condition, though.

1 Like

In fairness, the way I’ve just done it might cause an endless loop of itself, would certainly need testing. If so you’ll need a condition to prevent a second firing.

And as you say, if the states could be ‘unknown’ then a condition to knock that one away would be preferable. It was more of a pointer to bring it down to half of the size than a ‘this will work’ - but it can definitely be shrunk significantly :slight_smile:

1 Like

Appreciate it!

This is most always an iterative process for me.

Is started out with 4 automations, 1 for each case.
Reduced it to 2.
And then to 1 :+1:

I knew there is some more ‘air’ in it and I’ll definitely try & test the action part.

2 Likes

you could also put the 2 switches in a group.sonoff_switches, and have the action changed into:

action:
    service_template: >
      homeassisant.turn_{{ trigger.to_state.state }}
    data: 
      entity_id: group.sonoff_switches

if one switch is on, switching the group to on switches all entities in that group to on, even if the group was already on. Seems a bit strange, but I use that in my motion sensors groups to good effect.

1 Like

Thx @Mariusthvdb - I thought about using a group but wasn’t sure that it would work!