Automation Help - PIR lights with Time and state condition

Hello HA Gurus,
I am seeking for help. Maybe it is very simple but I am not able to find some working solution. I have automation for Night PIR lights which I would like to set conditions:

before midnight

  1. it should trigger only after 23:00:00, while sensor.period_of_day_newest is also Night
    after midnight
  2. it should trigger only when sensor.period_of_day_newest is Night

Time condition is valid only until midnight but also during that time sensor.period_of_day_newest is Night so they are overlapping before midnight.

  - condition: and
    conditions:
      - condition: time
        after: "23:00:00"
      - condition: state
        entity_id: sensor.period_of_day_newest
        state: night

Is there any way to combine time and state condition? I have sensor.period_of_day_newest which is calculating period of the day based on sun elevation.

Thanks a lot for help.
Lukas

That’s what your example does, it requires that the current time is after 23:00 and the sensor’s state is night.

Yes, but I assume my example is not complete. After midnight PIR lights will not work because time condition will return false. Right?

Add before: "06:00:00" to the Time Condition.

      - condition: time
        after: "23:00:00"
        before: "06:00:00

Maybe it’s just in my head, but 23:00 is usually night, The state “night” doesn’t make sense in this combination.
“time” goes from 00:00:00:00:01 ( or something like that ) to > 23.59.59 … , then it’s sort off start over ( new day ) so from 00:00:01-something until ( time of first condition ) nothing will happens

could you elaborate a little about this sensor ?

Ok, So this is whole automation.
So I want this automation to be working after 23:00:00 passing midnight but not before some fixed time (like 05:00:00) but till period_of_day_newest is Night or Dawn.

But I think before: “05:00:00” is anyway required there or let`sa say 07:00:00 which does not matter as sensor.period_of_day_newest will switch to Dawn or Day much earlier so it will be TRUE. So this is solution but I wondered if this can be done without “Before” condition.

alias: Obyvacka - PIR Svetlo - Night mode
description: ""
trigger:
  - type: motion
    platform: device
    device_id: 0b7a4aa5508c7a21689430ebe49bb438
    entity_id: binary_sensor.mi_pir_predsien_occupancy
    domain: binary_sensor
    id: motion-detected
  - type: no_motion
    platform: device
    device_id: 0b7a4aa5508c7a21689430ebe49bb438
    entity_id: binary_sensor.mi_pir_predsien_occupancy
    domain: binary_sensor
    for:
      hours: 0
      minutes: 2
      seconds: 0
condition:
  - condition: state
    entity_id: input_boolean.auto_lights_int_override
    state: "off"
  - condition: and
    conditions:
      - condition: device
        type: is_off
        device_id: 3855db4767508dcbeb9d40270091b522
        entity_id: light.obyvacka
        domain: light
      - condition: device
        type: is_off
        device_id: 581b8f14e91dbd6250cc1ccca79bba9b
        entity_id: light.jedalen
        domain: light
      - condition: device
        type: is_off
        device_id: e5b4daad23a83221f8dffa999f64dacd
        entity_id: light.hue_aurelle_panel_1
        domain: light
  - condition: and
    conditions:
      - condition: time
        after: "23:00:00"
        before: "05:00:00"
      - condition: state
        entity_id: sensor.period_of_day_newest
        state: night
action:
  - if:
      - condition: trigger
        id: motion-detected
    then:
      - service: light.turn_on
        data:
          transition: 2
          brightness_pct: 40
        target:
          entity_id: light.pillars
    else:
      - service: light.turn_off
        data: {}
        target:
          entity_id: light.pillars
mode: single

This is sensor I use:

    period_of_day_newest:
       friendly_name: Period of Day Newest
       value_template: >-
         {% if (as_timestamp(states.sun.sun.attributes.next_dusk)) - (as_timestamp(states.sun.sun.attributes.next_setting)) < 0 %}
           dusk
         {% elif (as_timestamp(states.sun.sun.attributes.next_rising)) - (as_timestamp(states.sun.sun.attributes.next_dawn)) < 0 %}
           dawn
         {% elif (states.sun.sun.attributes.elevation) < -5 %}
           night
         {% else %}
           day
         {% endif %}
       icon_template: >-
         {% if (as_timestamp(states.sun.sun.attributes.next_dusk)) - (as_timestamp(states.sun.sun.attributes.next_setting)) < 0 %}
           mdi:weather-sunset-down
         {% elif (as_timestamp(states.sun.sun.attributes.next_rising)) - (as_timestamp(states.sun.sun.attributes.next_dawn)) < 0 %}
           mdi:weather-sunset-up
         {% elif (states.sun.sun.attributes.elevation) < -5 %}
           mdi:weather-night
         {% else %}
           mdi:weather-sunny
         {% endif %}

For your application, you need before because you want the time range to start from 23:00 and extend beyond midnight.

It’s explained in the documentation for Time Condition:

Yep, clear for me now. Thanks a lot for help.

1 Like