Condensing automations

I have a few automations that work perfectly however i was wondering if was possible to condense them to two automations.

I have one that turns on the hot water and another which turns on the heating, they are just simple sonoff basic switches. At the moment i have two for the heating (one to turn on and one to turn off. For the hot water i have it turning on (and off) twice a day, so at TWO different times in the day. For this i have 4 different automations.

- id: turnheatingonmainhouse
  alias: 'Main House Heating on'
  trigger:
    - platform: time
      at: "19:30:00"
  action:
    service: switch.turn_on
    entity_id: switch.main_house_general_heating
      
- id: turnheatingoffmainhouse
  alias: 'Main House Heating off'
  trigger:
     - platform: time
       at: "09:30:00"
  action:
    service: switch.turn_off
    entity_id: switch.main_house_general_heating
    
- id: turnwateronmainhouseday
  alias: 'Main House Water on DAY'
  trigger:
    - platform: time
      at: "14:00:00"
  action:
    service: switch.turn_on
    entity_id: switch.main_house_hot_water
      
- id: turnwateroffmainhouseday
  alias: 'Main House Water off DAY'
  trigger:
     - platform: time
       at: "17:00:00"
  action:
    service: switch.turn_off
    entity_id: switch.main_house_hot_water
    
- id: turnwateronmainhousenight
  alias: 'Main House Water on NIGHT'
  trigger:
    - platform: time
      at: "00:00:00"
  action:
    service: switch.turn_on
    entity_id: switch.main_house_hot_water
      
- id: turnwateroffmainhousenight
  alias: 'Main House Water off NIGHT'
  trigger:
     - platform: time
       at: "06:00:00"
  action:
    service: switch.turn_off
    entity_id: switch.main_house_hot_water

Is it possible to condense these 6 automations to just two: one for the heating and one for the water?

Kind regards

- trigger:
  - platform: time
    at: '09:30:00'
  - platform: time
    at: '19:30:00'
  action:
  - service_template: >
      switch.turn_{{ 'off' if trigger.now.hour == 9 else 'on' }}
    entity_id: switch.main_house_general_heating

- trigger:
  - platform: time
    at: "00:00:00"
  - platform: time
    at: "06:00:00"
  - platform: time
    at: "14:00:00"
  - platform: time
    at: "17:00:00"
  - service_template: >
      switch.turn_{{ 'off' if trigger.now.hour in [6, 17] else 'on' }}
    entity_id: switch.main_house_hot_water
1 Like

many thanks!

1 Like