Time and Sun conditions always fail

Hi, why does this always fail when the Time or Sun conditions are present? I’m testing this now at 9:00pm, sun is set. HA 2021.5.x on RPi4.

Variant 1:

alias: Vibrate chair - work light on
description: ''
trigger:
  - platform: device
    domain: mqtt
    device_id: 0cc092f635104b10b306e786115bcaf7
    type: action
    subtype: vibration
    discovery_id: 0x00158d0002c4a72b action_vibration
condition:
  - condition: state
    entity_id: binary_sensor.hackintosh191_active
    state: 'on'
  - condition: time
    after: '18:00'
    before: '6:00'
action:
  - service: light.turn_on
    target:
      entity_id: light.e15_rgb
mode: single

Variant 2:

alias: Vibrate chair - work light on
description: ''
trigger:
  - platform: device
    domain: mqtt
    device_id: 0cc092f635104b10b306e786115bcaf7
    type: action
    subtype: vibration
    discovery_id: 0x00158d0002c4a72b action_vibration
condition:
  - condition: state
    entity_id: binary_sensor.hackintosh191_active
    state: 'on'
  - condition: sun
    before: sunrise
    after: sunset
action:
  - service: light.turn_on
    target:
      entity_id: light.e15_rgb
mode: single

Both work without the Sun and Time conditions. Please help.

EDIT: I should’ve added that I’m doing this in the UI Automation editor, I’m only sending the YAML generated by it for clarity.

Can you name a time of day that is both after sunset and before sunrise?
Likewise is there a time of day both after 18:00 and before 6:00?
I find that using the sun’s elevation is easier to get right. Elevation is negative = sun is below the horizon = after sunset one day and before sunrise the next day.

- alias: 'timing-Back door light on at night'
  trigger:
    platform: numeric_state
    entity_id: sun.sun
    attribute: elevation
    below: 0
  action:
    service: switch.turn_on
    entity_id: switch.back_door_motion_light
1 Like

Yes. Time conditions may span midnight.

Time condition windows can span across the midnight threshold if both after and before keys are used.
Conditions - Home Assistant

Because that can’t be valid, and that’s even called out in the documentation. You should instead check the state of sun.sun is below_horizon.

But that’s not what the automation editor offers. I have read the documentation.

EDIT: I should’ve added that I’m doing this in the UI Automation editor, I’m only sending the YAML generated by it for clarity.

I’m literally doing it by the book.

Thanks for the elevation tip!

But you aren’t reading the book correctly. :wink:

that chart is for use when you are using only one of those at a time.

if you use before and after together that means that both conditions need to be satisfied.

so if you chose after sunrise and before sunset it would work.

But if you use before sunrise and after sunset then it won’t work because it can never be both at the same time.

you need to use two “or” conditions - one for before sunrise or one for after sunset.

As far as the time condition not working I’m not sure why that would be the case. As far as I know t should work the way it is written.

There are no “or” conditions in the Automations editor, only via YAML.There is no way to do this with the Sun condition in the UI editor. That’s probably what threw me off. I’ll do it in YAML. Thanks.

The UI as seen in my screenshot is wrong then. It leads me to think when I set “after: sunset” and “before: sunrise”, it should work.or at least tell me there’s an issue. It’s just bad UX.

As for the time condition, I have no idea as well.

Screenshot 2021-06-03 at 14.25.30

Obviously :wink:

1 Like

Sorry but that’s even worse than I thought. Having parameters like Sun, Status and such mixed in the same menu as condition types such as AND, OR is a really, really bad UI - it’s just diffrent categories of things. I’d expect it to be between the different conditions, or on top, as an any other automation software.

AND it doesn’t solve the fact that the UI in my mobile screenshot above just doesn’t indicate what I’m trying can’t be done. Why is that UI that lets me designate BOTH Before and After even there, then? Either let me do what I want (which isn’t something unexpected, it’s just “do X in nighttime”) OR split the condition into separate After and Before.

I’m not doing anything weird here. I’m using the built-in functionality as presented. Sorry but that’s bad UX, no matter how you slice it.

Actually you are @qoheleth already explained this;

no, sorry, wrong. Moderator @tom_l said time conditions can span midnight. How else would you define night time?

And yes, I used the below horizon solution by @qoheleth, we’ll see if it works :slight_smile:

I agree this seems inconsistent, nevertheless it’s documented.

By using two conditions.

then the UI is, again, bad as that means night time can’t be defined by a single condition, so there’s no point in having both before/after and sunset/sunrise.

It’s incredibly difficult to devise good UI for complex systems with all sorts of conditions like HA, I don’t blame anyone. I’m just pointing out it’s confusing and unintuitive and maybe something devs could look into.

It’s a UI-ification of the way condition logic works:

Whether it’s ideal or not is another matter, but that’s why it’s the way it is.

Yes it can. This is the example given in the condition docs:

condition:
  alias: "Sun down"
  condition: state  # from sunset until sunrise
  entity_id: sun.sun
  state: "below_horizon"
3 Likes

I was talking about the UI editor, answering the notion that night time is defined by two conditions.

Sorry, should’ve prefaced with that.

Creating an automation with the Automation Editor or a text editor have something in common: they provide you with the freedom to compose utter nonsense.

The Automation Editor guards against common syntax errors (a text editor won’t, unless you use something like VS Code with a few plugins). However, neither have a “Home Assistant logic checker”; they allow you to create unsound logic (like an invalid Sunset/Sunrise Condition).

Anyway, the original question was answered many posts ago and the rest of the thread has become a
“I read the owner’s manual but the car let me drive into the ditch!” :man_shrugging:

2 Likes

Well, I don’t understand a point of having a special Sun condition in the editor that doesn’t allow for “sun is down”, I.e. one of the two possible states of Sun. It’s misleading and leads to futile threads like this. That’s all.

If you think it should work differently, post your suggestion as a Feature Request.