Time period and presence based switching

At home I have hot water circulation which circulates the hot water in the pipes to reach hot water immediately. This has to be work only in a given time period. E.g. weekdays from 6AM to 7AM and 6PM to 10PM, weekends from 8AM to 2PM and also 6PM to 10PM. Which makes it more complicated I’d like to run only when anybody is at home. Even the first person arrives after the given switch on time. E.g. in my case the circulation should start az 6PM, but at that time nobody is at home. The first person arrives 7 PM. In this case the circulation should start at 7PM when the first person arrives.
There is a trigger which is my DSC alarm system hooked to Home Assistant, It has different states like “disarmed”, “armed_away”, “armed home”. But in the morning there is no change when the circulation has to start.
So the circuklation has to start if the status of DSC is “disarmed” or “armed_home” and the time period is between the given 6AM-7AM or 6PM-10PM. And also should start if the status changes to “disarmed” within the given time period.
In a nutshell it should work between a time period if we are at home (“armed_away” or “disarmed”), or when we arrive at home (change to “disarmed”) in the given time period.
What is the easiest automation? I was thinking on this so long but no real good solution.

Check out Schedy, might be what you need.

Thanks, I’ll take a look. In the meantime I have a “something like a solution”. I could manage with several different automations. One listens to disarming alarm system and weekday/weekend and also conditioned with the Time. The others tracks the start time with a condition of alarm system.

Guessing you could get away with four different automations - though more would make it a little less ‘messy’. Also might help to create a sensor.weekday, personally I use these for various automations:

    date_dayoftheweek:
      value_template: >
        {% set ct = states('sensor.date_time') %}
        {% set ct = as_timestamp(strptime(ct,'%Y-%m-%d, %H:%M')) %}
        {{ ct | timestamp_custom("%A") }}

    date_weekend:
      value_template: >
        {% if is_state('sensor.date_dayoftheweek', 'Saturday') %} weekend
        {% elif is_state('sensor.date_dayoftheweek', 'Sunday') %} weekend
        {% else %} weekday
        {% endif %}

    date_weekday:
      value_template: >
        {% if is_state('sensor.date_weekend', 'weekday') %} on
        {% else %} off
        {% endif %}

The date_weekday on/off is what we’ll use for yours.

Automation 1:

- id: '1580155286486'
  alias: Turn ON Recirc Pump - WeekDAY
  description: ''
  trigger:
  - at: 06:00:00
    platform: time
  - at: '18:00:00'
    platform: time
  - entity_id: alarm_control_panel.home
    platform: state
    to: disarmed
  condition:
  - condition: or
    conditions:
    - after: 06:00:00
      before: 07:00:00
      condition: time
    - after: '18:00:00'
      before: '22:00:00'
      condition: time
  - condition: or
    conditions:
    - condition: state
      entity_id: alarm_control_panel.home
      state: disarmed
    - condition: state
      entity_id: alarm_control_panel.home
      state: armed_home
  - condition: state
    entity_id: switch.aux6
    state: 'off'
  - condition: state
    entity_id: sensor.date_weekday
    state: 'on'
  action:
  - entity_id: switch.aux6
    service: switch.turn_on

Automation 2:

- id: '1580155286486'
  alias: Turn OFF Recirc Pump - WeekDAY
  description: ''
  trigger:
  - at: 07:00:00
    platform: time
  - at: '22:00:00'
    platform: time
 conditions:
  - condition: state
    entity_id: switch.aux6
    state: 'on'
  - condition: state
    entity_id: sensor.date_weekday
    state: 'on'
  action:
  - entity_id: switch.aux6
    service: switch.turn_off

You could then create two more automations changing sensor.date_weekday to OFF and changing the triggered times as well. You could arguably also put in another trigger for Automation 2 where state alarm goes TO armed_away to turn it off.

I just threw this together, but should get you close.

Why don’t you se this, the workday sensor

werkdag/weekend

binary_sensor:

  • platform: workday
    country: NL
    excludes: [sat, sun, holiday]

  • platform: workday
    country: NL
    name: Holiday
    workdays: [sat, sun, holiday]
    excludes: []

Always more than one way to skin-a-cat with HA @wolfpins. :tomato: - :tomato: IMO. I use mine because it works for me :slight_smile:

Also, fwiw, I did try using workday and the HOLIDAYs aren’t the same holidays my office uses, so it threw me off the first time it was an ‘HA DEFINED HOLIDAY’ and NOT a work holiday at the office - so I built my own for that as well… :wink: Yes, I have to update the array, but I only have to do so once a year - and I reboot far more often than that… :joy:

    date_workday:
      value_template: >
        {% set ct = states('sensor.date_time') %}
        {% set ct = as_timestamp(strptime(ct,'%Y-%m-%d, %H:%M')) %}
        {% set date = states('sensor.date') %}
        {% set holidays = {
        '2020-01-01' : '1',
        '2020-05-25' : '1',
        '2020-07-03' : '1',
        '2020-09-07' : '1',
        '2020-11-26' : '1',
        '2020-12-25' : '1'} %}
        {% if 'Saturday' in ct | timestamp_custom("%A") %}
          off
        {% elif 'Sunday' in ct | timestamp_custom("%A") %}
          off
        {% elif holidays[date] == '1' %}
          off
        {% else %}
          on
        {% endif %}

:Thanks a lot. To be honest I did nearly the same, I added 2 more automations: alarm armed away and alarm disarmed. Alarm away only switches off the circulation, no matter when we leave the house (arm_away). Disarmed will do many other activities later on. Currently it just listening the time and when it is in the period it switches on the circulation.