Need help understanding how and when conditions are evaluated

Hello! I’ve been learning how to use Home Assistant (via Hass.io) automations for a few weeks now, but I cannot seem to figure out how to get two related automations to coexist properly.

Each day, I have an automation that turns on my outside smart lights 15 minutes before sunset. It works perfectly, by itself. I also have another one that turns the same lights off before sunrise, but logically I’m considering these one thing.

I also have a separate automation that manages the colors of my outside lights that is holiday-specific. Most of the time the lights are just white (this automation is not in use for that), but when holiday seasons roll around, I like to have a little fun for the kids. The one I’m trying to get working now is for Halloween. It uses a time pattern to shift the lights back and forth between green and purple every six seconds. I’ve created two scenes that it just swaps back and forth between.

In the Halloween automation, I have a condition that the outside lights must be on for it to trigger. My initial assumption was that the condition would be checked when the time pattern trigger hit, and if the condition was false, the automation wouldn’t do anything. Then, the next time the trigger hit, it would check again. I have since learned, through experience, that my assumption is not accurate. If I turn that automation on when the lights are off, it never fires after I turn the lights on. If the lights are on when I turn the Halloween one on, it will turn the lights back on if I turn them off.

What I would like to do is to have my outside light automations do their things, and if I want to enable Halloween lights, I simply go into the web UI and turn that automation on. It would fire every six seconds after the sunset automation runs, and then once the sunrise automation turns the lights off, it would stop. To do that, I think I need to understand better how and when the conditions in a given automation are evaluated. Is it possible to force it to recheck that every time the time pattern hits? Or is it just a one-time evaluation when the automation is activated?

For the record, I’ve already set the sunrise and sunset automations to turn off and on, respectively, the Halloween automation. This requires me to change those automations and reload, though, rather than just quickly toggle something from the UI. It seems like this shouldn’t require quite that much effort to get working, but maybe that really is my best option.

Hopefully my rambling makes sense, but I’ll be happy to clarify anything that doesn’t. Thanks for your help!

Whoops. Here are the relevant configuration blocks:

  alias: Outside Halloween Lights
  trigger:
  - hours: '*'
    minutes: '*'
    platform: time_pattern
    seconds: /6
  condition:
  - condition: state
    entity_id: light.outside_light_1
    state: 'on'
  action:
  - data_template:
      entity_id: "{% if states.light.outside_light_1.attributes.rgb_color == (255,0,255)\
        \ %}\n  scene.halloween_outside_2 \n{% else %}\n  scene.halloween_outside_1\n\
        {% endif %}\n"
    service: scene.turn_on
  alias: Outside Sunset
  trigger:
  - event: sunset
    offset: -00:15:00
    platform: sun
  condition: []
  action:
  - data:
      brightness_pct: '100'
      entity_id:
      - light.outside_light_1
      - light.outside_light_2
      - light.outside_light_3
      - light.outside_light_4
      - light.outside_light_5
    service: light.turn_on

We need to see your scenes to help with this, I suspect that’s where the problem lies.
EDIT: I see that the halloween automation should only run if the light is already on, but we still should see the scenes so we have the full picture.

Couldn’t you just add another trigger to your Halloween automation for when the light turns on?

1 Like

I assume you’re referring to a state change trigger? Wouldn’t that only help the first time and not with when the lights turn off or the time between? Forgive my (still very deep) ignorance of all the options and strategies for configuring these automations. I feel like I’m still barely scratching the surface.

Here are the scenes:

- name: Halloween Outside 1
  entities:
    light.outside_light_1:
      state: 'on'
      rgb_color: [255,0,255]
      brightness: 255
      transition: 2
    light.outside_light_2:
      state: 'on'
      rgb_color: [0,255,0]
      brightness: 255
      transition: 2
    light.outside_light_3:
      state: 'on'
      rgb_color: [255,0,255]
      brightness: 255
      transition: 2
    light.outside_light_4:
      state: 'on'
      rgb_color: [0,255,0]
      brightness: 255
      transition: 2
- name: Halloween Outside 2
  entities:
    light.outside_light_1:
      state: 'on'
      rgb_color: [0,255,0]
      brightness: 255
      transition: 2
    light.outside_light_2:
      state: 'on'
      rgb_color: [255,0,255]
      brightness: 255
      transition: 2
    light.outside_light_3:
      state: 'on'
      rgb_color: [0,255,0]
      brightness: 255
      transition: 2
    light.outside_light_4:
      state: 'on'
      rgb_color: [255,0,255]
      brightness: 255
      transition: 2

Confirm the scenes run when you trigger this automation manually, via the Services page.

Your logic is flawed based on how lights work.

It looks like you are using transitions and when these lights turn off, there is a transition. During that transition, the light is still on. So when you press the button, if the transition is 2 seconds and the timepattern falls in that 2 seconds, the light will be reported as on. Then your scene will turn back on.

@petro, the on and off automations don’t use transitions, though. They just turn lights on and off immediately. Only the color shift automations use the transitions in the scenes, but those transitions only happen once the time pattern hits to trigger the scene. Since the time pattern is 6 seconds and the transition is 2, they can’t overlap. Right? If not, can you explain in more detail where my logic breaks down? I don’t really know what to change to try to make it work, short of removing the transitions from the scenes.

@123, the scenes and everything all work exactly as expected as long as I enable and disable them in the right order, by hand, each day. I just can’t make them handle the initial and final moments properly.

Thanks!

But your light has a natural transition. When you turn it off there is no dimming affect? It just goes straight off? In my experience dimmers by default do not do this, they always have a default transition. Home assistant reflects that state. So in the UI watch what the light does when you press the button. Is there a delay from when you click it off? What about when you click off on the lights switch itself, is there a delay in the UI for updating the state?

If your 6 section time pattern falls in that ‘off’ transition window that I’m describing, the light will still be on because it’s not fully off.

As for automations, conditions get resolved after it triggers. So based on your condition, the only thing making it fire is the fact that home assistant still thinks the light is on.

EDIT: You can test this by adding a persistent notification to the time pattern automation’s action section that tells you the full state of the light when that triggers. Mind you, you’ll get a notification every 6 seconds.

- service: persistent_notification.create
  data:
    message: >
      State {{ states('light.outside_light_1') }}
      Attributes {{ states.light.outside_light_1.attributes }}
    title: "Light state"

Ah, I follow what you mean now. I’ll look at that more specifically. I wasn’t aware there could be quite that much delay in state changes.

Thank you for clarifying when conditions get evaluated too. That helps quite a bit, in and of itself.

I know petros won’t may not like this, but
What I’d do is leave your sunset/sunraise automations as they are (the times/conditions are irrelevant).
I’d have inbut_boolean’s to set different effects.

  1. for christmas
  2. for halloween
  3. for my birthday
    etc.

then when the relevant boolean is turned on, it first forces all the others off, then the halloween automation just has the halloween bit set as the condition and runs till it is not there.

The bit petros won’t like is using an input boolean, but in this case he may allow it as it’s user input.
Though his agument against it and his subsequent suggestion may help in this case also.
Instead on triggereing at a time (for the lights at sunset - 15 mins and switching off at …
You create a binary_sensor that checks is before sunrise or after sunset -15mins then trigger your lights on that. This way the halloween condition could be extended to include this bit as well as the halloween enable. All of the time slots could be user configurable via input_time date or elevation or whatever as could the colours you set them to. more flexible and you only code it once (like you won’t be teaking in on a daily basis ; - ))) )
And you are not automating on something as vague as a light status

I’d probably do something like this to be honest. But I would probably configure a seasons sensor instead of having a boolean.

Less user interaction the better!

You have gotta love a guy that has such contempt for his user base.
And they all live in the same house as him. : - >>>>>>>>>>>>

I work in an industry where I automate machine tooling lines… More complication & buttons means more support. My goal in life is to reduce the need for support!

Yep,
I worked in electrical engineering, PLC control systems and dabbled on the IT side a bit too (via Automation Management and ERP/MRP integration).
Give a user 2 choices and they won’t just pick the wrong one they’ll find a 3rd option that’s even worse.

Small side note: whilst supporting a small user base in IT, we got a call from a guy who said his screen went dark. One of the guys went out, came back, took out a monitor, came back with a monitor, went out with another monitor, came back with same monitor, threw ‘stuff’ down on his desk and walked out for the day. We later learned (after he’d had his monitor changed twice) that the guy admitted to changing his desktop to black, his windows groups to black and his icons to black (this was early windows 95 or maybe stil 3.1)

After some more tinkering around, I went with the input_boolean option for now. I have actions in the sunrise and sunset automations to set that to on or off as appropriate (not exactly what @Mutt suggested, but it worked for now), and then the color rotation automations use that as a condition. Since it gets set more-or-less immediately, it seems (so far) to work more consistently than relying on the state of the lights themselves. I’ll have to give it more time to know for sure though.

All that said, the idea of a season sensor is awfully intriguing. I’m with @petro that the less human input on these automations, the better. I’ll explore that some more and, if necessary, create a new thread to get help. Thanks for the help, guys!

Whatever you are comfortable doing, hopefully this thread has got you thinking along automation lines and you’ll expand into other areas with new confidence. This means you’ll make a mistake and break things. That’s how I learn things, as after I’ve broken them, I have to fix them.
Happy Coding :+1: