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

Making an automations to control a switch ON and OFF
i need to use diferent input_datetime entities… already defined in configuratiom.yaml
Capturar
they are already in my UI

I will automate under this entities… i want to change them over UI. and i cant set one of them to be neutral.

i have 3 cycles for each day… but sometimes i only need 2… if i set it to 0:00 the automation will happen at midnight…

if i could blank it… the now() state never match the entity.state

Thanks for suggestions…

It can’t be empty.

If you never plan to use midnight (00:00) for your schedule, then you can use 00:00 to represent an empty value. Just add a condition to the automation that rejects 00:00 so that the action is not executed.

I posted an example in your other topic. Here it is again but with a condition to reject 00:00.

- id: '1583526600964'
  alias: piscina_bomba_auto
  trigger:
    platform: template
    value_template: "{{states('sensor.time') == states('input_datetime.h_pb_on_3b')}}"
  condition:
    condition: template
    value_template: "{{states('input_datetime.h_pb_on_3b') != '00:00'}}"
  action:
    service: switch.turn_on
    entity_id: switch.sonoff_10009ace1f

Thanks…

let me explain deeper

i have a pool, and i control the water-pump with a electric clock in tehe electric board.
i want to control this water-pump with an HA automation over a sonoff basic switch.

so:
On my HA UI i have a map of weekdays, and for each weekday i have 3 time cycles ON/OFF


if i toogle on the automation i want the HA to read my Weekday/Time’s map and act over switch

i learn i need to ignore midnight…

but i need action:
switch.turn_on
switch.turn_off
in the same automation…

with this structure i belive i can do it
adding some more IFs in the action code

- id: '1583526600964'
  alias: piscina_bomba_auto
  trigger:
  - platform: time_pattern
    minutes: /1
  condition:
    condition: template
    value_template: "{{states('sensor.time') != '00:00'}}"
  action:
  - entity_id: switch.sonoff_10009ace1f
    service_template: "{% if states.sensor.time.state == states.input_datetime.h_pb_on_3b.state\
      \ %}\n  switch.turn_on\n{% else %}\n  switch.turn_off\n{% endif %}\n"

BUT THE ON AND OFF APEAR 1 MINUTE AFTER THE input_datetime.value

i can try to subtract 1 minute in the if…now() - 00:01 == input_datetime.value
but i didn’t reach the right code…

Any ideias? other way to do it?
i goole it for a long time… i’m surprise… because i think there are lot of automations following this ideia
like garden watering… pools, … even christmas tres… lolololo

thanks for your help
i’m new in this world

after publish i saw your reply in the other post
… sorry… lets use only this

i first code is only to try… i’m usung the ELSE part for off… but may ideia is diferente:

action:

if monday

action:
if monday
if now = A or B or C
switch on
if now = D or E or F
switch off

if tuesday
if now = A or B or C
switch on
if now = D or E or F
switch off

and so on… (not using the else code)

it seems complicated…
and every minute i have a trigger

can i find a diferente way?
thamks

Do you want the automation to access this schedule?

If you do, then what kind of entities are you using to store the schedule? The automation needs to access those entities.

For example, Tuesday has:
ON
17:08

What kind of entities are you using to store 17:08 and ON?

one for each
configurations.yaml

input_datetime:
  h_pb_on_2a:
    has_date: false
    has_time: true
  h_pb_off_2a:
    has_date: false
    has_time: true
  h_pb_on_2b:
    has_date: false
    has_time: true
  h_pb_off_2b:
    has_date: false
    has_time: true
  h_pb_on_2c:
    has_date: false
    has_time: true

any other way?

all i want to do is this:
Capturar
with a HA automation + switch
and take control anyware…

thanks

i’m not so strange… lololol… i’m?

Your schedule has:
7 days
3 periods per day
2 states per period

7 x 3 x 2 = 42 input_datetime entities.

That means the automation will need to check 42 entities every minute.

Does the schedule have to be so flexible that you need the ability to set the on/off times using a UI? The alternative is to hard-code the times within the automation. For example, a Time Condition lets you specify the time and day.

almost…

the first IF is the weekday…
inside of this one 6 entities…

so… every minute… 7 IFs for weekday… and one off then with 6 entities…

i know is a lot… but i don’t find another way…
do you think it’s hard?
RPi will be freezen?


beaucause i’m new i can’t post more today… :(((… forum limitations…

2-3-4-5-6-s-d
mon-tue-wed-thu-fri-sat-sun

Sure, in your pseudo-code:

if monday
if now = A or B or C
switch on
if now = D or E or F
switch off

but you haven’t shown how that will done in an actual template.

Does the 2 in h_pb_on_2a represent the day of the week? Is that Tuesday or Monday (it depends on whether you consider Sunday or Monday as the first day of the week).

Here’s my suggested solution.

It requires that all 42 input_datetime entities have ordered names. This is the naming format:

<day of the week><period of the day><state>
  1. Day of the week is an integer value representing the ISO weekday (Monday = 1, Sunday = 7).
  2. Period of the day is an integer value between 1 and 3.
  3. State is an integer value where 0 represents off and 1 is on

For example, this represents Saturday, Period 3, ON:

input_datetime:
  '631':
    name: 'Saturday Period 3 ON'
    has_date: false
    has_time: true
    initial: '00:00'

For your convenience, I have generated the configuration for all 42 input_datetime entities here:

Click to show

input_datetime:
  '111':
    name: 'Monday Period 1 ON'
    has_date: false
    has_time: true
    initial: '00:00'
  '110':
    name: 'Monday Period 1 OFF'
    has_date: false
    has_time: true
    initial: '00:00'
  '121':
    name: 'Monday Period 2 ON'
    has_date: false
    has_time: true
    initial: '00:00'
  '120':
    name: 'Monday Period 2 OFF'
    has_date: false
    has_time: true
    initial: '00:00'
  '131':
    name: 'Monday Period 3 ON'
    has_date: false
    has_time: true
    initial: '00:00'
  '130':
    name: 'Monday Period 3 OFF'
    has_date: false
    has_time: true
    initial: '00:00'

  '211':
    name: 'Tuesday Period 1 ON'
    has_date: false
    has_time: true
    initial: '00:00'
  '210':
    name: 'Tuesday Period 1 OFF'
    has_date: false
    has_time: true
    initial: '00:00'
  '221':
    name: 'Tuesday Period 2 ON'
    has_date: false
    has_time: true
    initial: '00:00'
  '220':
    name: 'Tuesday Period 2 OFF'
    has_date: false
    has_time: true
    initial: '00:00'
  '231':
    name: 'Tuesday Period 3 ON'
    has_date: false
    has_time: true
    initial: '00:00'
  '230':
    name: 'Tuesday Period 3 OFF'
    has_date: false
    has_time: true
    initial: '00:00'

  '311':
    name: 'Wednesday Period 1 ON'
    has_date: false
    has_time: true
    initial: '00:00'
  '310':
    name: 'Wednesday Period 1 OFF'
    has_date: false
    has_time: true
    initial: '00:00'
  '321':
    name: 'Wednesday Period 2 ON'
    has_date: false
    has_time: true
    initial: '00:00'
  '320':
    name: 'Wednesday Period 2 OFF'
    has_date: false
    has_time: true
    initial: '00:00'
  '331':
    name: 'Wednesday Period 3 ON'
    has_date: false
    has_time: true
    initial: '00:00'
  '330':
    name: 'Wednesday Period 3 OFF'
    has_date: false
    has_time: true
    initial: '00:00'

  '411':
    name: 'Thursday Period 1 ON'
    has_date: false
    has_time: true
    initial: '00:00'
  '410':
    name: 'Thursday Period 1 OFF'
    has_date: false
    has_time: true
    initial: '00:00'
  '421':
    name: 'Thursday Period 2 ON'
    has_date: false
    has_time: true
    initial: '00:00'
  '420':
    name: 'Thursday Period 2 OFF'
    has_date: false
    has_time: true
    initial: '00:00'
  '431':
    name: 'Thursday Period 3 ON'
    has_date: false
    has_time: true
    initial: '00:00'
  '430':
    name: 'Thursday Period 3 OFF'
    has_date: false
    has_time: true
    initial: '00:00'

  '511':
    name: 'Friday Period 1 ON'
    has_date: false
    has_time: true
    initial: '00:00'
  '510':
    name: 'Friday Period 1 OFF'
    has_date: false
    has_time: true
    initial: '00:00'
  '521':
    name: 'Friday Period 2 ON'
    has_date: false
    has_time: true
    initial: '00:00'
  '520':
    name: 'Friday Period 2 OFF'
    has_date: false
    has_time: true
    initial: '00:00'
  '531':
    name: 'Friday Period 3 ON'
    has_date: false
    has_time: true
    initial: '00:00'
  '530':
    name: 'Friday Period 3 OFF'
    has_date: false
    has_time: true
    initial: '00:00'

  '611':
    name: 'Saturday Period 1 ON'
    has_date: false
    has_time: true
    initial: '00:00'
  '610':
    name: 'Saturday Period 1 OFF'
    has_date: false
    has_time: true
    initial: '00:00'
  '621':
    name: 'Saturday Period 2 ON'
    has_date: false
    has_time: true
    initial: '00:00'
  '620':
    name: 'Saturday Period 2 OFF'
    has_date: false
    has_time: true
    initial: '00:00'
  '631':
    name: 'Saturday Period 3 ON'
    has_date: false
    has_time: true
    initial: '00:00'
  '630':
    name: 'Saturday Period 3 OFF'
    has_date: false
    has_time: true
    initial: '00:00'

  '711':
    name: 'Sunday Period 1 ON'
    has_date: false
    has_time: true
    initial: '00:00'
  '710':
    name: 'Sunday Period 1 OFF'
    has_date: false
    has_time: true
    initial: '00:00'
  '721':
    name: 'Sunday Period 2 ON'
    has_date: false
    has_time: true
    initial: '00:00'
  '720':
    name: 'Sunday Period 2 OFF'
    has_date: false
    has_time: true
    initial: '00:00'
  '731':
    name: 'Sunday Period 3 ON'
    has_date: false
    has_time: true
    initial: '00:00'
  '730':
    name: 'Sunday Period 3 OFF'
    has_date: false
    has_time: true
    initial: '00:00'

Here is the automation:

- alias: 'Pool Schedule'
  trigger:
    platform: time_pattern
    minutes: '/1'
  condition:
    - condition: template
      value_template: "{{states('sensor.time') != '00:00'}}"
    - condition: template
      value_template: >
        {% set t = states('sensor.time') %}
        {% 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 = states('sensor.time') %}
      {% 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

Here’s how it works:

  • The first condition stops execution if the time is 00:00. This permits the use of 00:00 as a do-nothing value.
  • The second condition uses for-loops to iterate through all input_datetimes for the current day. That means it only needs to check 6 input_datetimes (3 periods x 2 states).
  • It checks if the current time matches any of the time-values in the 6 input_datetimes.
  • If the condition fails to find a matching time-value, execution stops. Otherwise, if it finds a match, it proceeds to the action.
  • The action repeats the same process performed in the condition in order to determine which input_datetime contains the matching time-value.
  • The last character in the input_datetime’s name is either 1 or 0. This is used to determine if the service should be switch.turn_on or switch.turn_off.

EDIT
Corrected typo in automation.
Replaced:

        {{ ns.datetime != '' %}

with:

        {{ ns.datetime != '' }}

1 Like

:pray: :pray: :pray: :pray: :pray:
very NICEEEE
you are so kind… THANKs… a LOT

I Understand you code… always learning

i dont know why… but give me some erros…

could we have diferent versions…???
i’m runing HassOS 3.11 with RPI 4 4Gb SD32Gb

Capturar

Blockquote
Invalid config for [automation]: required key not provided @ data[‘action’]. Got None
required key not provided @ data[‘trigger’]. Got None. (See /config/configuration.yaml, line 13).
Invalid config for [automation]: invalid template (TemplateSyntaxError: unexpected ‘}’) for dictionary value @ data[‘condition’][1][‘value_template’]. Got None
not a valid value for dictionary value @ data[‘condition’][1][‘condition’]. Got None
required key not provided @ data[‘condition’][1][‘entity_id’]. Got None. (See /config/configuration.yaml, line 13)

for example:
in my code i can’t have ’
Capturar
don’t work for me

i don’t understand why…!!!.. :frowning:

you did a very good approach in your code…
and i’m sure it’s OK

why not working in my HA?

  • I already change the 42 input_datetime code in my config.yaml
  • tuned my UI with new names… went ok
  • when update Automations.yaml with your code… have erros

could we have diferent versions…???
i’m runing HassOS 3.11 with RPI 4 4Gb SD32Gb

my automations.yaml
what i’m doing wrong?

:pray: :pray: :pray: :pray: :pray:
THANKs again

- id: '1583526600964'
- alias: 'piscina_bomba_auto'
  trigger:
    platform: time_pattern
    minutes: '/1'
  condition:
    - condition: template
      value_template: "{{states('sensor.time') != '00:00'}}"
    - condition: template
      value_template: >
        {% set t = states('sensor.time') %}
        {% 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 = states('sensor.time') %}
      {% 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

Don’t worry, it did not work for either because I made a small error (but forgot to fix the version I posted).

In the automation’s second condition, on the last line, I used %} instead of }} to end the line. To fix it, replace the % with } like this:

        {{ ns.datetime != '' %}

with

        {{ ns.datetime != '' }}

not yet

error
Invalid config for [automation]: required key not provided @ data[‘action’]. Got None
required key not provided @ data[‘trigger’]. Got None. (See /config/configuration.yaml, line 13).

automations.yaml
already updated

- id: '1583526600964'
- alias: 'piscina_bomba_auto'
  trigger:
    platform: time_pattern
    minutes: '/1'
  condition:
    - condition: template
      value_template: "{{states('sensor.time') != '00:00'}}"
    - condition: template
      value_template: >
        {% set t = states('sensor.time') %}
        {% 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 = states('sensor.time') %}
      {% 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

Your version is a little bit different than mine because the first line contains:
- id: '1583526600964'

Change this:

- id: '1583526600964'
- alias: 'piscina_bomba_auto'

to this:

- id: '1583526600964'
  alias: 'piscina_bomba_auto'

YES… now runs well

but appens my first PROBLEM
the switch only change 1 min after the defined time…!!!

real test:

at 13:24
i setup the ‘711’ = 13:26 and ‘710’ = 13:27

what happened
at 13:26 nothing
at 13:27 Switch_ON
and at 13:28 Switch_OFF

it have 1 min diference… later
like in mine 1st little test

could it be beaucause of this:
when i check my HA log diary
my sensor.time update 1 sec later!!!

can be beaucause of this?
Capturar
where we can see… 1min later reactions of switch, too

thansk for you enfforts

I think I know why that’s happening. I don’t normally use the time_pattern trigger so I forgot about a small but important detail.

Change it to this:

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

It means it should trigger every minute and zero seconds.
17:00:00
17:01:00
17:02:00

Without specifying 0 seconds, this can happen:
17:00:55
17:01:55
17:02:55
It triggers every minute but not on the minute.

Yes, I think it is the problem

At 00.sec triggers the automation and at 01.sec the sensor.time is updated

So… at the automation moment the sensor.time have min-1