Help with combining two on/off automations into one

I’m fairly new to Home Assistant. I have it set up and running well, and created several simple automations which works. However, I’m not sure how to combine on/off automations for the same device into one automation.

For example, I have a heating automation that triggers when a Sonoff temp/humidity sensor’s temperature value drops below a certain value. It then switches the heating on using a Sonoff ZBMINI. There’s a condition that it will only trigger during the day between set times.
Then I have a separate automation to do the opposite, to turn the heating off when temperature goes above a certain value.
I also have the same two automations for night time, set to trigger only between certain set times.
I would like to combine the two into one and I’m sure it’s perfectly doable, but it doesn’t work for some reason. My implementation is probably not correct.

Here’s my automations. How would I go about combining them into one? Ideally if I can combine the two daytime and two night time automations all into one that would be first prize, but I’m happy to have a daytime and night time automation separately to keep things simpler while I learn more about automations.

Heating ON - Day:

alias: Heating ON - Day
description: ''
trigger:
  - type: temperature
    platform: device
    device_id: 3cf865edfc3fbd08e231b9da73c97d31
    entity_id: sensor.temp_and_humid_sensor_01_temperature
    domain: sensor
    below: 16.5
condition:
  - condition: time
    after: '05:30:00'
    before: '22:30:00'
action:
  - type: turn_on
    device_id: f3a06f130750f78b0d1aa71f27eef33d
    entity_id: light.heating_switch_on_off
    domain: light
mode: single

Heating OFF - Day

alias: Heating OFF - Day
description: ''
trigger:
  - type: temperature
    platform: device
    device_id: 3cf865edfc3fbd08e231b9da73c97d31
    entity_id: sensor.temp_and_humid_sensor_01_temperature
    domain: sensor
    above: 17.5
condition:
  - condition: time
    after: '05:30:00'
    before: '22:30:00'
action:
  - type: turn_off
    device_id: f3a06f130750f78b0d1aa71f27eef33d
    entity_id: light.heating_switch_on_off
    domain: light
mode: single

On mobile so you get the shorthand version.

Create the new automation triggering on the temperature sensor state and no value.

Leave condition blank.

Then in action, select choose. Here is where you now create a new ‘option’ for each of the previous automations. Each option gets a condition or multiple conditions (time, above, below, etc) and the appropriate action.

[edit] story time. I started using HA just like you. Every automation was separate because I didn’t know about choose. Now I have numerous automations with three or four options based on time, presence, state, etc. It’s a great feature.

You’ll want to look at the “Trigger ID” option in the triggers.

You set a “Trigger ID” for “On” or “Off” based on the specific trigger and then in the action use “Choose” and select your appropriate named trigger as the condition for the action.

I’ve merged a few automations from separate on or off automations into a singular automation with this method.

1 Like

Wow that was quick responses, thank you guys! I’ll set up a new automation with your suggestions and report back.
Much appreciated!

Have you considered setting up a generic thermostat? In your configuration.yaml add

#
#Create a generic Thermostat for ??
#Use Temperature sensor located ??
#
climate:
  - platform: generic_thermostat
    name: Heating
    heater: light.heating_switch_on_off
    target_sensor: sensor.temp_and_humid_sensor_01_temperature
    min_temp: 15
    max_temp: 25
    target_temp: 20
    away_temp: 17

You can then set the state to away and it will control the temperature. Lots easier.

See here for further documentation:

If I understand the generic thermostat correctly, in it’s simplest form I don’t need to even add an automation for it, it runs completely from the configuration.yaml settings, right?
So if I want to set certain times for it to run during the day/night I would create two different thermostats, one for day and one for night temperatures, and create an automation them to run them at certain times. Correct?

It would be better to use one thermostat and set with an automation. The away temp is used while your away. If user tracking set up you can use that with an automation to set the thermostat to away. I use my cell phone attaching to my wifi (ubiquity) to say whether we are home or away. With multiple people you just need to create a group and use the group sensor for home or away.

I use an automation to control my thermostats with varying temperatures during the week and weekend. @123 gave a lot of help on this.

- id: 'Main Floor Thermostat Control Heating'
  alias: Main Floor Thermostat Control Heat Control
  description: 'Set the main floor thermostat for Heat'
  trigger:
    - id: 'schedule'
      platform: time
      at:
        - input_datetime.sp1_wk
        - input_datetime.sp2_wk
        - input_datetime.sp3_wk
        - input_datetime.sp4_wk
        - input_datetime.sp5_wk
        - input_datetime.sp1_wknd
        - input_datetime.sp2_wknd
        - input_datetime.sp3_wknd
        - input_datetime.sp4_wknd
        - input_datetime.sp5_wknd
    - id: 'override'
      platform: state
      entity_id: input_boolean.thermostat_main
      to: 'on'
      for: '00:00:30'
    - id: 'disable'
      platform: state
      entity_id: input_boolean.thermostat_main
      to: 'off'
      for: '00:00:30'
  condition: []
  action:
    - choose:
        - conditions:
            - "{{ trigger.id != 'disable' }}"
            - "{{ is_state('input_boolean.thermostat_main', 'on') }}"
          sequence:
            - service: climate.set_temperature
              target:
                entity_id: climate.main_floor
              data:
                temperature: >-
                  {% if now().isoweekday() in [1,2,3,4,5] %}
                    {% set weekday = {state_attr('input_datetime.sp1_wk', 'hour'): states('input_number.t1_wk'),
                                      state_attr('input_datetime.sp2_wk', 'hour'): states('input_number.t2_wk'),
                                      state_attr('input_datetime.sp3_wk', 'hour'): states('input_number.t3_wk'),
                                      state_attr('input_datetime.sp4_wk', 'hour'): states('input_number.t4_wk'),
                                      state_attr('input_datetime.sp5_wk', 'hour'): states('input_number.t5_wk')} %}
                    {{ weekday[now().hour] }}
                  {% else %}
                    {% set weekend = {state_attr('input_datetime.sp1_wknd', 'hour'): states('input_number.t1_wknd'),
                                      state_attr('input_datetime.sp2_wknd', 'hour'): states('input_number.t2_wknd'),
                                      state_attr('input_datetime.sp3_wknd', 'hour'): states('input_number.t3_wknd'),
                                      state_attr('input_datetime.sp4_wknd', 'hour'): states('input_number.t4_wknd'),
                                      state_attr('input_datetime.sp5_wknd', 'hour'): states('input_number.t5_wknd')} %}
                    {{ weekend[now().hour] }}
                  {% endif %}
        - conditions:
            - "{{ trigger.id == 'disable' }}"
          sequence:
            - service: climate.turn_off
              target:
                entity_id: climate.main_floor
              data:
                hvac_mode: 'off'
      default: []
  mode: single

@AllHailJ have you considered the custom scheduler-component/card for the scheduling?

Handy to have the schedule in Lovelace.

Thank you, I’ve set up the thermostat and will look at your automation example next.

I have it in Lovelace but it is not pretty - especially with two thermostats. I will look at the custom scheduler-component/card. Many thanks!

That looks handy indeed, I’ll have a play with it!

Really handy. That makes it too easy. I spent days trying to figure out the automation. Man, was I dumb!!

@AllHailJ - we all started at ground zero with HA. Still finding new things that amaze me every week :slight_smile:

@gerrit.eu

The scheduler is way cool. Here is a screen shot to show how easy.

I seem to get more than I contribute but I’ll keep trying.

Best Regards to all

@AllHailJ you can have it in one schedule

But I could only get 2 set points inside a scheme and I wanted 4

I’ve got a bunch with five. Helps to use a PC as they can be difficult to select using a phone.

I also had 5 or more…without any issues. Example:

Just figured it out. Thanks

So I set up the thermostat, and added the scheduler, very nifty thank you! Only problem is that my heating is not coming on when the temperature drops below the set value (1 degree Celsius below target temp).
The climate card does display the correct temperature sensor values so there’s no problem reading the value. The heating comes on if I switch it on manually through HA, so that works. It just seems like the thermostat is not triggered. I know where to debug automations, but I can’t find where/how to debug the thermostat.
As a test, I removed my scheduler setting and tested the thermostat on its own, but no luck.
Can you spot anything wrong in my config?

climate:
  - platform: generic_thermostat
    name: Heating
    unique_id: climateday01
    heater: light.heating_switch_on_off
    target_sensor: sensor.temp_and_humid_sensor_01_temperature
    min_temp: 15
    max_temp: 25
    ac_mode: false
    target_temp: 17.5
    cold_tolerance: 1
    hot_tolerance: 0
    min_cycle_duration:
      seconds: 60
    initial_hvac_mode: "off"
    away_temp: 15
    precision: 0.1