Simplifying Automations in Home Assistant



Originally posted 2018-04-21 13:56:11.

kodi runs on a raspberry pi

Automating my Coffee Maker

This article is about simplifying my automations in Home Assistant. Before I did this, I had 8 different automations controlling my coffee maker. (first-world problems right). These did the following:

  1. Weekdays turn coffee maker on at 7am
  2. Weekends turn coffee maker on at 8am
  3. Weekdays turn coffee maker off at 2pm
  4. Every night turn coffee maker on at 7pm
  5. Every night turn coffee maker off at 10pm
  6. Every 4th Sunday turn coffee maker on at 7:15am
  7. Every 4th Sunday turn coffee maker off at 8:30am
  8. Every 4th Sunday turn coffee maker on at 12pm

Not that I really need to explain myself, but my coffee maker works best if it has 30 minutes warm up time before using it and it’s a good practice to turn it off when not in use. (I’ve had to replace the main board once already because electronics and heat are not friends and it could be better insulated I guess) Weekends we get up later and can be a bit more random about when we want a coffee. I usually turn it off (voice command to Google of course) when we don’t need anymore coffee and I always have a coffee at night. (is 4 double espresso’s a day bad?)

So I had 8 automations which was a pain if we were going out for the day as I would scroll through the list of automations turning them off so they didn’t trigger if we were going to be out. * in total, 5 on and 3 off. I have reduced that to 1 on and one off now.

My automations for the coffee maker now look like this:

- id: 'coffee-on'
  alias: Coffee Maker On
  trigger:
    - platform: time
      at: '07:00:00'
    - platform: time
      at: '08:00:00'
    - platform: time
      at: '07:15:00'
    - platform: time
      at: '12:00:00'
    - platform: time
      at: '19:00:00'
  condition:
    condition: or
    conditions:
      - condition: template
        value_template: "{{ now().strftime('%H:%M') == '07:00' and now().weekday() < 5 }}"
      - condition: template 
        value_template: "{{ now().strftime('%H:%M') == '08:00' and now().weekday() >= 5 }}"
      - condition: template
        value_template: "{{ now().strftime('%H:%M') == '07:15' and ((as_timestamp(now()) - as_timestamp('2017-09-24 00:00:00')) / 86400)|int % 28 == 0 }}"
      - condition: template
        value_template: "{{ now().strftime('%H:%M') == '12:00' and ((as_timestamp(now()) - as_timestamp('2017-09-24 00:00:00')) / 86400)|int % 28 == 0 }}"
      - condition: template
        value_template: "{{ now().strftime('%H:%M') == '19:00' }}"
  action:
    - service: switch.turn_on
      data:
        entity_id: switch.coffee_maker
- id: 'coffee-off'
  alias: Coffee Maker Off
  trigger:
    - platform: time
      at: '08:30:00'
    - platform: time
      at: '14:00:00'
    - platform: time
      at: '22:00:00'
  condition:
    condition: or
    conditions:
      - condition: template
        value_template: "{{ now().strftime('%H:%M') == '08:30' and ((as_timestamp(now()) - as_timestamp('2017-09-24 00:00:00')) / 86400)|int % 28 == 0 }}"
      - condition: template
        value_template: "{{ now().strftime('%H:%M') == '14:00' and now().weekday() < 5 }}"
      - condition: template
        value_template: "{{ now().strftime('%H:%M') == '22:00' }}"
  action:
    - service: switch.turn_off
      data:
        entity_id: switch.coffee_maker

 

So the way Home Assistant works… any trigger will trigger the action IF the condition is met. You can have multiple conditions as I do and you can have it so that all conditions must be met (and) or any one condition must be met (or) which is what I have above.

As an example, the first condition in the ‘off’ template above first checks if the time is 8:30 and the second part takes the time now and subtracts the midnight 24/09/2017 (first date to count from)and divides it by 86400 (the number of seconds in a day) to work out how many days have elapsed then does a mod 28 (28 days is 4 weeks) and checks if the integer portion = 0 (if true it’s a 4th Sunday) So if it’s 8:30 on a 4th Sunday the automation is triggered.

Pretty cool and I only need to disable one automation if we are going out. Nice.

  
  

Automations for Lights

In our lounge room we have some lamps that we use when we are in that room in the late afternoon/evening.

I have determined by trial and error that if the sun elevation is lower than 25° then we need the lights on in there. During summer, this happens anywhere from 6PM to 7:30PM, but in Winter, they were coming on at 3:30 when no one is in that room anyway.

So I came up with the following with a little help from some friends.

- action:
  - data:
      brightness_pct: 75
      entity_id: light.lounge
      kelvin: 3100
    service: light.turn_on
  alias: Turn Lounge Light On Elevation < 25° & after 4PM
  condition:
  - after: '16:00:00'
    before: '22:00:00'
    condition: time
  - condition: template
    value_template: '{{states.sun.sun.attributes.elevation < 25}}'
  id: '1505977024304'
  trigger:
  - below: '25'
    entity_id: sun.sun
    platform: numeric_state
    value_template: '{{ state.attributes.elevation }}'
  - at: '16:00:00'
    platform: time

This is beautiful in it’s simplicity!

First notice there are 2 triggers. If either is triggered, it will check that both (and) conditions are met and will turn the lights on.

If the sun elevation hits 25° or the time is 4pm it will make sure it’s between 4pm and 10pm and also that the sun is below 25°.

This means if the sun at 3:30pm is below 25° it is triggered but the condition of after 4pm will fail and the lights stay off. Similarly, in summer, at 4pm it is triggered but the condition of below 25° fails and the lights stay off. In winter, the sun is below 25° at 4pm so the lights come on.

Nice!



Leave a Reply