- condition: template
value_template: >
{% set t = states('sensor.time')+1min %}
and
action:
service_template: >
{% set t = states('sensor.time')+1min %}
```
with that... at automation trigger moment will match
how can i do it? is it possible?
Yes, the way to ensure the time is aligned is to use now() instead of sensor.time.
- id: '1583526600964'
alias: 'piscina_bomba_auto'
trigger:
platform: time_pattern
minutes: '/1'
seconds: '0'
condition:
- condition: template
value_template: "{{ now().hour + now().minute != 0 }}"
- condition: template
value_template: >
{% set t = '{:02d}:{:02d}'.format(now().hour, now().minute) %}
{% set ns = namespace(datetime = '') %}
{% set d = now().isoweekday() %}
{% for p in range(1,4) %}
{% for s in range(0,2) %}
{% set dt = 'input_datetime.' ~ d ~ p ~ s %}
{% if t == states(dt)[:5] %}
{% set ns.datetime = dt %}
{% endif %}
{% endfor %}
{% endfor %}
{{ ns.datetime != '' }}
action:
service_template: >
{% set t = '{:02d}:{:02d}'.format(now().hour, now().minute) %}
{% set ns = namespace(datetime = '') %}
{% set d = now().isoweekday() %}
{% for p in range(1,4) %}
{% for s in range(0,2) %}
{% set dt = 'input_datetime.' ~ d ~ p ~ s %}
{% if t == states(dt)[:5] %}
{% set ns.datetime = dt %}
{% endif %}
{% endfor %}
{% endfor %}
switch.turn_{{'on' if ns.datetime[-1] == '1' else 'off'}}
entity_id: switch.sonoff_10009ace1f
The first condition’s template changes to this (short and simple):
Here’s an alternative version. This one takes advantage of the fact an input_datetime has attributes called hour and minute. It can compare those attributes to now().hour and now().minute instead of doing a string-match like in the previous version: if t == states(dt)[:5]
- id: '1583526600964'
alias: 'piscina_bomba_auto'
trigger:
platform: time_pattern
minutes: '/1'
seconds: '0'
condition:
- condition: template
value_template: "{{ now().hour + now().minute != 0 }}"
- condition: template
value_template: >
{% set ns = namespace(datetime = '') %}
{% set d = now().isoweekday() %}
{% for p in range(1,4) %}
{% for s in range(0,2) %}
{% set dt = 'input_datetime.' ~ d ~ p ~ s %}
{% if state_attr(dt, 'hour') == now().hour and
state_attr(dt, 'minute') == now().minute %}
{% set ns.datetime = dt %}
{% endif %}
{% endfor %}
{% endfor %}
{{ ns.datetime != '' }}
action:
service_template: >
{% set ns = namespace(datetime = '') %}
{% set d = now().isoweekday() %}
{% for p in range(1,4) %}
{% for s in range(0,2) %}
{% set dt = 'input_datetime.' ~ d ~ p ~ s %}
{% if state_attr(dt, 'hour') == now().hour and
state_attr(dt, 'minute') == now().minute %}
{% set ns.datetime = dt %}
{% endif %}
{% endfor %}
{% endfor %}
switch.turn_{{'on' if ns.datetime[-1] == '1' else 'off'}}
entity_id: switch.sonoff_10009ace1f
Send me a box of assorted Portuguese pastries (Malasadas, Pastel de Nata, Sonhos, etc).
I’m just glad to hear that it works the way you want it to.
Yes, I did learn a few new things, notably the one-second difference between now() and sensor.time.
Good luck and enjoy the piscina! Where I live in Canada, my pool is currently frozen solid and under about 30 cm of snow. It won’t be ready to (comfortably) swim in it until June.