Sensor.sun_next_dawn to trigger automation

I’d like to trigger an action to happen only between 10pm and next dawn. It’s not working, and I believe the problem is if I set it to happen after 10pm and before dawn, dawn translates to something like 7am (but not the next day), so the condition is to happen after 10pm, but before 7am. which is impossible.
How can I workaround this?

Post the condition you created.

For example, have you tried a Time Condition like this?

  - condition: time
    after: '22:00:00'
    before: sensor.sun_next_dawn

Exactly!

condition: time
after: "22:01:00"
weekday:
  - mon
  - tue
  - wed
  - thu
  - fri
  - sat
  - sun
before: sensor.sun_next_dawn

But as I said, sensor.sun_next_dawn seems to be the equivalent to 7:00:00, so it never triggers.

Can you post the whole automation?

The documentation explains that this means it spans midnight (i.e. from 22:00 today until 07:00 tomorrow).

  - condition: time
    after: "22:00:00"
    before: "07:00:00"

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

This should behave the same way.

  - condition: time
    after: "22:00:00"
    before: sensor.sun_next_dawn

If it doesn’t then it would be helpful to see how the Time Condition is used in your automation.

The idea is whenever I go to bed, I put my mobile in the charger, and that changes the lights at the patio:

alias: Lights patio sleep
description: ""
triggers:
  - trigger: state
    entity_id:
      - sensor.pgt_n19_charger_type
    from: null
    to: usb
conditions:
  - condition: time
    after: "22:01:00"
    before: sensor.sun_next_dawn
actions:
  - action: light.turn_on
    metadata: {}
    data:
      effect: Sunset
    target:
      area_id: patio
mode: single

The whole thing works perfectly until I add the before: sensor.sun_next_dawn condition (because I don’t want this to happen if I plug my mobile in during the day). So it’s definitely that what is preventing it to trigger.

Try the following Template Condition.

Nope, that won’t work (after midnight).

Try two logically ORed Time Conditions.

alias: Lights patio sleep
description: ""
triggers:
  - trigger: state
    entity_id:
      - sensor.pgt_n19_charger_type
    from: null
    to: usb
conditions:
  - condition: or
    conditions:
      - condition: time
        after: "22:01:00"
      - condition: time
        before: sensor.sun_next_dawn
actions:
  - action: light.turn_on
    metadata: {}
    data:
      effect: Sunset
    target:
      area_id: patio
mode: single

Thank you for your help, but it still doesn’t work.

But now I think the problem should be somewhere else:
If I test the condition individually, it passes:


(it’s currently 13:48 here)

But the whole automation doesn’t work, in fact there’s not even a trace (I’m not sure if there should be a trace, if not all conditions are met)

Which means the Time Condition works.

A trace is produced when the automation is triggered by one of its triggers. It doesn’t need to fulfill its conditions (or execute actions) to produce a trace.

Look at the recent history of sensor.pgt_n19_charger_type. Did its value recently change from some other state to usb?

Yes, looks like this sensor is not as reliable as I thought. Sometimes instead of usb, it changed to ac , and sometimes it’s not even changing.

Thank you for pointing me in the right direction.

1 Like