Automations not working properly

I have these two automations to turn on lights in two different hours of evening and night:

- alias: Accendi Luce Camera Letto Sera
  initial_state: on
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_158d0001a92ca1
    to: 'on'
  condition:
    condition: or
    conditions:
      - condition: template
        value_template: '{{ state_attr("sun.sun", "elevation") < -0.5 }}'
      - condition: time
        before: '01:00:00'
  action:
    - service: light.turn_on
      entity_id:
        - light.yeelight_3
        - light.yeelight_4
      data_template:
        transition: 2
        brightness: 255
        rgb_color: ['{{ (range(0, 255)|random) }}','{{ (range(0, 255)|random) }}','{{ (range(0, 255)|random) }}']
    - delay: 00:01:30
    - service: light.turn_off
      entity_id:
        - light.yeelight_3
        - light.yeelight_4
       
- alias: Accendi Luce Camera Letto Notte
  initial_state: on
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_158d0001a92ca1
    to: 'on'
  condition:
    - condition: time
      after: '01:00:00'
    - condition: sun
      before: 'sunrise'
      before_offset: "-00:30:00"
  action:
    - service: light.turn_on
      entity_id:
        - light.yeelight_4
      data_template:
        transition: 5
        brightness: 25
        rgb_color: ['{{ (range(0, 255)|random) }}','{{ (range(0, 255)|random) }}','{{ (range(0, 255)|random) }}']
    - delay: 00:00:30
    - service: light.turn_off
      entity_id:
        - light.yeelight_4

The first one is to turn on lights between the sunset and one o’clock in the night. The second between one o’clock and sunrise, but, while the first is running properly, the second one runs both of the automations and not as i want…

Where they are wrong?

Your first automation will trigger if the the time is before one o’clock in the night or if the elevation < -0.5.

Early in the morning, let’s say 3 o’clock your second automation will be triggered because it’s after 1 o’clock and before 30 minutes before sunris. However, your first automation will also be triggered because then the elevation is also < -0.5.

Thanks, so how to change that automation?

Try this:

- alias: Accendi Luce Camera Letto Sera
  initial_state: on
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_158d0001a92ca1
    to: 'on'
  condition:
    condition:
    conditions:
      - condition: sun
        after: 'sunset'
      - condition: time
        before: '01:00:00'
  action:
    - service: light.turn_on
      entity_id:
        - light.yeelight_3
        - light.yeelight_4
      data_template:
        transition: 2
        brightness: 255
        rgb_color: ['{{ (range(0, 255)|random) }}','{{ (range(0, 255)|random) }}','{{ (range(0, 255)|random) }}']
    - delay: 00:01:30
    - service: light.turn_off
      entity_id:
        - light.yeelight_3
        - light.yeelight_4
       
- alias: Accendi Luce Camera Letto Notte
  initial_state: on
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_158d0001a92ca1
    to: 'on'
  condition:
    - condition: time
      after: '01:00:00'
    - condition: sun
      before: 'sunrise'
      before_offset: "-00:30:00"
  action:
    - service: light.turn_on
      entity_id:
        - light.yeelight_4
      data_template:
        transition: 5
        brightness: 25
        rgb_color: ['{{ (range(0, 255)|random) }}','{{ (range(0, 255)|random) }}','{{ (range(0, 255)|random) }}']
    - delay: 00:00:30
    - service: light.turn_off
      entity_id:
        - light.yeelight_4

Do you also have an automation for the time between sunrise and sunset or you don’t want to turn on the light in this time?

No i want to turn on light on those rooms only at those times… thanks, i’ll try and let you know

what are you doing here? never seen that before. (or might it be a typo :wink: ) sorry if it is

if you’re ‘and’ ing these conditions:

condition:
  - condition: sun
    after: 'sunset'
  - condition: time
    before: '01:00:00'

would suffice?

Yeah the middle one is a spare wheel

So as burningstone pointed out your automations were working perfectly but doing what you asked, rather than what you wanted ?

Describe in words what you want to happen.

As i code in the automations i want, after 1 o’clock ,only one light (light.yeelight_3) will turn on when the motion sensor is triggered, but it happens that after 1 o’clock both lights are turned on. So i think @Burningstone is doing the right solution…
About the conditions @Mariusthvdb is right…

You are right this is a typo, thanks for pointing it out!

No, that’s not what you said in the automations.
And your description is also faulty
After 01:00 1 light
but after 01:00 two lights

You need to try to think like the machine will interpret
What is the Trigger ? the trigger is always the motion sensor (full stop - punto)
the conditions and the appropriate actions are another matter
So the first condition is sun lower than 0.5 deg and falling
The next condition is the sunrise -00:30:00
No action will take place outside these limits
So action’s will be ALWAYS to turn on yeelight3
AND … if its after 01:00 to also turn on yeelight4
Then wait 00:01:30
Then turn both lights off

Is that accurate ?
the time “slot’s” are slightly different for the two automations but if you can allign them (other than the 01:00:00 you could merge them into one automation. You have a solution though so just run with that.

Ok, i try to be clearer than before.
What i want is the following:

  1. Between sunset (or when the sun is lower than 0,5 deg and falling) and 1 o’clock in the night, both lights (yeelight 3 and yeelight 4) will turn on when motion sensor is triggered.
  2. Between 1 o’clock and sunrise (minus offset 30 minutes before sunrise) only one light (yeelight 3) will turn on when motion sensor is triggered.
  3. After 00:01:30 in the first automation both lights will turn off and after 00:00:30 in the second only the light i turned on (yeelight 3) will turn off.

Hope it’s clear now.

If you have a better solution to merge both automations in one, well i’ll be glad to use that.