Mix 4 automation into 1

Hello Guys, I’m new to home assistant and I need some help.
I’ve created 4 automations, but is there any way to mix them into 1, or if it’s not possible mix them into 2.

1st automation called “Fürdés (Otthon)”
2nd automation called “Fürdés (Munka)”
3rd automation called "Hajmosás (Otthon)
4th automation called "Hajmosás (Munka)

So the best would be to mix these 4 into 1, or if it’s not possible mix "Fürdés (Otthon) and Fürdés (Munka) into 1 called “Fürdés”, and "Hajmosás (Otthon) and Hajmosás (Munka) into 1 called “Hajmosás”

Here are my automations:

1st “Fürdés (Otthon)”

- id: '1603527524327'
  alias: Fürdés (Otthon)
  description: Mivel itthon vagyok ezért reggel bekapcsolja a bojlert
  trigger:
  - platform: state
    entity_id: calendar.furdes
    to: 'on'
  condition:
  - condition: state
    entity_id: person.kulcsar_aniko
    state: home
  action:
  - type: turn_on
    device_id: 3ead97ef12eb11eb92ddf31d451c055e
    entity_id: switch.80820452bcddc226a269
    domain: switch
  - delay: 02:30:00
  - type: turn_off
    device_id: 3ead97ef12eb11eb92ddf31d451c055e
    entity_id: switch.80820452bcddc226a269
    domain: switch
  mode: single

2nd "Fürdés (Munka)

- id: '1604571179937'
  alias: Fürdés (Munka)
  description: Mivel dolgozok ezért később kapcsolja be a bojlert (11:00-13:30)
  trigger:
  - platform: state
    entity_id: calendar.furdes
    to: 'on'
  condition:
  - condition: state
    entity_id: person.kulcsar_aniko
    state: not home
  action:
  - delay: 04:00:00
  - type: turn_on
    device_id: 3ead97ef12eb11eb92ddf31d451c055e
    entity_id: switch.80820452bcddc226a269
    domain: switch
  - delay: 02:30:00
  - type: turn_off
    device_id: 3ead97ef12eb11eb92ddf31d451c055e
    entity_id: switch.80820452bcddc226a269
    domain: switch
  mode: single

3rd "Hajmosás (Otthon)

- id: '1603527704795'
  alias: Hajmosás (Otthon)
  description: Mivel itthon vagyok ezért reggel bekapcsolja a bojlert
  trigger:
  - platform: state
    entity_id: calendar.hajmosas
    to: 'on'
  condition:
  - condition: state
    entity_id: person.kulcsar_aniko
    state: home
  action:
  - type: turn_on
    device_id: 3ead97ef12eb11eb92ddf31d451c055e
    entity_id: switch.80820452bcddc226a269
    domain: switch
  - delay: 03:30:00
  - type: turn_off
    device_id: 3ead97ef12eb11eb92ddf31d451c055e
    entity_id: switch.80820452bcddc226a269
    domain: switch
  mode: single

4th "Hajmosás (Munka)

- id: '1604571385122'
  alias: Hajmosás (Munka)
  description: Mivel dolgozok ezért később kapcsolja be a bojlert (11:00-14:30)
  trigger:
  - platform: state
    entity_id: calendar.hajmosas
    to: 'on'
  condition:
  - condition: state
    entity_id: person.kulcsar_aniko
    state: not home
  action:
  - delay: 04:00:00
  - type: turn_on
    device_id: 3ead97ef12eb11eb92ddf31d451c055e
    entity_id: switch.80820452bcddc226a269
    domain: switch
  - delay: 03:30:00
  - type: turn_off
    device_id: 3ead97ef12eb11eb92ddf31d451c055e
    entity_id: switch.80820452bcddc226a269
    domain: switch
  mode: single

The only difference between the actions in the first two automations is that if kulcsar_aniko is not_home there’s an initial 4-hour delay before the switch is turned on.

The only difference between the second pair of automations and the first pair is that they are triggered by a different calendar (furdes vs hajmosas) and there’s a longer delay between turning the switch on and off (2:30 vs 3:30).

This single automation combines all four automations:

- alias: Example 1
  trigger:
  - platform: state
    entity_id: calendar.furdes
    to: 'on'
  - platform: state
    entity_id: calendar.hajmosas
    to: 'on'
  action:
  - choose:
    - conditions: "{{ is_state('person.kulcsar_aniko', 'not_home') }}"
      sequence:
      - delay: 04:00:00
  - service: switch.turn_on
    entity_id: switch.80820452bcddc226a269
  - delay: "{{ '02:30:00' if trigger.to_state.object_id == 'furdes' else '03:30:00' }}"
  - service: switch.turn_off
    entity_id: switch.80820452bcddc226a269

Your automations use long values for delay (hours). You should know that if Home Assistant is restarted during the delay period, the countdown is lost. For example, let’s say the 4-hour delay is counting down and currently has 3 hours left. If Home Assistant restarts at this point, it will not resume counting down the remaining 3 hours (so the switch will never be turned on). You should keep this in mind should your automation ever fail to run the switch on or off.


EDIT

Correction. Replace objectid with object_id in the delay template.

1 Like

Edit - Taras beat me :slight_smile: But I’ll still post what I have so far

You will need to get good with templates.

I can get you started.

First, we want to combine all of our triggers. You can have a list of as many different triggers as you want in any automation. They don’t have to be related at all, though it helps later on when trying to parse the info from them if they are.

- alias: Fürdés (Otthon)
  description: Mivel itthon vagyok ezért reggel bekapcsolja a bojlert
  trigger:
  # Trigger if either of these calendars turn on
  - platform: state
    entity_id: calendar.furdes
    to: 'on'
  - platform: state
    entity_id: calendar.hajmosas
    to: 'on'

Now we have 2 possible triggers that can happen. So lets look at the conditions. We need to determine which condition to evaluate depending on which trigger happened.

Looking at your automations, it is not clear what you are trying to do. My guess is if you’re home, turn on the boiler for 2h30m. If you aren’t home, it waits exactly 4 hours, then do it. The 4 hour delay seems like it could use some improvement. I’ll give an example using a wait_template to improve this.

We no longer want to abort the entire automation depending on conditions. Instead, each condition will affect the actions. So leave conditions as empty.

condition: []
action:
  # Waits up to 4 hours before continuing. 
  # But, if you come home before this 4 hour timer, it will start right away.
  - wait_template: "{{ is_state('person.kulcsar_aniko', 'home') }}"
    timeout: "04:00:00"
    continue_on_timeout: true
  - service: switch.turn_on
    entity_id: switch.80820452bcddc226a269
  - delay: "{{ '02:30:00' if trigger.entity_id == 'calendar.furdes' else '03:30:00' }}"
  - service: switch.turn_off
    entity_id: switch.80820452bcddc226a269

1 Like

Thank you guys, I appreciate it.
By the way, what I wanted to do is to make the water heater to make enough hot water to take a bath (fürdés) or for hairwashing too(Hajmosás), so for “Fürdés” the water heater turns on for 2 hours 30 minutes, and for “Hajmosás” it turns on for 3 hours 30 minutes. The “Fürdés” and “Hajmosás” events both starts at 07:00 and “Fürdés” ends at 09:30 and “Hajmosás” ends at 10:30.
Which is why I thought about location, because if I go to work it shouldn’t turn on at 07:00, because I’m not home, that’s why I give the delay 4 hours so the water heater will turn on at 11:00 and by the time I arrive home about 15:00, there will be hot water.

Hello, I tried your code and on november 6th I was home and in my calendar the “Hajmosás” event triggered. “Hajmosás (Otthon)”
The bojler turned on at 07:00 and turned off at 10:30, so it worked perfectly.
But today I’m home and in my calendar the “Fürdés” event triggered. “Fürdés (Otthon)”
The bojler turned on at 07:00, and it should turn off after 2 hours 30 minutes, but it hasn’t turned off at 09:30 and I don’t know why.

There’s a small error in the example I had posted. I have already corrected it (see above).

In the delay template, the word objectid was missing an underscore.
Change it to object_id and it should work correctly.

Yesterday the 4th automation has also worked perfectly so 4 out 4 works flawslessly, :slight_smile: thank you for your help

1 Like