Automation will not run past 12AM

thank you for the reply

Ok in the YAML I have 2 “After” conditions but only 1 “Before” would this break the time condition regarding spanning the midnight threshold if both “after and before” are used, would adding another before as shown below make this work

thanks

alias: DOWNSTAIRS HALL NIGHT MOTION
description: ""
trigger:
  - type: motion
    platform: device
    device_id:removed
    entity_id: removed
    domain: binary_sensor
    for:
      hours: 0
      minutes: 0
      seconds: 0
  - type: no_motion
    platform: device
    device_id: removed
    entity_id:removed
    domain: binary_sensor
    for:
      hours: 0
      minutes: 2
      seconds: 0
condition:
  - condition: sun
    after: sunset
    enabled: true
  - condition: time
    before: sensor.sun_next_rising
    weekday:
      - mon
      - wed
      - tue
      - thu
      - fri
      - sat
      - sun
    enabled: true
    after: "21:00:00"
    before: "09:00:00" <<<<<< do i need to add this 
action:
  - if:
      - type: is_motion
        condition: device
        device_id:removed
        entity_id:removed
        domain: binary_sensor
    then:
      - type: turn_on
        device_id:removed
        entity_id: removed
        domain: light
        brightness_pct: 10
    else:
      - type: turn_off
        device_id: removed
        entity_id: removed
        domain: light
mode: single

As per the documentation, the time range will span midnight if both after and before are specified.

So this spans midnight because it means a time range between 9:00 PM and 9:00 AM.

    after: "21:00:00"
    before: "09:00:00"
1 Like

ok perfect, thanks

Have you any idea why it didn’t it work before,

I have before and after used in first post, is it because there was 2 “after” conditions but only 1 “before”

In theory, that should have worked.

Copy-paste the following template into the Template Editor and report the results:

{{ states('sensor.sun_next_rising') }}
{{ states('sensor.sun_next_rising') | as_datetime | as_local }}

Speculation:

Perhaps there’s a bug in how the Time Condition computes the time range when sensor.sun_next_rising is used to define the range’s boundary. EDIT Not a bug. Behavior is due to the operation of the first Time Condition. See dbrunt’s explanation below.

1 Like

Multiple conditions default to ‘and’ unless you specifically use ‘or’ so in your first post, the first condition is after: sunset which defaults to before: midnight which results in ‘false’ after midnight so any other condition doesn’t matter. All conditions have to be 'true` to proceed.

2 Likes

Your two conditions do not make any sense to me, especially the second one with every day of the week specified!

I would just use an overall condition after: sunset & before: sunrise to prevent daytime processing and then in your actions use ‘if’ to determine what you want to do based on whether the time is before or after 9:00 pm.

1 Like
Copy-paste the following template into the Template Editor and report the results:

{{ states('sensor.sun_next_rising') }}
{{ states('sensor.sun_next_rising') | as_datetime | as_local }}
Result type: string
2023-12-30T08:48:41+00:00
2023-12-30 08:48:41+00:00
This template listens for the following state changed events:

Entity: sensor.sun_next_rising

Good point. There are two Time Conditions and the first one effectively reduces the overall time range. Therefore there’s no bug in the second condition and it’s the combination of the two Time Conditions that explains its ultimate behavior.

1 Like

Thanks but, as explained by dbrunt, your first example uses two Time Conditions. The second one spans midnight but the first one doesn’t. The two conditions are logically ANDed so that’s why the overall time range is limited by the first Time Condition.

I suggest you remove the first Time Condition and simply use the second one.

1 Like

Ok, So i’ve removed the extra condition and added the time condition to the if/then/else

i’ll try that tonight and see if it works

thanks very much

alias: testing ds pir night
description: ""
trigger:
  - type: motion
    platform: device
    device_id: REMOVED
    entity_id: REMOVED
    domain: binary_sensor
    for:
      hours: 0
      minutes: 0
      seconds: 0
  - type: no_motion
    platform: device
    device_id: REMOVED
    entity_id: REMOVED
    domain: binary_sensor
    for:
      hours: 0
      minutes: 2
      seconds: 0
condition:
  - condition: sun
    after: sunset
    enabled: true
    before: sunrise
action:
  - if:
      - type: is_motion
        condition: device
        device_id: REMOVED
        entity_id: REMOVED
        domain: binary_sensor
      - condition: time
        after: "21:00:00"
    then:
      - type: turn_on
        device_id: REMOVED
        entity_id: REMOVED
        domain: light
        brightness_pct: 10
    else:
      - type: turn_off
        device_id: REMOVED
        entity_id: REMOVED
        domain: light
mode: single

If you simply want to restrict the time range to night time, you can use a Sun Condition like this:

condition:
  - condition: state 
    entity_id: sun.sun
    state: below_horizon
2 Likes

If you’re not going to turn on the light at a different brightness after sunset & before 21:00 then there’s no need for the condition after sunset & before sunrise. Just use the condition after 21:00 & before sensor.sun_next_rising as your single primary condition.

It all depends on your overall desired behaviour.

i have 2 profiles depending on time to adjust brightness

indicates you different brightnesses that but there’s nothing in this automation dealing with that, but it certainly could.
Consider this automation which will

  1. turn on the lights at different brightnesses between sunset & surise when motion is detected
  2. turn on the lights at sunset if motion is already detected
  3. at 9:00 pm will change to reduced brightness if motion is already detected
  4. turn them off when motion is clear or at sunrise
alias: Living room automatic lighting
description: ""
mode: single
trigger:
  - platform: state
    entity_id:
      - binary_sensor.living_room_occupancy
    to: "on"
    id: Motion
  - platform: state
    entity_id:
      - binary_sensor.living_room_occupancy
    to: "off"
    id: No Motion
  - platform: sun
    event: sunrise
    offset: 0
    id: Sunrise
  - platform: sun
    event: sunset
    offset: 0
    id: Sunset
  - platform: time
    at: "21:00:00"
    id: 9:00 PM
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - Motion
              - Sunset
              - 9:00 PM
          - condition: state
            entity_id: binary_sensor.living_room_occupancy
            state: "on"
        sequence:
          - choose:
              - conditions:
                  - condition: sun
                    after: sunset
                  - condition: time
                    before: "21:00:00"
                sequence:
                  - service: light.turn_on
                    target:
                      entity_id: light.living_room_lights
                    data:
                      brightness_pct: 80
              - conditions:
                  - condition: time
                    after: "21:00:00"
                    before: sensor.sun_next_rising
                sequence:
                  - service: light.turn_on
                    target:
                      entity_id: light.living_room_lights
                    data:
                      brightness_pct: 20
      - conditions:
          - condition: trigger
            id:
              - No Motion
              - Sunrise
        sequence:
          - service: light.turn_off
            target:
              entity_id: light.living_room_lights
            data: {}
1 Like

It’s easier to maintain your automations if you avoid using type: turn_on as device_id: will undoubtedly change at some point like when a sensor fails and you have to replace it, which will require you to edit all automations that used the old one.
Using service: light.turn_on means you only have to ensure the new device ends up with same entity_id the old one was.

                sequence:
                  - service: light.turn_on
                    target:
                      entity_id: light.living_room_lights
                    data:
                      brightness_pct: 80
2 Likes

Thanks Daniel,

These posts couldn’t have came at a better time, I managed to get local-tuya installed and was worried all my automations would break when I attempt to change over from cloud tuya, I’ve done what you said regarding entities.

Before you had done this I managed to get 2 automations into 1 (basically the same automations with different time/brightness profiles as above) however, doing this with 2x IF/THEN statements wouldn’t work, one would switch on the lights and the other after 2 seconds would switch it off as shown below

alias: Downstairs Hall PIR DAY/NIGHT
description: ""
trigger:
  - type: motion
    platform: device
    device_id:removed
    entity_id: removed
    domain: binary_sensor
    for:
      hours: 0
      minutes: 0
      seconds: 0
  - type: no_motion
    platform: device
    device_id: removed
    entity_id: removed
    domain: binary_sensor
    for:
      hours: 0
      minutes: 2
      seconds: 0
condition:
  - condition: state
    entity_id: sun.sun
    state: below_horizon
action:
  - if:
      - type: is_motion
        condition: device
        device_id: removed
        entity_id: removed
        domain: binary_sensor
      - condition: time
        **after: "21:00:00"**
    then:
      - type: turn_on
        device_id: removed
        entity_id:removed
        domain: light
        brightness_pct: 10
    else:
      - type: turn_off
        device_id: removed
        entity_id: removed
        domain: light
  - if:
      - type: is_motion
        condition: device
        device_id: removed
        entity_id: removed
        domain: binary_sensor
      - condition: time
        **before: "21:00:00"**
    then:
      - type: turn_on
        device_id: removed
        entity_id: removed
        domain: light
        brightness_pct: 100
    else:
      - type: turn_off
        device_id:removed
        entity_id: removed
        domain: light
mode: single

so after that I used “choose between 2 actions” with 2 IF-THEN statements,
I did however keep sunset and sunrise rather than state below horizon because it is too dark for a while, while the sun sets

and then I went back and changed all the devices to entity ID’s

I’m not sure if your automation has benefits over the way i have done it? I did copy your automation into a new automation YAML and have a look at the visual editor but its well above my understanding for what’s going on

it currently works but i have not tested it past 9pm or 12am yet

thanks again

alias: new test downstairs hall PIR CHOOSE - IF /THEN SERVICES
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.downstairs_hall_motion_sensor_motion
    from: "off"
    to: "on"
  - platform: state
    entity_id:
      - binary_sensor.downstairs_hall_motion_sensor_motion
    from: "on"
    to: "off"
    for:
      hours: 0
      minutes: 2
      seconds: 0
condition:
  - condition: sun
    before: sunrise
    after: sunset
action:
  - choose:
      - conditions:
          - condition: time
            after: "21:00:00"
            before: "09:00:00"
        sequence:
          - if:
              - condition: state
                entity_id: binary_sensor.downstairs_hall_motion_sensor_motion
                state: "on"
            then:
              - service: light.turn_on
                target:
                  entity_id: light.hall_lights_light
                data:
                  brightness_pct: 10
            else:
              - service: light.turn_off
                data: {}
                target:
                  entity_id: light.hall_lights_light
      - conditions:
          - condition: time
            before: "21:00:00"
            after: "13:00:00"
        sequence:
          - if:
              - condition: state
                entity_id: binary_sensor.downstairs_hall_motion_sensor_motion
                state: "on"
                for:
                  hours: 0
                  minutes: 0
                  seconds: 0
            then:
              - service: light.turn_on
                data:
                  brightness_pct: 100
                target:
                  entity_id: light.hall_lights_light
            else:
              - service: light.turn_off
                target:
                  entity_id: light.hall_lights_light
                data: {}
mode: single

This automation looks okay to me but I prefer my style which I find easier to debug.
Your automation more or less done my way would be:

alias: new test downstairs hall PIR CHOOSE - IF /THEN SERVICES
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.downstairs_hall_motion_sensor_motion
    from: "off"
    to: "on"
    id: Motion
  - platform: state
    entity_id:
      - binary_sensor.downstairs_hall_motion_sensor_motion
    from: "on"
    to: "off"
    for:
      hours: 0
      minutes: 2
      seconds: 0
    id: No Motion
condition:
  - condition: sun
    before: sunrise
    after: sunset
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - Motion
        sequence:
          - if:
              - condition: time
                before: "21:00:00"
                after: "12:00:00"
            then:
              - service: light.turn_on
                target:
                  entity_id:
                    - light.hall_lights_light
                data:
                  brightness_pct: 100
            else:
              - service: light.turn_on
                target:
                  entity_id: light.hall_lights_light
                data:
                  brightness_pct: 10
      - conditions:
          - condition: trigger
            id:
              - No Motion
          - condition: state
            entity_id: light.hall_lights_light
            state: "on"
        sequence:
          - service: light.turn_off
            target:
              entity_id: light.hall_lights_light
            data: {}
mode: single

which I find easier to dig through & understand. It also has the benefit of being able to easily add to or change the motion triggers in one place if you need to, plus it’s 8 lines shorter than yours!

1 Like

Only one service call is needed if you calculate the required brightness in advance.

alias: example 
trigger:
  - platform: state
    entity_id: binary_sensor.downstairs_hall_motion_sensor_motion
    from: "off"
    to: "on"
  - platform: state
    entity_id: binary_sensor.downstairs_hall_motion_sensor_motion
    from: "on"
    to: "off"
    for:
      minutes: 2
condition:
  - condition: state
    entity_id: sun.sun
    state: below_horizon
action:
  - variables:
      is_on: "{{ trigger.to_state.state == 'on' }}"
      hour: '{{ now().hour }}'
      pct: >
        {{ 10 if is_on and (hour >= 21 or hour < 9)
          else 100 if is_on and 13 <= hour < 21
          else 0 }}
  - service: light.turn_on
    target:
      entity_id: light.hall_lights_light
    data:
       brightness_pct: '{{ pct }}'
mode: single
2 Likes

That’s cool!

Which one, of the several examples presented here, did you ultimately use?

sorry for late reply!

I ended up using your automation for downstairs lights

and dbrunts for upstairs :rofl:

I just make sure to keep every automation offered even if its disabled so i can use parts from them in the future

this part may be years into the future but :face_with_peeking_eye:

 pct: >
        {{ 10 if is_on and (hour >= 21 or hour < 9)
          else 100 if is_on and 13 <= hour < 21
          else 0 }}

thanks :+1:

You’re welcome!

Please consider marking my post above with the Solution tag. Only one post in the entire topic can have this tag. It leads other users directly to it by automatically placing a link in your first post and adding a checkmark to the topic’s title. All of this helps users find answers to similar questions.

For more information about the Solution tag, please refer to FAQ Guideline 21.

1 Like