Repeat until time in automation stops earlier than the scheduled time

Hi,
I cann’ot understand why this automation stops at midnight instead 2:30 …

alias: XMAS Lamp Light V2.0
description: ""
trigger:
  - platform: time
    at: "17:10:00"
condition: []
action:
  - repeat:
      sequence:
        - service: light.turn_on
          target:
            device_id: 5cd5474060c9ae06cc5f6405b16798c3
          data:
            rgb_color: "{{ states('input_select.color_vector') }}"
            brightness: 255
        - delay:
            hours: 0
            minutes: 0
            seconds: 5
            milliseconds: 0
        - service: input_select.select_next
          target:
            entity_id: input_select.color_vector
          data:
            cycle: true
      until:
        - condition: time
          before: "02:30:00"
  - service: light.turn_off
    target:
      device_id: 5cd5474060c9ae06cc5f6405b16798c3
    data: {}
mode: single

I guess there is “something” related to midnight… but I cann’ot understand …

Thanks for support.

Because every time from midnight forward until 2:29am is before 2:30am.

As for your original question, your until condition is wrong. It should use the after property rather than the before property.


I will recommend to use two separate triggers with trigger ID set,

17:00 as start_time
and 02:30 as end_time.

Then add two blocks of if-then blocks checking for trigger ID.

If trigger ID is 17:00, then turn it on and set the colour.

If trigger ID is 02:30, then turn it off.

This way, the script wouldn’t have to run without stopping from 17:00 - 02:30.

You can do a similar check for changing colours. If you have the start and end time as helpers then you could conditionally check using a template and a new trigger for “seconds place of the time % 5 is 0” as the template condition, then if that trigger is true, cycle the colour.


I’ve made a simple blueprint to help you with this:

Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

It’s a quick draft so you may want to adjust some stuff with the conditions if it’s buggy in some cases.

Example usage

input_boolean.test_toggle would be your light.

alias: ⏱️ Scheduled Interval - Example
description: ""
use_blueprint:
  path: samuelthng/ad_hoc_scheduled_interval.yaml
  input:
    start_time: "17:00:00"
    start_action:
      - service: light.turn_on
        target:
          entity_id:
            - input_boolean.test_toggle
        data:
          rgb_color: "{{ states('input_select.color_vector') }}"
          brightness: 255
    end_time: "02:30:00"
    end_action:
      - service: light.turn_off
        target:
          entity_id:
            - input_boolean.test_toggle
    interval_entity:
      - input_select.color_vector
      - input_boolean.test_toggle
    interval_action:
      - service: input_select.select_next
        target:
          entity_id: input_select.color_vector
        data:
          cycle: true
      - service: light.turn_on
        target:
          entity_id: input_boolean.test_toggle
        data:
          rgb_color: "{{ states('input_select.color_vector') }}"
          brightness: 255

Hi thanks for blueprint…
I have imported succesfully and create the animation as you suggested… it starts… but won’t cycle for colour change.

I have just replaced the entity input_boolean.test_toggle with my rgb bulb id.

From trace it seems that no if condition is matched after the first run (after 5 seconds)

{
  "trace": {
    "last_step": "action/2/if/condition/1",
    "run_id": "9ef4b4e909b3e0dd72e415e2a76fb675",
    "state": "stopped",
    "script_execution": "finished",
    "timestamp": {
      "start": "2023-12-28T11:15:06.297103+00:00",
      "finish": "2023-12-28T11:15:06.300177+00:00"
    },
    "domain": "automation",
    "item_id": "1703760612682",
    "trigger": "state of light.smart_rgb_bulb_2304123204813952050848e1e9c43242_none",
    "trace": {
      "trigger/2": [
        {
          "path": "trigger/2",
          "timestamp": "2023-12-28T11:15:06.297250+00:00",
          "changed_variables": {
            "this": {
              "entity_id": "automation.xmas_lamp_light_v3_0",
              "state": "on",
              "attributes": {
                "id": "1703760612682",
                "last_triggered": "2023-12-28T11:15:00.091027+00:00",
                "mode": "single",
                "current": 0,
                "friendly_name": "XMAS Lamp Light V3.0"
              },
              "last_changed": "2023-12-28T11:13:31.465068+00:00",
              "last_updated": "2023-12-28T11:15:00.496311+00:00",
              "context": {
                "id": "01HJR24ZVTFRWRYW1RJH9W84ED",
                "parent_id": null,
                "user_id": null
              }
            },
            "trigger": {
              "id": "Change Interval",
              "idx": "2",
              "alias": null,
              "platform": "state",
              "entity_id": "light.smart_rgb_bulb_2304123204813952050848e1e9c43242_none",
              "from_state": {
                "entity_id": "light.smart_rgb_bulb_2304123204813952050848e1e9c43242_none",
                "state": "off",
                "attributes": {
                  "min_color_temp_kelvin": 2695,
                  "max_color_temp_kelvin": 6535,
                  "min_mireds": 153,
                  "max_mireds": 371,
                  "supported_color_modes": [
                    "color_temp",
                    "rgb"
                  ],
                  "color_mode": null,
                  "brightness": null,
                  "color_temp_kelvin": null,
                  "color_temp": null,
                  "hs_color": null,
                  "rgb_color": null,
                  "xy_color": null,
                  "togglex_mode": null,
                  "icon": "mdi:coach-lamp-variant",
                  "friendly_name": "Smart Bulb None",
                  "supported_features": 0
                },
                "last_changed": "2023-12-28T11:08:26.848952+00:00",
                "last_updated": "2023-12-28T11:08:26.848952+00:00",
                "context": {
                  "id": "01HJR1RZGGQYE6804HQBATY4BW",
                  "parent_id": null,
                  "user_id": "3d68936c685f480f9cda400b4ed43f0a"
                }
              },
              "to_state": {
                "entity_id": "light.smart_rgb_bulb_2304123204813952050848e1e9c43242_none",
                "state": "on",
                "attributes": {
                  "min_color_temp_kelvin": 2695,
                  "max_color_temp_kelvin": 6535,
                  "min_mireds": 153,
                  "max_mireds": 371,
                  "supported_color_modes": [
                    "color_temp",
                    "rgb"
                  ],
                  "color_mode": "rgb",
                  "brightness": 255,
                  "color_temp_kelvin": null,
                  "color_temp": null,
                  "hs_color": [
                    0,
                    100
                  ],
                  "rgb_color": [
                    255,
                    0,
                    0
                  ],
                  "xy_color": [
                    0.701,
                    0.299
                  ],
                  "togglex_mode": null,
                  "icon": "mdi:coach-lamp-variant",
                  "friendly_name": "Smart Bulb None",
                  "supported_features": 0
                },
                "last_changed": "2023-12-28T11:15:01.293920+00:00",
                "last_updated": "2023-12-28T11:15:01.293920+00:00",
                "context": {
                  "id": "01HJR24ZVTFRWRYW1RJH9W84ED",
                  "parent_id": null,
                  "user_id": null
                }
              },
              "for": {
                "__type": "<class 'datetime.timedelta'>",
                "total_seconds": 5
              },
              "attribute": null,
              "description": "state of light.smart_rgb_bulb_2304123204813952050848e1e9c43242_none"
            }
          }
        }
      ],
      "action/0": [
        {
          "path": "action/0",
          "timestamp": "2023-12-28T11:15:06.298507+00:00",
          "changed_variables": {
            "context": {
              "id": "01HJR255XS2J25422A93DPZ5JY",
              "parent_id": "01HJR24ZVTFRWRYW1RJH9W84ED",
              "user_id": null
            }
          }
        }
      ],
      "action/0/if": [
        {
          "path": "action/0/if",
          "timestamp": "2023-12-28T11:15:06.298581+00:00",
          "result": {
            "result": false
          }
        }
      ],
      "action/0/if/condition/0": [
        {
          "path": "action/0/if/condition/0",
          "timestamp": "2023-12-28T11:15:06.298608+00:00",
          "result": {
            "result": false
          }
        }
      ],
      "action/1": [
        {
          "path": "action/1",
          "timestamp": "2023-12-28T11:15:06.298669+00:00"
        }
      ],
      "action/1/if": [
        {
          "path": "action/1/if",
          "timestamp": "2023-12-28T11:15:06.298699+00:00",
          "result": {
            "result": false
          }
        }
      ],
      "action/1/if/condition/0": [
        {
          "path": "action/1/if/condition/0",
          "timestamp": "2023-12-28T11:15:06.298717+00:00",
          "result": {
            "result": false
          }
        }
      ],
      "action/2": [
        {
          "path": "action/2",
          "timestamp": "2023-12-28T11:15:06.298759+00:00"
        }
      ],
      "action/2/if": [
        {
          "path": "action/2/if",
          "timestamp": "2023-12-28T11:15:06.298785+00:00",
          "result": {
            "result": null
          }
        }
      ],
      "action/2/if/condition/0": [
        {
          "path": "action/2/if/condition/0",
          "timestamp": "2023-12-28T11:15:06.298802+00:00",
          "result": {
            "result": true
          }
        }
      ],
      "action/2/if/condition/1": [
        {
          "path": "action/2/if/condition/1",
          "timestamp": "2023-12-28T11:15:06.298824+00:00",
          "error": "In 'time' condition: unknown 'after' entity input_datetime.test_start_time"
        }
      ]
    },
    "config": {
      "mode": "single",
      "trigger": [
        {
          "platform": "time",
          "at": "12:15:00",
          "id": "Start Time"
        },
        {
          "platform": "time",
          "at": "02:30:00",
          "id": "End Time"
        },
        {
          "platform": "state",
          "entity_id": [
            "input_select.color_vector",
            "light.smart_rgb_bulb_2304123204813952050848e1e9c43242_none"
          ],
          "for": {
            "hours": 0,
            "minutes": 0,
            "seconds": 5
          },
          "id": "Change Interval"
        }
      ],
      "condition": [],
      "action": [
        {
          "if": [
            {
              "condition": "trigger",
              "id": [
                "Start Time"
              ]
            }
          ],
          "then": [
            {
              "service": "light.turn_on",
              "target": {
                "entity_id": [
                  "light.smart_rgb_bulb_2304123204813952050848e1e9c43242_none"
                ]
              },
              "data": {
                "rgb_color": "{{ states('input_select.color_vector') }}",
                "brightness": 255
              }
            }
          ]
        },
        {
          "if": [
            {
              "condition": "trigger",
              "id": [
                "End Time"
              ]
            }
          ],
          "then": [
            {
              "service": "light.turn_off",
              "target": {
                "entity_id": [
                  "light.smart_rgb_bulb_2304123204813952050848e1e9c43242_none"
                ]
              },
              "data": {}
            }
          ]
        },
        {
          "if": [
            {
              "condition": "trigger",
              "id": [
                "Change Interval",
                "Start Time"
              ]
            },
            {
              "condition": "time",
              "after": "input_datetime.test_start_time",
              "before": "input_datetime.test_end_time"
            }
          ],
          "then": [
            {
              "service": "input_select.select_next",
              "target": {
                "entity_id": "input_select.color_vector"
              },
              "data": {
                "cycle": true
              }
            },
            {
              "service": "light.turn_on",
              "target": {
                "entity_id": "light.smart_rgb_bulb_2304123204813952050848e1e9c43242_none"
              },
              "data": {
                "rgb_color": "{{ states('input_select.color_vector') }}",
                "brightness": 255
              }
            }
          ]
        }
      ],
      "id": "1703760612682",
      "alias": "XMAS Lamp Light V3.0",
      "description": ""
    },
    "blueprint_inputs": {
      "id": "1703760612682",
      "alias": "XMAS Lamp Light V3.0",
      "description": "",
      "use_blueprint": {
        "path": "samuelthng/ad_hoc_scheduled_interval.yaml",
        "input": {
          "start_time": "12:15:00",
          "start_action": [
            {
              "service": "light.turn_on",
              "target": {
                "entity_id": [
                  "light.smart_rgb_bulb_2304123204813952050848e1e9c43242_none"
                ]
              },
              "data": {
                "rgb_color": "{{ states('input_select.color_vector') }}",
                "brightness": 255
              }
            }
          ],
          "end_time": "02:30:00",
          "end_action": [
            {
              "service": "light.turn_off",
              "target": {
                "entity_id": [
                  "light.smart_rgb_bulb_2304123204813952050848e1e9c43242_none"
                ]
              },
              "data": {}
            }
          ],
          "interval_entity": [
            "input_select.color_vector",
            "light.smart_rgb_bulb_2304123204813952050848e1e9c43242_none"
          ],
          "interval_action": [
            {
              "service": "input_select.select_next",
              "target": {
                "entity_id": "input_select.color_vector"
              },
              "data": {
                "cycle": true
              }
            },
            {
              "service": "light.turn_on",
              "target": {
                "entity_id": "light.smart_rgb_bulb_2304123204813952050848e1e9c43242_none"
              },
              "data": {
                "rgb_color": "{{ states('input_select.color_vector') }}",
                "brightness": 255
              }
            }
          ]
        }
      }
    },
    "context": {
      "id": "01HJR255XS2J25422A93DPZ5JY",
      "parent_id": "01HJR24ZVTFRWRYW1RJH9W84ED",
      "user_id": null
    }
  },
  "logbookEntries": [
    {
      "name": "XMAS Lamp Light V3.0",
      "message": "triggered by state of light.smart_rgb_bulb_2304123204813952050848e1e9c43242_none",
      "source": "state of light.smart_rgb_bulb_2304123204813952050848e1e9c43242_none",
      "entity_id": "automation.xmas_lamp_light_v3_0",
      "context_id": "01HJR255XS2J25422A93DPZ5JY",
      "when": 1703762106.297359,
      "domain": "automation"
    }
  ]
}

Hey @lor74,

Weird because it worked for my short 5 minute test.

Could you add a service call to select next in the start automation as well? That should forcefully trigger the interval loop.

Also be sure to double check that the entity id used for the color_vector selector is right as well.

If all else fails, you can still use your automation, but remember to update this:

      until:
        - condition: time
          after: "02:30:00" # Make sure this is 'after' not 'before'

Thanks again @ samuelthng.
Today I will check what you suggested. Yes, now I have understood the condition until … and why after is wrong :slight_smile:.
I will try also this change… but in this case I guess that it will stop at the first cycle since when the automation starts at 17:00:00 it’s after 02:30:00 right? Anyway I will try…

1 Like