Automation Condition Between Sunrise/OffSet Time Not Working

Hi,

I am trying to bring my old Hubitat automations into Home Assistant but I am having trouble with one of them. I am trying to get some lights to turn on in the morning between a certain time, AND only if sunrise is between a certain time (following a sensor activating). The first of these conditions returned true this morning, but not the Sunrise Condition. Can anyone let me know what I am doing wrong?

alias: Turn on Hall and Small Lounge Lamps Morning
description: ""
trigger:
  - type: motion
    platform: device
    device_id: 3fbe12760ae4a09f3e1b3ee03e85daf0
    entity_id: binary_sensor.hall_tri_sensor_motion
    domain: binary_sensor
condition:
  - condition: time
    after: "06:30:00"
    before: "08:30:00"
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
      - sat
      - sun
  - condition: and
    conditions:
      - condition: sun
        before: sunrise
        before_offset: "-01:30:00"
        after: sunrise
        after_offset: "00:30:00"
action:
  - service: light.turn_on
    data: {}
    target:
      device_id:
        - 5ff8ecb282bada3a7b4bcc92b9388979
        - 4a697147058334065b65ef5e646bec9d
        - bd74dff7c74670aacc9834b91191aeb0
mode: single

It can’t be both before AND after sunrise. Did you perhaps mean to use an OR condition?

Thanks for the reply, I am looking for the period being between Sunrise minus an hour and a half and sunrise plus thirty minutes, i.e. a two hour period based upon sunrise.

You can try elevation like this

condition:
  - condition: numeric_state
    entity_id: sun.sun
    below: 10
    attribute: elevation

It will turn on light around an hour or less before sunset and the same for sunrise.

condition:
  - condition: time
    after: "06:30:00"
    before: "08:30:00"
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
      - sat
      - sun
  - condition: sun
      before: sunrise
      before_offset: "-01:30:00"
      after: sunrise
      after_offset: "00:30:00"
1 Like

Tom, there is no explanation here. Will this test both conditions prior to enabling the automation? If so, what is the point of the “AND” statement within the conditions options?

The AND was superfluous.

OK, thanks. I have updated the code, I’ll see if it works in the morning (and in the future when the second condition is not met!)

Tried it this morning and it doesn’t work, the second condition is not met. Any ideas?

What time was sunrise?

What does the automation trace show?

Sunrise was 07:39 this morning. This is the trace from the trigger at 08:02, but it never operated at any stage in the previous hour.:

Paste the full trace.

OK, I’ll do it in the morning when the conditions should be met again. What do you mean by the full trace, show each of the tabs or is there something else I should click?

Click the 3 dots icon top right and select “download trace”.

Sorry, had to go into work yesterday so trigger was to early for me to get a trace. Here is the one from this morning:

{
  "trace": {
    "last_step": "condition/1",
    "run_id": "1d0e6155af1974469e59ed2295d196f9",
    "state": "stopped",
    "script_execution": "failed_conditions",
    "timestamp": {
      "start": "2022-10-19T06:19:07.715226+00:00",
      "finish": "2022-10-19T06:19:07.716462+00:00"
    },
    "domain": "automation",
    "item_id": "1665837658979",
    "trigger": "state of binary_sensor.hall_tri_sensor_motion",
    "trace": {
      "trigger/0": [
        {
          "path": "trigger/0",
          "timestamp": "2022-10-19T06:19:07.715381+00:00",
          "changed_variables": {
            "this": {
              "entity_id": "automation.turn_on_hall_and_small_lounge_lamps_morning",
              "state": "on",
              "attributes": {
                "last_triggered": null,
                "mode": "single",
                "current": 0,
                "id": "1665837658979",
                "friendly_name": "Turn on Hall and Small Lounge Lamps Morning"
              },
              "last_changed": "2022-10-17T10:24:36.688035+00:00",
              "last_updated": "2022-10-17T10:24:36.688035+00:00",
              "context": {
                "id": "01GFJQJMAGE84WKKF8HHHNBAFN",
                "parent_id": null,
                "user_id": null
              }
            },
            "trigger": {
              "id": "0",
              "idx": "0",
              "alias": null,
              "platform": "device",
              "entity_id": "binary_sensor.hall_tri_sensor_motion",
              "from_state": {
                "entity_id": "binary_sensor.hall_tri_sensor_motion",
                "state": "off",
                "attributes": {
                  "device_class": "motion",
                  "friendly_name": "Hall Tri-Sensor Motion"
                },
                "last_changed": "2022-10-19T06:12:37.689910+00:00",
                "last_updated": "2022-10-19T06:12:37.689910+00:00",
                "context": {
                  "id": "01GFQDYNNSZSS15D1WH66XM1BN",
                  "parent_id": null,
                  "user_id": null
                }
              },
              "to_state": {
                "entity_id": "binary_sensor.hall_tri_sensor_motion",
                "state": "on",
                "attributes": {
                  "device_class": "motion",
                  "friendly_name": "Hall Tri-Sensor Motion"
                },
                "last_changed": "2022-10-19T06:19:07.713386+00:00",
                "last_updated": "2022-10-19T06:19:07.713386+00:00",
                "context": {
                  "id": "01GFQEAJJ1VH8R8FRGFQZ8DKH0",
                  "parent_id": null,
                  "user_id": null
                }
              },
              "for": null,
              "attribute": null,
              "description": "state of binary_sensor.hall_tri_sensor_motion"
            }
          }
        }
      ],
      "condition/0": [
        {
          "path": "condition/0",
          "timestamp": "2022-10-19T06:19:07.715466+00:00",
          "result": {
            "after": {
              "__type": "<class 'datetime.time'>",
              "isoformat": "06:30:00"
            },
            "now_time": {
              "__type": "<class 'datetime.time'>",
              "isoformat": "07:19:07.715531"
            },
            "before": {
              "__type": "<class 'datetime.time'>",
              "isoformat": "08:30:00"
            },
            "result": true
          }
        }
      ],
      "condition/1": [
        {
          "path": "condition/1",
          "timestamp": "2022-10-19T06:19:07.715660+00:00",
          "result": {
            "wanted_time_before": "2022-10-19T05:11:18.984804+00:00",
            "result": false
          }
        }
      ]
    },
    "config": {
      "id": "1665837658979",
      "alias": "Turn on Hall and Small Lounge Lamps Morning",
      "description": "",
      "trigger": [
        {
          "type": "motion",
          "platform": "device",
          "device_id": "3fbe12760ae4a09f3e1b3ee03e85daf0",
          "entity_id": "binary_sensor.hall_tri_sensor_motion",
          "domain": "binary_sensor"
        }
      ],
      "condition": [
        {
          "condition": "time",
          "after": "06:30:00",
          "before": "08:30:00"
        },
        {
          "condition": "sun",
          "before": "sunrise",
          "before_offset": "-01:30:00",
          "after": "sunrise",
          "after_offset": "00:30:00"
        }
      ],
      "action": [
        {
          "service": "light.turn_on",
          "data": {},
          "target": {
            "device_id": [
              "5ff8ecb282bada3a7b4bcc92b9388979",
              "4a697147058334065b65ef5e646bec9d",
              "bd74dff7c74670aacc9834b91191aeb0"
            ]
          }
        }
      ],
      "mode": "single"
    },
    "blueprint_inputs": null,
    "context": {
      "id": "01GFQEAJJ3N0Y8Z2NJC7M3FJQF",
      "parent_id": "01GFQEAJJ1VH8R8FRGFQZ8DKH0",
      "user_id": null
    }
  },
  "logbookEntries": []
}

It is failing this condition:

        {
          "path": "condition/1",
          "timestamp": "2022-10-19T06:19:07.715660+00:00",
          "result": {
            "wanted_time_before": "2022-10-19T05:11:18.984804+00:00",
            "result": false
          }
        }
      ]

Which is this one:

          "condition": "sun",
          "before": "sunrise",
          "before_offset": "-01:30:00",

Thanks for the reply. It is failing the second condition, the time period around sunset. The first condition works (I have just tested). I suspect that condition 2 is not a period (i.e. return true if the period is 90 minutes before sunset until 30 minutes after), it would only report true for two points in time; the point at 90 minutes before and the point at 30 minutes after, but that is only a guess.

Yeah, that is not right. It is a time range:

  - condition: sun
      before: sunrise
      before_offset: "-01:30:00"
      after: sunrise
      after_offset: "00:30:00"

From the previous midnight to 90 minutes before sunrise and from 30 minutes after sunrise to the next midnight. Obviously that can never be true as those two time ranges do not overlap, and both have to be true because of the AND logic.

I suspect what you want is 90 minutes before sunrise to 30 minutes after sunrise. In which case do this:

condition:
  - condition: time
    after: "06:30:00"
    before: "08:30:00"
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
      - sat
      - sun
  - condition: sun
    before: sunrise
    before_offset: "00:30:00"
    after: sunrise
    after_offset: "-01:30:00" 

This will be from the previous midnight to 30 minutes after sunrise and from 90 minutes before sunrise to the next midnight. The only time those two time ranges overlap is 90 minutes before sunrise to 30 minutes after sunrise. Which is what you want.

Have a look at this (assuming sunrise is at 6am)

What you had:

1 Like

Thanks again, it took some thinking through, but I get the logic. I have amended and will see if it works in the morning.

Just checked it, and it worked. Thank you for your support.

1 Like