Light automation - HA shows as if the lights are on although they are not

Hi,

I have an automation that turns on lights when there is motion, and depending on time and sun position. The automation generally works, I have no issue with the automation itself. The lights get turned on every time during a specific time of the day, but a side effect and the problem is that for some reason home assistant shows as if the lights are being turned on every time of the day the motion sees the movement, even tough the lights do not actually get turned on (so the automation works in essence). I am not sure why this happen, but I assume I must be doing something incorrect.

alias: >-
  [BEDROOM][LIGHTS] Motion > bright lights before 20:13, and dark after, LED on
  weekends
description: >-
  Turns off lights with specific settings when motion is detected based on the
  time of day.
trigger:
  - platform: state
    entity_id:
      - binary_sensor.shelly_motion_5_motion
    to: "on"
condition: []
action:
  - choose:
      - conditions:
          - condition: and
            conditions:
              - condition: time
                after: "15:00:00"
                before: "20:12:59"
                weekday:
                  - sun
                  - sat
                  - fri
                  - thu
                  - wed
                  - tue
                  - mon
              - condition: sun
                after: sunset
        sequence:
          - service: light.turn_on
            data:
              kelvin: 3704
              brightness_pct: 100
            target:
              entity_id:
                - light.tradfri_group_sypialnia_korytarz
                - light.tradfri_group_sypialnia_nad_lozkiem
      - conditions:
          - condition: and
            conditions:
              - condition: time
                after: "20:13:00"
                before: "22:15:00"
                weekday:
                  - mon
                  - wed
                  - thu
                  - fri
                  - tue
              - condition: sun
                after: sunset
        sequence:
          - service: light.turn_on
            data:
              kelvin: 2252
              brightness_pct: 25
            target:
              entity_id:
                - light.tradfri_group_sypialnia_toaletka
                - light.tradfri_sypialnia_lozko_l_light
      - conditions:
          - condition: and
            conditions:
              - condition: time
                after: "20:13:00"
                before: "22:15:00"
                weekday:
                  - sat
                  - sun
              - condition: sun
                after: sunset
        sequence:
          - service: light.turn_on
            data:
              kelvin: 2252
              brightness_pct: 25
            target:
              entity_id:
                - light.tradfri_group_sypialnia_toaletka
                - light.tradfri_sypialnia_lozko_l_light
          - service: light.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: light.k22_switch_0
mode: single

It’s hard to tell if the conditions cover all times, but it could me that the light at 25% brightness seems off, while it is on.

The 2 conditions in the choose statement for basically an or once the trigger happens. The light on action appears the same.

I would re-arrange this.

  • Trigger: leave it
  • Conditions: add the 2 conditions as an or condition
  • Action: just put the service calls here

What this does is the actions don’t even run to leave the possibility of some artifact where the way you have it written the actions run and have the possibility to artifact that whatever you are seeing. It is also less duplication and easier to troubleshoot with the trace.

That won’t work. They have different brightness and color temps depending on the time of day and weekday for different lights. The only condition they can move to the main block is the after sunset condition, as it is common to all.

alias: >-
  [BEDROOM][LIGHTS] Motion > bright lights before 20:13, and dark after, LED on
  weekends
description: >-
  Turns off lights with specific settings when motion is detected based on the
  time of day.
trigger:
  - platform: state
    entity_id:
      - binary_sensor.shelly_motion_5_motion
    to: "on"
condition:
  - condition: sun
    after: sunset
action:
  - choose:
      - conditions:
          - condition: time
            after: "15:00:00"
            before: "20:12:59"
            weekday:
              - sun
              - sat
              - fri
              - thu
              - wed
              - tue
              - mon
        sequence:
          - service: light.turn_on
            data:
              kelvin: 3704
              brightness_pct: 100
            target:
              entity_id:
                - light.tradfri_group_sypialnia_korytarz
                - light.tradfri_group_sypialnia_nad_lozkiem
      - conditions:
          - condition: time
            after: "20:13:00"
            before: "22:15:00"
            weekday:
              - mon
              - wed
              - thu
              - fri
              - tue
        sequence:
          - service: light.turn_on
            data:
              kelvin: 2252
              brightness_pct: 25
            target:
              entity_id:
                - light.tradfri_group_sypialnia_toaletka
                - light.tradfri_sypialnia_lozko_l_light
      - conditions:
          - condition: time
            after: "20:13:00"
            before: "22:15:00"
            weekday:
              - sat
              - sun
        sequence:
          - service: light.turn_on
            data:
              kelvin: 2252
              brightness_pct: 25
            target:
              entity_id:
                - light.tradfri_group_sypialnia_toaletka
                - light.tradfri_sypialnia_lozko_l_light
          - service: light.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: light.k22_switch_0
mode: single

If the lights are not physically turning on check Settings → System-> Logs for errors.

The automation service calls look correct so it is possibly a communication problem with your lights or you are setting a Kelvin value outside the light’s abilities. Or some other problem with your light groups.

e.g. these appear to be groups rather than individual lights:

light.tradfri_group_sypialnia_toaletka
light.tradfri_group_sypialnia_korytarz
light.tradfri_group_sypialnia_nad_lozkiem

You can use Developer Tools → Services to test your service calls to the individual lights and to the groups of lights.

And in addition to this, please post the trace of the automation.

You can find it under Settings > Automations & scenes, search for the automation and click the three-dot-menu on the right, Traces. Download it by clicking another time the three-dot-menu in the upper right corner Download Trace.

Thank you all for the support. I implemented the change in the automation as suggested, that makes sense. I attach the trace of automation. Appreciate the support.

Trace:

{
  "trace": {
    "last_step": "condition/0",
    "run_id": "045847237064fd4fb276d8f9a2fe757d",
    "state": "stopped",
    "script_execution": "failed_conditions",
    "timestamp": {
      "start": "2024-07-02T16:20:53.486960+00:00",
      "finish": "2024-07-02T16:20:53.487793+00:00"
    },
    "domain": "automation",
    "item_id": "1716030195569",
    "trigger": "state of binary_sensor.shelly_motion_5_motion",
    "trace": {
      "trigger/0": [
        {
          "path": "trigger/0",
          "timestamp": "2024-07-02T16:20:53.487095+00:00",
          "changed_variables": {
            "this": {
              "entity_id": "automation.bedroom_lights_motion_bright_lights_before_20_13_and_dark_after_led_on_weekends",
              "state": "on",
              "attributes": {
                "id": "1716030195569",
                "last_triggered": "2024-07-01T20:20:43.112290+00:00",
                "mode": "single",
                "current": 0,
                "icon": "mdi:alarm-light-outline",
                "friendly_name": "[BEDROOM][LIGHTS] Motion > bright lights before 20:13, and dark after, LED on weekends"
              },
              "last_changed": "2024-06-28T13:59:16.415966+00:00",
              "last_reported": "2024-07-01T20:20:43.113872+00:00",
              "last_updated": "2024-07-01T20:20:43.113872+00:00",
              "context": {
                "id": "01J1QZ9XF7XW6QQBGA34WF1RG3",
                "parent_id": "01J1QZ9XF6MHZDK0MFG7E2ZXPC",
                "user_id": null
              }
            },
            "trigger": {
              "id": "0",
              "idx": "0",
              "alias": null,
              "platform": "state",
              "entity_id": "binary_sensor.shelly_motion_5_motion",
              "from_state": {
                "entity_id": "binary_sensor.shelly_motion_5_motion",
                "state": "off",
                "attributes": {
                  "device_class": "motion",
                  "friendly_name": "Shelly motion 5 motion"
                },
                "last_changed": "2024-07-02T15:19:18.596068+00:00",
                "last_reported": "2024-07-02T15:19:18.596068+00:00",
                "last_updated": "2024-07-02T15:19:18.596068+00:00",
                "context": {
                  "id": "01J1T0EQT47ECBSNH8ZDRS8054",
                  "parent_id": null,
                  "user_id": null
                }
              },
              "to_state": {
                "entity_id": "binary_sensor.shelly_motion_5_motion",
                "state": "on",
                "attributes": {
                  "device_class": "motion",
                  "friendly_name": "Shelly motion 5 motion"
                },
                "last_changed": "2024-07-02T16:20:53.485413+00:00",
                "last_reported": "2024-07-02T16:20:53.485413+00:00",
                "last_updated": "2024-07-02T16:20:53.485413+00:00",
                "context": {
                  "id": "01J1T3ZG3DRWNFRTMEJ8MRG13E",
                  "parent_id": null,
                  "user_id": null
                }
              },
              "for": null,
              "attribute": null,
              "description": "state of binary_sensor.shelly_motion_5_motion"
            }
          }
        }
      ],
      "condition/0": [
        {
          "path": "condition/0",
          "timestamp": "2024-07-02T16:20:53.487198+00:00",
          "result": {
            "wanted_time_after": "2024-07-02T19:25:32.685823+00:00",
            "result": false
          }
        }
      ]
    },
    "config": {
      "id": "1716030195569",
      "alias": "[BEDROOM][LIGHTS] Motion > bright lights before 20:13, and dark after, LED on weekends",
      "description": "Turns off lights with specific settings when motion is detected based on the time of day.",
      "trigger": [
        {
          "platform": "state",
          "entity_id": [
            "binary_sensor.shelly_motion_5_motion"
          ],
          "to": "on"
        }
      ],
      "condition": [
        {
          "condition": "sun",
          "after": "sunset"
        }
      ],
      "action": [
        {
          "choose": [
            {
              "conditions": [
                {
                  "condition": "and",
                  "conditions": [
                    {
                      "condition": "time",
                      "after": "15:00:00",
                      "before": "20:12:59",
                      "weekday": [
                        "sun",
                        "sat",
                        "fri",
                        "thu",
                        "wed",
                        "tue",
                        "mon"
                      ]
                    }
                  ]
                }
              ],
              "sequence": [
                {
                  "service": "light.turn_on",
                  "data": {
                    "kelvin": 3704,
                    "brightness_pct": 100
                  },
                  "target": {
                    "entity_id": [
                      "light.tradfri_group_sypialnia_korytarz",
                      "light.tradfri_group_sypialnia_nad_lozkiem"
                    ]
                  }
                }
              ]
            },
            {
              "conditions": [
                {
                  "condition": "and",
                  "conditions": [
                    {
                      "condition": "time",
                      "after": "20:13:00",
                      "before": "22:15:00",
                      "weekday": [
                        "mon",
                        "wed",
                        "thu",
                        "fri",
                        "tue"
                      ]
                    }
                  ]
                }
              ],
              "sequence": [
                {
                  "service": "light.turn_on",
                  "data": {
                    "kelvin": 2252,
                    "brightness_pct": 25
                  },
                  "target": {
                    "entity_id": [
                      "light.tradfri_group_sypialnia_toaletka",
                      "light.tradfri_sypialnia_lozko_l_light"
                    ]
                  }
                }
              ]
            },
            {
              "conditions": [
                {
                  "condition": "and",
                  "conditions": [
                    {
                      "condition": "time",
                      "after": "20:13:00",
                      "before": "22:15:00",
                      "weekday": [
                        "sat",
                        "sun"
                      ]
                    }
                  ]
                }
              ],
              "sequence": [
                {
                  "service": "light.turn_on",
                  "data": {
                    "kelvin": 2252,
                    "brightness_pct": 25
                  },
                  "target": {
                    "entity_id": [
                      "light.tradfri_group_sypialnia_toaletka",
                      "light.tradfri_sypialnia_lozko_l_light"
                    ]
                  }
                },
                {
                  "service": "light.turn_on",
                  "metadata": {},
                  "data": {},
                  "target": {
                    "entity_id": "light.k22_switch_0"
                  }
                }
              ]
            }
          ]
        }
      ],
      "mode": "single"
    },
    "blueprint_inputs": null,
    "context": {
      "id": "01J1T3ZG3EPCZQE00JAB44PP5C",
      "parent_id": "01J1T3ZG3DRWNFRTMEJ8MRG13E",
      "user_id": null
    }
  },
  "logbookEntries": []
}

This is where your automation fails, the first condition

According to the trace you posted, the time for sunset would be 19:25 (or 7:25pm), the automation ran at 16:20 (or 4:20pm).

Hi, excuse the late response.

But in essence, I want it to work after sunset. so it seems that the automation is correctly set up. Nevertheless, how do i fix it then in such case?