How to calculate a constant time of a given date (yesterday)?

Hi guys, I’m stuck a bit with a date calculation. I’ve got a hot water circulation script, that circulatest the water in the morning (before wake up). I wanted to add a condition that checks if there was somebody in the house after 8:00PM Yesterday or e.g. left for vacation and no circulation is needed in the morning.

So I wanted to do something like: “If there was a movement after 8:00PM Yesterday then circulate, else don’t”.
I’m struggling with getting the time for 8:00PM Yesterday. I managed to extract the day for yesterday, but I do not really know how to build up (concatenate?) the whole date&time from this:

{{ (as_timestamp(now()) - (24*3600)) | timestamp_custom('%d', True) }}

Thank you very much in advance!

Does this run at a fixed time?

If so, you only need to check for movement in a fixed interval. i.e. the time your script runs minus 8pm yesterday. Forget about the date.

[ X seconds] = time script runs - 8pm yesterday.

condition:
  condition: template
  value_template: "{{ as_timestamp(now()) - as_timestamp(states.sensor.movement.last_changed) < [X seconds] }}"

Ah, unfortunately this does not help :frowning:
The thing is that the script runs at 5:00, 5:30, 6:00, 6:30, 7:00.
So every time the script runs, [X seconds] needs to be a different (30 min increasing) value. :S

Could somebody please help me out? :S

How do you trigger the script at those times?

Maybe disable the script at 7pm, and use movement in the evening to enable it again. If no movement detected, the script will not run.

1 Like

Thank you guys. This is how it looks like now.
The trigger is a simple time trigger in every 15 mins.
Currently the condition is responsible to check if there was movement in the past 10 hours (input_number.hot_water_circulation_inactivity is set to 10). The problem is that at 5:25 this is going to be 5:25 - 10 = 19:25, then at 5:40 it will be 5:40 - 10 = 19:40, etc.
This is what I wanted to avoid with a fixed time; something like “yesterday 8:00 PM”.

- alias: Hot Water Circulation Morning On P2
  trigger:
  - platform: time
    at: "05:25:00"
  - platform: time
    at: "05:40:00"
  - platform: time
    at: "05:55:00"
  - platform: time
    at: "06:10:00"
  - platform: time
    at: "06:25:00"
  - platform: time
    at: "06:40:00"
  - platform: time
    at: "06:55:00"
  action:
  # Only if there was movement in the past 10 hours (so the house is not empty).
  - condition: and
    conditions:
    - condition: template
      value_template: "{{ (((as_timestamp(now()) - as_timestamp(states('input_datetime.last_motion_detected').replace(',', ''))) / 60 / 60) < ((states('input_number.hot_water_circulation_inactivity')) | int )) }}"
  - service: timer.cancel # The manual timer to be canceled if ran.
    entity_id: timer.hot_water_circulation_manual
  - service: timer.cancel # The automatic timer to be canceled if ran.
    entity_id: timer.hot_water_circulation_automatic
  - service: timer.start
    data_template:
      entity_id: timer.hot_water_circulation_automatic
      duration: >
        {{ ((states('input_number.hot_water_circulation_base_duration')) | int ) | timestamp_custom('%H:%M:%S', false) }}
  - service: input_datetime.set_datetime
    entity_id: input_datetime.hot_water_last_circulated # Setting last run time to current time.
    data_template:
      datetime: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"
  - service: mqtt.publish
    data:
      topic: "shellies/shelly1-8CABCABCABCA/relay/0/command"
      payload: "on"

@ddaannnnee has provided you with an easy solution above.

Hi @tom_I, I might not get it but the sniplet you wrote above will also depend on the time I run the script.

"{{ as_timestamp(now()) - as_timestamp(states.sensor.movement.last_changed) < [X seconds] }}"

The script runs at 6:00, 6:20, etc. As this happens, the condition will always check a different “base point”, because now() changes all the time while X is a constant.

If we assume a 12 hr check and a last movement time at 6:01PM, then the script will give us the following:

At 6:00AM --> 6:00AM - 6:01PM < 12 hours --> TRUE
At 6:20AM --> 6:20AM - 6:01PM < 12 hours --> FALSE

Am I wrong here?:S

You’re only wrong because you’re still looking at my solution instead of @ddaannnnee’s, which is a much simpler idea.

Write an automation that disables your hot water circulation automation after the last morning run time ends. Simplified:

trigger:
  platform: time
  at: "08:00:00"
action:
  service: homeassistant.turn_off
  entity_id: automation.hot_water_circulation_morning_on_p2

Write another automation that re-enables it if movement is detected at night:

trigger:
  platform: state
  entity_id: sensor.your_movement_sensor(s)_here
condition:
- condition: time
  after: "20:00:00"  # until midnight
- condition: state:
  entity_id: automation.hot_water_circulation_morning_on_p2
  state: "off"
action:
  service: homeassistant.turn_on
  entity_id: automation.hot_water_circulation_morning_on_p2

So your pump is disabled from running every day unless movement was detected between 8pm and midnight the day before. Exactly what you wanted.

Oops, thanks I was looking at a wrong post. Sorry guys.
Yeah, this idea is great, I’ve implemented it.

- alias: Hot Water Circulation Morning
  trigger:
  - platform: time
    at: "04:50:00"
  action:
  # Let's turn it off...
  - service: automation.turn_off
    data:
      entity_id: automation.hot_water_circulation_morning_on_p1
  - service: automation.turn_off
    data:
      entity_id: automation.hot_water_circulation_morning_on_p2
  # And then see if there was a movement in the past 10 hours (so we turn it back on).
  - condition: and
    conditions:
    - condition: template
      value_template: "{{ (((as_timestamp(now()) - as_timestamp(states('input_datetime.last_motion_detected').replace(',', ''))) / 60 / 60) < ((states('input_number.hot_water_circulation_inactivity')) | int )) }}"
  - service: automation.turn_on
    data:
      entity_id: automation.hot_water_circulation_morning_on_p1
  - service: automation.turn_on
    data:
      entity_id: automation.hot_water_circulation_morning_on_p2

This script turns off the predefined morning circulation scripts by default at 4:50 BUT then turns those back on (in the “same time”) in case movement was detected in the past 9 hours.