What's a better solution?

So what’s better, resource wise, between these two automations:

It’s use to check every hours between 1 and 5 am if the temperature was raised and then lower it back.

This code runs at 1, 2, 3, 4 and 5 am.

- id: '1584584156367'
  alias: Présence - Michèle la nuit
  description: ''
  trigger:
  - hours: '1'
    platform: time_pattern
  - hours: '2'
    platform: time_pattern
  - hours: '3'
    platform: time_pattern
  - hours: '4'
    platform: time_pattern
  - hours: '5'
    platform: time_pattern
  condition:
  - condition: state
    entity_id: person.michele
    state: Home
  - condition: state
    entity_id: light.lumieremicheleplafonnier
    state: 'Off'
  - condition: state
    entity_id: light.chambre_de_michele
    state: 'Off'
  action:
  - data:
      temperature: 17
    entity_id: climate.neviweb130_climate_michele
    service: climate.set_temperature

This code runs every hour and condition limits it to kick in between 1 and 5 am.

- id: '1584584156367'
  alias: Présence - Michèle la nuit
  description: ''
  trigger:
  - hours: '*'
    platform: time_pattern
  condition:
  - condition: state
    entity_id: person.michele
    state: Home
  - condition: state
    entity_id: light.lumieremicheleplafonnier
    state: 'Off'
  - condition: state
    entity_id: light.chambre_de_michele
    state: 'Off'
 -  condition: time
    after: '00:59:00'
    before: '05:59:00'
  action:
  - data:
      temperature: 17
    entity_id: climate.neviweb130_climate_michele
    service: climate.set_temperature

Thanks.

  • The first one “wakes up” five times (then checks the conditions).
  • The second one “wakes up” 24 times (then checks the conditions).

The first one is triggered far fewer times than the second one.

I’m curious to know why you chose to use a Time Pattern Trigger instead of a Time Trigger?

Good question, because I thought I could use something like that in time patterns:
hours: ‘1-5’ so one lime would have been enough.

Time based triggers are evaluated every second. So for the first automation the triggers are evaluated 432,000 times a day, whereas for the second automation the triggers are only evaluated 86,400 times a day. So from that perspective, fewer triggers are better.

@SylvainGa, the states in your conditions for the lights are wrong. States are case sensitive. They need to be lower case: 'off'

Same thing for the person entity; it should be: home

When writing automations, etc., you need to use the states as they are shown on the STATES tab of the Develop Tools page. What you see elsewhere in the UI are translations.

Why is the 2nd one evaluated fewer times? Both are time-based so I would assume both are checked every second to determine if the current time matches the criteria. What is it about the time_pattern that reduces the number of “ticks”?

Here’s the new code:

- id: '1584584156367'
  alias: Présence - Michèle la nuit
  description: ''
  trigger:
  - hours: '*'
    platform: time_pattern
  condition:
  - after: 0:30:00
    before: '5:30:00'
    condition: time
  - condition: state
    entity_id: person.michele
    state: home
  - condition: state
    entity_id: light.lumieremicheleplafonnier
    state: 'off'
  - condition: state
    entity_id: light.chambre_de_michele
    state: 'off'
  action:
  - data:
      temperature: 17
    entity_id: climate.neviweb130_climate_michele
    service: climate.set_temperature

@pnbruckner, I have a tendency to always capitalize the first word of a line so I sometime forgets NOT to do it here :slight_smile:

@123 I would assume because the first one has 5 time events (time pattern are not required actually, just time) so 5 * 86,400 is 432,000, but the second one is launched every hour to do the time testing in its condition so not sure which one in the end has less impact on the system. Maybe it’s just trivial anyway. My Raspberry 3 CPU 15 minutes load is still just at 0.24 for my 79 running automations.

I don’t really like comparing numbers as you rarely compare apples with apples, but on a pi 3, 5 min average is 0.2 for 192 automations.
So you ‘could’ do some optimisations, but not sure it matters that much. :man_shrugging:

If you used both automations, what would happen each second?

  • The first automation gets all 5 of its triggers evaluated.
  • The second automation gets its single trigger evaluated.

So each second (each “clock tick”) Home Assistant would be evaluating 5 + 1 + any other time-based triggers. That’s still 86,400 times per day and not 86,400 multiplied by the number of time-based triggers found in all automations.

The first one has five times as many triggers, so there are five times as many evaluations that have to be done every second.

EDIT: But, in general, I agree that it probably doesn’t make a huge difference either way. It’s best to write the automations the way that makes the most sense. Optimization can be done later, if it’s ever needed.

Sorry but does this imply that a ‘normal’ time trigger at say 19:21:04 is actually equated every second of the day as well ?

:scream:
:scream_cat:
:rofl:

Well,it DOES have to test if the time is 19:21:04 so I would say yes.

Yes, it is.

Yes, the first automation has 5 checks to do each clock tick whereas the second automation has just one. However that does not translate into “432,000 times per day”. It’s still 86,400 times per day.

It would be fair to say that both are evaluated 86,400 times per day but the first one has 5 times the workload. Seen that way, I would have to recant my previous statement and say the second automation, not the first, is more efficient because it has 5 times less things to do each second (86400 vs 43200 things to do).

I said “the triggers”, not “each trigger”, but I can see how it’s a bit ambiguous. The first automation results in 432,000 evaluations per day. That is how often the current time is compared against the five time patterns as a whole.

Yes the first one has five time the load but the second one is triggered every time where the condition is checked, but being only once per hour, I would presume it’s far less than every seconds.

Yes, conditions are only evaluated once one of the triggers “fires.”

would this fire each second:

  trigger:
    - platform: time
      at: '00:00:01'

and this only once a day:

  trigger:
    - platform: state
      entity_id: sensor.date

never thought about that tbh. quickly running through my automations now, must have a few time triggers ;-(

Going through my automations and have this one (one of my most complex) which runs every minutes of the 5th hour and 7th hour (5th hour is for week days and 7th hour is for weekends or holidays) and it checks if it’s time to raise the room’s temperature based on the outside temperature. Idea here is colder it is outside, longer it will take to warm up the room. Would just one time pattern with * for hours and minutes with a check for the time and weekday as the first two conditions be easier on the load?

- id: '1582595974178'
  alias: Présence - Michèle le matin
  description: ''
  trigger:
  - hours: '5'
    minutes: '*'
    platform: time_pattern
  - hours: '7'
    minutes: '*'
    platform: time_pattern
  condition:
  - condition: state
    entity_id: person.michele
    state: home
  - condition: or
    conditions:
    - condition: and
      conditions:
      - condition: state
        entity_id: binary_sensor.workday_today
        state: 'on'
      - after: '5:00:00'
        before: '6:00:00'
        condition: time
      - condition: template
        value_template: '{% set at_time = 6 * 60 %} {% set cur_time = now() %} {%
          set cur_time = cur_time.hour * 60 + cur_time.minute %} {% set set_temp =
          states.climate.neviweb130_climate_michele.attributes.temperature | float
          %} {% set max_temp = 15.0 %} {% if states(''sensor.outside_temperature'')
          == ''unavailable'' %} {% set out_temp = states(''sensor.temperature'') |
          float %} {% else %} {% set out_temp = states(''sensor.outside_temperature'')
          | float %} {% endif %} {{ at_time - cur_time == 0 or (set_temp < 20.0 and
          at_time - cur_time > 0 and out_temp < max_temp and at_time - cur_time +
          out_temp * 1.5 - 30.0 < 0) }}'
    - condition: and
      conditions:
      - condition: state
        entity_id: binary_sensor.workday_today
        state: 'off'
      - after: '7:00:00'
        before: '8:00:00'
        condition: time
      - condition: template
        value_template: '{% set at_time = 8 * 60 %} {% set cur_time = now() %} {%
          set cur_time = cur_time.hour * 60 + cur_time.minute %} {% set set_temp =
          states.climate.neviweb130_climate_michele.attributes.temperature | float
          %} {% set max_temp = 15.0 %} {% if states(''sensor.outside_temperature'')
          == ''unavailable'' %} {% set out_temp = states(''sensor.temperature'') |
          float %} {% else %} {% set out_temp = states(''sensor.outside_temperature'')
          | float %} {% endif %} {{ at_time - cur_time == 0 or (set_temp < 20.0 and
          at_time - cur_time > 0 and out_temp < max_temp and at_time - cur_time +
          out_temp * 1.5 - 30.0 < 0) }}'
  action:
  - data_template:
      temperature: '{% if is_state(''binary_sensor.workday_today'',''on'') %}22{%
        else %}21{% endif %}'
      entity_id: climate.neviweb130_climate_michele
    service: climate.set_temperature

I can agree with that phrasing. :+1:

Out of curiosity, if there are no time-based triggers used in any automation, would you know if Home Assistant’s core still employs a 1-second clock tick for other purposes? In other words, even in the absence of time-based triggers, Home Assistant still does some sort of internal housekeeping requiring a 1-second tick?

There is code that fires a time_changed event every second. (And if that second is missed by more than a second, it also fires a timer_out_of_sync event.)

Time based triggers are implemented by adding a function (and associated data) to a list associated with the time_changed event. Whenever that event occurs, all the functions in the list are run. (This is a very common pattern in HA. State changes are processed this way, too, but for the state_changed event.) Each time based trigger causes one addition to the time_change event list.

Integrations also often add functions to be called on a time basis.

But HA itself does not do anything on a periodic basis. It’s mostly event driven.

If you go to the EVENTS tab of the Developer Tools page, and look at the list on the right, you can see how many “listeners” there are for the time_changed and other events.

1 Like