Time condition script

Hi, my entity is not working properly, due to some issue in the Time Configuration
Would appreciate if anyone could help me

  1. In condition I want to add – time 2 to 4 am and 2 to 4 pm – switch.toggle
  2. In the If statement I want to add - time after 4 am to 2 pm & 5pm to 1 am — if the entity state “ON” – ACTION “switch.off”

The below code is not working correctly

trigger:
- platform: state
entity_id:
- binary_sensor.input_2
id: "on"
- platform: state
entity_id:
- binary_sensor.input_2
id: "off"
condition:
- condition: time
after: "18:40:00"
enabled: true
- condition: or
conditions:
- condition: time
after: "06:40:00"
action:
- service: switch.toggle
entity_id: switch.e16s_output2
- if:
- condition: state
state: "on"
entity_id: switch.e16s_output2
- condition: time
after: "08:35:00"
- condition: or
conditions:
- condition: time
after: "20:54:00"
then:
- service: switch.turn_off
entity_id: switch.e16s_output2
mode: single

Please follow Community Guidelines #11 and format your configuration block properly by adding 3 backticks (```) above and below the code block, or by selecting the whole block and using the Preformatted Text button:

image

There are a couple issue I can see even without it formatted properly:

  1. Both of your triggers are going to fire on every state and attribute change because you have not assigned a to or from value.
  2. Your Or condition isn’t doing anything because it only has one condition nested under it.)
1 Like

Sorry Arshad, but this hurts my eyes. Nothing in your description follows the code you are providing. Please follow the suggestions by Drew and also what you want to achieve. :slightly_smiling_face:
From where did you get this code?

Thank you all for helping me correct my mistakes. It has only been a month since I joined the Community and 
started learning about home automation. I truly appreciate all the knowledge and insights I have gained from this community.

My goal is to create a water level indicator on my home automation system. Here's how I plan to achieve it:

1. I will use three single copper wires placed in the water tank at different levels: Lower, Half, and Full.
2. I will connect these wires to digital inputs (DC 0-5V, 4-20mA) on my system.
3. The concept is as follows: 
   - When there is a loop between the Lower and Half wires, Digital Input 1 will be "ON," indicating that the water tank is half full.
   - When there is a loop between the Lower, Half, and Full wires, Digital Input 2 will be "ON," indicating that the water tank is full.
   - When both Digital Input 1 and Digital Input 2 are in the "OFF" state, it means the tank is empty.
4. I have tested this setup, and it is working well. Now, I want to automate it further.

My automation goals are as follows:
- When the state of Digital Input 2 is "ON," I want to turn off the switch.e16s_output_2.
- When the state of Digital Input 2 is "OFF," I want to turn on the switch.e16s_output_2.
- This automation should only occur between 6 AM to 8 AM and 6 PM to 8 PM since water supply is available during these times.
- If switch.e16s_output_2 is "ON" after 6 AM to 8 AM and 6 PM to 8 PM, it should be automatically turned "OFF."
trigger:
  - platform: state
    entity_id:`
     - binary_sensor.input_2
    id: "on"
  - platform: state
    entity_id:
     - binary_sensor.input_2
    id: "off"
condition:
  - condition: time
    after: "18:40:00"
    enabled: true
  - condition: or
    conditions:
      - condition: time
        after: "06:40:00"
action:
 - service: switch.toggle
      entity_id: switch.e16s_output2
  - if:
      - condition: state
        state: "on"
        entity_id: switch.e16s_output2
      - condition: time
        after: "08:35:00"
      - condition: or
        conditions:
          - condition: time
            after: "20:54:00"
    then:
      - service: switch.turn_off
          entity_id: switch.e16s_output2
mode: single
trigger:
  - platform: state
    entity_id: binary_sensor.input_2
    to: 'on'
    id: 'off'
  - platform: state
    entity_id: binary_sensor.input_2
    to: 'off'
    id: 'on'
  - platform: time
    id: 'on'
    at: 
      - "18:00"
      - "06:00"
  - platform: time
    id: time_off
    at: 
      - "20:00"
      - "08:00"
condition:
  - or:
      - condition: trigger
        id: time_off
      - condition: time
        after: "06:00"
        before: "08:00"
      - condition: time
        after: "18:00"
        before: "20:00"
action:
  - choose:
      - alias: Turn off switch at end of time blocks
        conditions:
          - condition: trigger
            id: time_off
        sequence:
          - service: switch.turn_off
            entity_id: switch.e16s_output2
      - alias: Option for sensor-based operation and start of time blocks
        conditions:
          - condition: template
            value_template: "{{ has_value('binary_sensor.input_2') and trigger.id != states('binary_sensor.input_2') }}" 
        sequence:
          - service: switch.turn_{{ trigger.id }}
            entity_id: switch.e16s_output2
mode: single
1 Like

Thank you for the great help. I am going to use the code and monitor it for the next two days. I will provide an update on the status once the monitoring period is over.
:slightly_smiling_face:

Working perfectly as expected. Thanks for support

1 Like

Is there way to merge both the Automation-1 & Automation-2

Reason

  • water pump is on/off between specific time prefectly but to avoid DRY run of pump , i have put input-3 (dry sensor)
  • so, water pump auto off after 5 min. if Dry sensor is OFF

Automation-1 → Water Pump on/off between time

trigger:
  - platform: state
    entity_id: binary_sensor.input_2
    to: "on"
    id: "off"
  - platform: state
    entity_id: binary_sensor.input_2
    to: "off"
    id: "on"
  - platform: time
    id: "on"
    at:
      - "18:40"
      - "06:40"
  - platform: time
    id: time_off
    at:
      - "20:30"
      - "08:30"
condition:
  - condition: or
    conditions:
      - condition: trigger
        id: time_off
      - condition: time
        after: "06:40:00"
        before: "08:30:00"
      - condition: time
        after: "18:40:00"
        before: "20:30:00"
action:
  - choose:
      - alias: Turn off switch at end of time blocks
        conditions:
          - condition: trigger
            id: time_off
        sequence:
          - service: switch.turn_off
            entity_id: switch.e16s_output2
      - alias: Option for sensor-based operation and start of time blocks
        conditions:
          - condition: template
            value_template: >-
              {{ has_value('binary_sensor.input_2') and trigger.id !=
              states('binary_sensor.input_2') }}
        sequence:
          - service: switch.turn_{{ trigger.id }}
            entity_id: switch.e16s_output2

Automation- 2 -> Turn off water pump - to avoid Dry Run

trigger:
  - platform: state
    entity_id:
      - binary_sensor.input_3
    to: "off"
    for:
      hours: 0
      minutes: /5
      seconds: 0
condition:
  - condition: or
    conditions:
      - condition: time
        after: "06:40:00"
        before: "08:30:00"
      - condition: time
        after: "18:40:00"
        before: "20:30:00"
    enabled: true
action:
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.e16s_output2
mode: single

Thanks

Yes, but let’s clarify the conditional logic…

Should the normal automation run if input_3 is off?
Should input_3 turning off for 5 minutes only turn the pump switch off during the time periods, or should it always turn the switch off?

Should the normal automation run if input_3 is off?
Automation should stop - if input_3 is OFF

if switch.e16s_output2 is ON 
 after delay of 5 min. check the status of Input_3
 if the status of input_3 is OFF
Then 
switch.e16s_output2  - turn to OFF

I have tried to modify the script but it does not trigger on time

trigger:
  - platform: state
    entity_id: binary_sensor.input_2
    to: "on"
    id: "off"
  - platform: state
    entity_id: binary_sensor.input_2
    to: "off"
    id: "on"
  - platform: time
    id: "on"
    at:
      - "18:40"
      - "06:40"
  - platform: time
    id: time_off
    at:
      - "20:30"
      - "08:30"
condition:
  - condition: or
    conditions:
      - condition: trigger
        id: time_off
      - condition: time
        after: "09:40:00"
        before: "11:30:00"
      - condition: time
        after: "21:40:00"
        before: "23:30:00"
action:
  - choose:
      - alias: Turn off switch at end of time blocks
        conditions:
          - condition: trigger
            id: time_off
        sequence:
          - service: switch.turn_off
            entity_id: switch.e16s_output2
      - alias: Option for sensor-based operation and start of time blocks
        conditions:
          - condition: template
            value_template: >-
              {{ has_value('binary_sensor.input_2') and trigger.id !=
              states('binary_sensor.input_2') }}
        sequence:
          - service: switch.turn_{{ trigger.id }}
            entity_id: switch.e16s_output2
  - if:
      - condition: state
        entity_id: binary_sensor.input_3
        state: "off"
    then:
      - delay:
          hours: 0
          minutes: 5
          seconds: 0
          milliseconds: 0
      - service: switch.turn_off
        data: {}
        target:
          entity_id: switch.e16s_output2
    enabled: true
mode: single

I think you are misunderstanding how automations works in Home Assistant. The automation is event-driven, not looping continuously or waiting around for conditions to become true. If you want an automation to wait you have to tell it too do so by using one of the three Wait actions.

If you want the event of input_3’s state change to cause the pump to turn off, then that needs to be a trigger. Also, if you want input_3’s current state to affect whether other triggers cause the action to execute, you need to include it as a condition in the appropriate place.

Understanding Automations

trigger:
  - platform: state
    entity_id: binary_sensor.input_2
    to: "on"
    id: "off"
  - platform: state
    entity_id: binary_sensor.input_2
    to: "off"
    id: "on"
  - platform: state
    entity_id: binary_sensor.input_3
    to: "off"
    from: "on"
    for: "00:05:00"
  - platform: time
    id: "on"
    at:
      - "18:40"
      - "06:40"
  - platform: time
    id: time_off
    at:
      - "20:30"
      - "08:30"
condition:
  - condition: or
    conditions:
      - condition: trigger
        id: time_off
      - condition: time
        after: "09:40:00"
        before: "11:30:00"
      - condition: time
        after: "21:40:00"
        before: "23:30:00"
action:
  - choose:
      - alias: Turn off switch at end of time blocks and when input_3 turns off
        conditions:
          - condition: trigger
            id: time_off
        sequence:
          - service: switch.turn_off
            entity_id: switch.e16s_output2
      - alias: Option for sensor-based operation and start of time blocks
        conditions:
          - condition: state
            entity_id: binary_sensor.input_3
            state: "on"
          - condition: template
            value_template: >-
              {{ has_value('binary_sensor.input_2') and trigger.id !=
              states('binary_sensor.input_2') }}
        sequence:
          - service: switch.turn_{{ trigger.id }}
            entity_id: switch.e16s_output2
mode: queued
1 Like

Thank you for helping me correct my mistakes. Nowadays, I am learning about YAML and how it works in automation. Your suggested points have been really helpful for me. I aspire to become a scripting master like you.

I am sorry to bother you again, below code is not working properly - switch.e16s_output2 NOT “on”

looking for the below statement:-
when binary_sensor.input_2 is OFF turn ON the switch.e16s_output2 and when binary_sensor.input_2 is on turn off the switch.e16s_output2
between 6 to 11 pm and 6 am to 11 am
if
binary_sensor.input_3 is off for 5 minutes between 6 to 11 pm and 6 am to 11 am
Then turn off the switch.e16s_output2 again
and
Repeat the above automation after every 1 hr between 6 to 11 pm and 6 am to 11 am

Note - time change just to take more time for testing

alias: "Water-Auto-ON/off "
description: Water-Pump-Automation
trigger:
  - platform: state
    entity_id: binary_sensor.input_2
    to: "on"
    id: "off"
  - platform: state
    entity_id: binary_sensor.input_2
    to: "off"
    id: "on"
  - platform: state
    entity_id: binary_sensor.input_3
    to: "off"
    from: "on"
    for: "00:05:00"
  - platform: time
    id: "on"
    at:
      - "18:00"
      - "06:00"
  - platform: time
    id: time_off
    at:
      - "23:00"
      - "11:00"
condition:
  - condition: or
    conditions:
      - condition: trigger
        id: time_off
      - condition: time
        after: "06:00:00"
        before: "11:00:00"
      - condition: time
        after: "18:00:00"
        before: "23:00:00"
action:
  - choose:
      - alias: Turn off switch at end of time blocks and when input_3 turns off
        conditions:
          - condition: trigger
            id: time_off
        sequence:
          - service: switch.turn_off
            entity_id: switch.e16s_output2
      - alias: Option for sensor-based operation and start of time blocks
        conditions:
          - condition: state
            entity_id: binary_sensor.input_3
            state: "on"
          - condition: template
            value_template: >-
              {{ has_value('binary_sensor.input_2') and trigger.id !=
              states('binary_sensor.input_2') }}
        sequence:
          - service: switch.turn_{{ trigger.id }}
            entity_id: switch.e16s_output2
mode: queued

I don’t know if I’m misinterpreting what you have said or I just don’t understand what you are trying to do…

Here’s what I think I understand at this point:

  1. You are trying to fill a tank only during certain time slots of the day
  2. Those time slots are between the hours of 6-11 and 18-23.
  3. input_2 being on indicates the tank is full.
  4. input_3 being on indicates there is liquid at the source for the pump to draw into the tank.

So far, the following should be used to trigger this automation to stop pumping:

  1. No liquid at the source (input_3 turns off for 5 minutes)
  2. The end of a time slot (11:00 and 23:00)
  3. The tank becomes Full (input_2 changes to on)

Are there any other events that should be triggering this automation to stop pumping?

What events should be triggering this automation to start pumping?

Yes , you are correct
(input-2 indicate take is full)
(input-3 indicate there is liquid in the pipe to draw water into the tank)
(output-2 indicate water pump on/off)

What events should be triggering this automation to start pumping?

  1. when (input-2 to OFF)

Time

  1. hours of 6-11 and 18-23.

events that should be triggering this automation to stop pumping?

  1. when (input-2 change to ON)
  2. when (input-3 off for 5 minutes) — This is to avoid the water pump running without liquid
    mean - when output-2 change to ON if input-3 not change ON
    with a delay of 5 minutes, output-2 turn OFF
trigger:
  - alias: Turn pump off when tank is full
    platform: state
    entity_id: binary_sensor.input_2
    to: "on"
    id: "off"
  - alias: Turn pump on when tank is not full for 15 minutes
    platform: state
    entity_id: binary_sensor.input_2
    to: "off"
    for: "00:15:00"
    id: "on"
  - alias: Turn pump off if source pipe is empty for 5 minutes
    platform: state
    entity_id: binary_sensor.input_3
    to: "off"
    from: "on"
    for: "00:05:00"
    id: 'off'
  - alias: Turn pump on at the beginning of time slots
    platform: time
    id: "on"
    at:
      - "06:00"
      - "18:00"
  - alias: Turn pump off at the end of time slots
    platform: time
    id: 'off'
    at:
      - "11:00"
      - "23:00"
condition:
  - or:
      - condition: trigger
        id: 'off'
      - condition: time
        after: "06:00"
        before: "11:00:00"
      - condition: time
        after: "18:00:00"
        before: "23:00:00"
action:
  - choose:
      - alias: Option for 'on' triggers
        conditions:
          - condition: trigger
            id: 'on'
          - alias: Source must have water
            condition: state
            entity_id: binary_sensor.input_3
            state: "on"
          - alias: Tank must not be full
            condition: state
            entity_id: binary_sensor.input_2
            state: "off"
        sequence:
          - service: switch.turn_on
            entity_id: switch.e16s_output2
      - alias: Option 'off' triggers 
        conditions:
          - condition: trigger
            id: 'off'
        sequence:
          - service: switch.turn_off
            entity_id: switch.e16s_output2
mode: queued

Tested all, working as expected
Great to you!

1 Like