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:
Ah, unfortunately this does not help
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
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"
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:
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.