Help Needed with Combining Automations - Thermostat Automation

I have a couple of automations, one switches on the heating at specific times and the other switches it off if no one is home. I need to somehow combine them, so that if the heating was not triggered, as we were not home at the time, then it is triggered when we get home, if it is within the time it is supposed to be on. Here’s the relevant section of configuration.yaml

  - id: '1573685276228'
  alias: Thermostat on - Evening
  description: ''
  trigger:
  - at: '17:30'
    platform: time
  condition: []
  action:
  - service: climate.turn_on
- id: '1573685325172'
  alias: Thermostat off - Evening
  description: ''
  trigger:
  - at: '23:00'
    platform: time
  condition: []
  action:
  - service: climate.turn_off
- id: '1574506627626'
  alias: Not Home Thermostat
  description: ''
  trigger:
  - entity_id: device_tracker.life360_me
    from: home
    platform: state
  condition:
  - condition: and
    conditions:
    - condition: template
      value_template: '{{ not is_state(''device_tracker.life360_wife'', ''home'')  }}'
  action:
  - service: climate.turn_off

Just add a tracker trigger to your on automation. So they turn on when you get home.

@tom_l, I had thought of that, but I only want it to come on if it is within the time set by the timers. So in this example, I only want the tracker to switch on the heating if it’s between 17:30 and 23:00. I guess I can add a condition to every timer occurrence, but there are a number of them throughout the day so ideally I would like a global one.

I guess I can add a condition to every timer occurrence

Yep. That’s what they’re for.

You can add all the triggers to one automation and one set of conditions. e.g.

  alias: Thermostat on
  trigger:
  - platform: time
    at: '06:30'
  - platform: time
    at: '12:30'
  - platform: time
    at: '17:30'
  - platform: state
    entity_id: device_tracker.life360_me
    to: 'home'
  condition:
  - condition: state
    entity_id: device_tracker.life360_me
    state: 'home'
  - condition: or
    conditions:
    - condtion: time
      after: '06:30:00'
      before: '10:00:00'
    - condtion: time
      after: '12:30:00'
      before: '14:00:00'
    - condtion: time
      after: '17:00:00'
      before: '23:00:00'
  action:
  - service: climate.turn_on
    entity_id: ?

Thanks again @tom_l. Can I combine or and and in a condition? For example, I have the following code to check whether either of us are in the house:

trigger:
  - entity_id: device_tracker.life360_me
    from: home
    platform: state
  condition:
  - condition: and
    conditions:
    - condition: template
      value_template: '{{ not is_state(''device_tracker.life360_wife'', ''home'')  }}'

so would this look like:

  alias: Thermostat on
  trigger:
  - platform: time
    at: '06:30'
  - platform: time
    at: '12:30'
  - platform: time
    at: '17:30'
  - platform: state
    entity_id: device_tracker.life360_me
    to: 'home'
  condition:
  - condition: state
    entity_id: device_tracker.life360_me
    state: 'home'
  - condition: and
    conditions:
    - condition: template
      value_template: '{{ not is_state(''device_tracker.life360_wife'', ''home'')  }}'
  - condition: or
    conditions:
    - condtion: time
      after: '06:30:00'
      before: '10:00:00'
    - condtion: time
      after: '12:30:00'
      before: '14:00:00'
    - condtion: time
      after: '17:00:00'
      before: '23:00:00'
  action:
  - service: climate.turn_on
    entity_id: ?

I also notice that you have put a ? next to entity id for climate. This is missing in my configuration as I only have one climate entry. Does this matter?

Last one first:

I only have one climate entry. Does this matter?

Yes and no. If the service can’t find a valid entity id it will act on all the entities in the climate domain. In your case, just the one. It’s better if you do supply the entity rather than relying on a default fall-back like this. It’s unlikely but this behaviour could change in a future release and that could cause you an issue.

Now for the other question,

There are a couple of ways to do it. An automation is an interesting solution. I would have just put both trackers in a group. The group will be home if either of you are home.

Another way to do it is:

alias: Thermostat on
  trigger:
  - platform: time
    at: '06:30:00'
  - platform: time
    at: '12:30:00'
  - platform: time
    at: '17:30:00'
  - platform: state
    entity_id: device_tracker.life360_me
    to: 'home'
  - platform: state
    entity_id: device_tracker.life360_wife
    to: 'home'
  condition:
  - condition: or
    conditions:
    - condition: state
      entity_id: device_tracker.life360_me
      state: 'home'
    - condition: state
      entity_id: device_tracker.life360_wife
      state: 'home'
  - condition: or
    conditions:
    - condtion: time
      after: '06:30:00'
      before: '10:00:00'
    - condtion: time
      after: '12:30:00'
      before: '14:00:00'
    - condtion: time
      after: '17:00:00'
      before: '23:00:00'
  action:
  - service: climate.turn_on
    entity_id: ?

This will turn the heating on at the beginning of the specified times if either of you are home, or if you are both away and one of you comes home during the specified heating times.

1 Like

I will do some reading up on how to add trackers to groups, as I have a number of trackers and it would be handy to have groups.

Here: https://www.home-assistant.io/integrations/group/#group-behavior

And the ‘awesome_people’ example up above on that page.

2 Likes

Thanks, it turns out that I already use groups to my climate control, where I group several sensors to create an average. I just hadn’t realised that this could also be done with trackers. I’ve clearly got a lot to learn, but only been at this for a couple of weeks and have no previous experience of yaml. I must say that the community here is awesome, providing great support.

1 Like

It would appear that adding groups breaks a lot of other stuff. When I add this to groups.yaml:

group:
  family:
    name: Family
    entities:
      - device_tracker.life360_me
      - device_tracker.life360_wife
      - device_tracker.life360_kid1
      - device_tracker.life360_kid2
  parents:
    name: Parents
    entities:
      - device_tracker.life360_me
      - device_tracker.life360_wife
  kids:
    name: Kids
    entities:
      - device_tracker.life360_kid1
      - device_tracker.life360_kid2

I get this in notifications:

Invalid config
The following components and platforms could not be set up:

  • group

  • switch

  • script

  • automation

  • device-tracker

  • default-config

  • generic_thermostat.climate

Please check your config.

Remove this from your groups.yaml:

group:

Then adjust your indentation so that the group names are against the margin, maintaining the indention of the items in the groups.

The way !include works is that it includes the contents of the file so this:

groups: !include groups.yaml

With the contents of the file you have gives this as a result once the include has be parsed:

group:
  group:  # <-- bad!
    family:
      name: Family
      entities:
        - device_tracker.life360_me
        - device_tracker.life360_wife
        - etc...
2 Likes

@tom_l is correct and I have to say ‘Well Spotted !’ (not sure I’ve got there anytime this side of next year)

BUT …
if you ever move to packages, then this will change again.
Not something you need to worry about now but I’m mentioning it so as if it crops up you may remember what the reason could be.

Edit: thinking about it you may not have an issue then either. I’m just weird as everything I have is in one package or another, no other files than ui-lovelace.yaml and a 25 line configuration.yaml

So in the above example, where it will turn the heating on at the beginning of the specified times if either of us are home, or if we are both away and one of us comes home during the specified heating times, if all conditions are met, the service is set to service: climate.turn_on.

If the conditions are met and the heating comes on, does this not need another automation to set service to climate.turn_off after the end times, i.e. after 10:00, 14:00 and 23:00?

Yes it does.

Can it be combined into the same automation, or would it require a separate one?

A separate one would be best. Something like this:

alias: Thermostat off
  trigger:
  - platform: time
    at: '10:00:00'
  - platform: time
    at: '14:00:00'
  - platform: time
    at: '23:00:00'
  - platform: state
    entity_id: group.trackers
    to: 'not_home'
  action:
  - service: climate.turn_off
    entity_id: ?

No conditions required!

1 Like
  - platform: state
    entity_id: group.trackers
    to: 'not_home'

Why is this required?

It isn’t if you are happy that the heating stays on whilst you are out (say for the evening or on holiday ? )

1 Like

Requirements from your original post:

I have a couple of automations, one switches on the heating at specific times and the other switches it off if no one is home

2 Likes

Ah, I see now. I’d got a little confused with the logic. You are perfectly correct and it is one of my requirements. Thanks.