Time to sunrise automation

Hey all,

I’m sure I cant be the only one interested in this, but I cant find any posts on it.

I want to have a condition of 23:00 to sunrise. How can I do that?

You need to have two conditions with “OR”:
Condition one to cover from 23:00 to midnight: current time greather or equal to 23:00
OR
Condition two to cover from midnight to sunrise: sun.sun before sunrise…

condition:
  condition: or
  conditions:
    - condition: template
      value_template: '{{ (as_timestamp(now()) | timestamp_custom("%H:%M")) >= "23:00" }}'
    - condition: sun
      before: sunrise
2 Likes

Thanks for that! So it was just that its so simple that everyone gets it HAHA

condition:
  - condition: time
    after: '23:00'
    before: '00:00'
  - condition: or
    conditions:
      - condition: sun
        before: sunrise

Ill prob just put your one in, again thanks :slight_smile:But is this on the right track?

The OR condition must be on top (see my updated post…)
Your two conditions are correct as well… (the “'before ‘00:00’”’ is not necessary)

1 Like

Slightly simpler:

condition:
  condition: or
  conditions:
    - condition: time
      after: '23:00'
    - condition: sun
      before: sunrise

For the time condition if only one of after or before is used, the other end of the time range is midnight. However if both after and before are used the time range can span midnight.

You guys are great, thank you both :slight_smile:

Operators at the top of the condition…

Got ya,

    - condition: time
      after: '23:00'
    - condition: sun
      before: sunrise

So this is really, 23:00 till Midnight or Midnight till Sunrise

Correct.

1 Like

One more question on this automation if you have time?

image

In Webcore I would put both my actions in one Automation, like a routine.

Generally in HA, do you just have this as a separate automation?

Two automations would be simplest but there are ways to use only one.

trigger:  # triggers are OR logic, either trigger will activate the automation
  - platform: state
    entity_id: binary_sensor.motion
    to: 'on'
  - platform: binary_sensor.motion
    to: 'off'
    for:
      minutes: 5
action:
  - choose: 
      - conditions: # conditions are AND by default, so 'on" and 11pm to sunrise
          - condition: state
            entity_id: binary_sensor.motion
            state: 'on'
          - condition: or
            conditions:
              - condition: time
                after: '23:00'
              - condition: sun
                before: sunrise
        sequence:
          - service: light.turn_on
            entity_id: light.mirror_light
      - conditions:
          - condition: state
            entity_id: binary_sensor.motion
            state: 'off'
        sequence:
          - service: light.turn_off
            entity_id: light.mirror_light

More on script syntax here (automations are just scripts with triggers):