Best way to Turn ON and OFF a pool Water-pump? or a Garden watering

NO… stil the same…
with 0 wnd 2

    platform: time_pattern
    minutes: '/1'
    seconds: '0'

and

trigger:
    platform: time_pattern
    minutes: '/1'
    seconds: '2'

i’m trying to trigger the automation after the sensor.time update

even with ‘2’
!!!
when i chage seconds: ‘2’ the trigger stop work
!!!

I used this automation to test time_pattern:

- alias: 'time test'
  trigger:
    platform: time_pattern
    minutes: '/1'
    seconds: '0'
  action:
    service: system_log.write
    data_template:
      message: "Triggered: {{now()}}"
      level: warning

It works as expected. It triggers every minute, on the minute.

Try it on your system and then check Developer Tools > Logs to confirm you get the same results.

there is a misunderstood

my automation triggers at 00.sec
the sensor.time update every minute at 01.sec

Capturar

so… when the automation triggers… the sensor.time not yet in the right minute
match only happen min+1

Ah! I understand now.

sensor.time is not aligned with time_pattern. There’s a one-second difference.

OK, I’ll explore how to adapt the template to avoid this one-second difference.

Screenshot from 2020-03-08 11-33-10

can we add a minute to sensor.time variable

- 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?

or use now() instead use sensor.time
the system time… the same used in trigger…

LOTs of Thanks

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):

      value_template: "{{ now().hour + now().minute != 0 }}"

In the second condition’s template, and the action’s service_template, the current time is now calculated like this:

      {% set t = '{:02d}:{:02d}'.format(now().hour, now().minute) %}

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
1 Like

now every thing IS OK… Perfect
you are TOP

I’m so greatfull…
THANKS,

what can i do for you? really…

the only thing i change was erase the INITAL VALUE of the INPUT_DATETIME variables…
beaucause, when i restart RPI… lose my map…

THANKS AGAIN… i would like to meet you around

i hope this exercise had improve YOUR BIG knowledge…
:pray: :pray: :pray: :pray: :pray: :pray:
Gustavo Pinto from Portugal
any thing i can help…

1 Like

Send me a box of assorted Portuguese pastries (Malasadas, Pastel de Nata, Sonhos, etc). :wink:

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.