Long, 12 hour, Automation Failing

i want to create an automation for an outdoor light;

  1. turn on 1 hour before sunset to 100% brightness
  2. dim to 30% brightness at 23:30
  3. turn off 2 hours before sunrise

ideally i wanted to package this in a single automation - so its nice, neat, tidy.

but i find that while this automation works. i am finding that if i basically do any tinkering to home assistant between sunset and sunrise the automation is incredibly sensitive to aborting and doesn’t run through to the end. i can obviously split it into three seperate automations and then it is much more robust, but would be nice if it was all nicely contained in a single automation.

is that a bug of home assistant? (or a feature of HA? lol)


  alias: Front Door Sunset/Sunrise
  description: ''
  trigger:
  - platform: sun
    event: sunset
    offset: -01:00
  condition: []
  action:
  - type: turn_on
    device_id: 03419912808df6849d866075b5de8421
    entity_id: light.outdoor_front
    domain: light
    brightness_pct: 100
  - wait_for_trigger:
    - platform: time
      at: '23:30'
    continue_on_timeout: false
  - type: turn_on
    device_id: 03419912808df6849d866075b5de8421
    entity_id: light.outdoor_front
    domain: light
    brightness_pct: 30
  - wait_for_trigger:
    - platform: sun
      event: sunrise
      offset: -02:00
    continue_on_timeout: false
  - type: turn_off
    device_id: 03419912808df6849d866075b5de8421
    entity_id: light.outdoor_front
    domain: light
  mode: single
- id: '1641319084548'
  alias: Dim Front Door
  description: ''
  trigger:
  - platform: time
    at: '23:45'
  condition: []
  action:
  - type: turn_on
    device_id: 03419912808df6849d866075b5de8421
    entity_id: light.outdoor_front
    domain: light
    brightness_pct: 30
  mode: single

Split it into 2-3 nice, tidy and working automations.

This isn’t an efficient way of doing this automation because any reloading of the automations or restarting Home Assistant will cancel the automation.

What you want to do is have 3 triggers and then a choose action to pick what to do depending on the trigger.

For instance:

  alias: Front Door Sunset/Sunrise
  description: ''
  trigger:
  - platform: sun
    event: sunset
    offset: -01:00
    id: sunset
  - platform: time
    at: '23:30:00'
    id: time
  - platform: sun
    event: sunrise
    offset: -02:00
    id: sunrise

  condition: []

  action:
  - choose:
    - conditions:
      - condition: trigger
        id: sunset
      sequence:
      - type: turn_on
        device_id: 03419912808df6849d866075b5de8421
        entity_id: light.outdoor_front
        domain: light
        brightness_pct: 100

    - conditions:
      - condition: trigger
        id: time
      sequence:
      - type: turn_on
        device_id: 03419912808df6849d866075b5de8421
        entity_id: light.outdoor_front
        domain: light
        brightness_pct: 30

    - conditions:
      - condition: trigger
        id: sunrise
      sequence:
      - type: turn_off
        device_id: 03419912808df6849d866075b5de8421
        entity_id: light.outdoor_front
        domain: light
1 Like

Caveman lol

Genius!
Thanks a lot
Will try tonight :sunglasses:

this.

it’s by design according to the developers.

IMHO, it’s a poor design tho and I’ve done my best to try to bring attention to it and offer solutions but no one seems interested in fixing it.

I even tried to just add a more explicit note about it in the documentation as well but it’s been sitting in PR limbo for months.

This is the kind of situation that the TOD integration handles nicely.

  • Create 3 TOD sensors that define your times of day.
  • Trigger your automation when any of those sensors turn on
  • Also trigger it on a home assistant start
  • Have the automation choose which action to run based on the TOD sensor state.

You can trigger this automation any time (eg startup) and your light will always turn to the correct setting based on the TOD sensor state.

An additional benefit - these sensors are available to use in other automations as well!

Here is the TOD sensor definition.
binary_sensor:
  - platform: tod
    name: Early Morning
    after: sunset
    after_offset: "-01:00"
    before: "22:30"

  - platform: tod
    name: Night Time
    after: "22:30"
    before: sunrise
    before_offset: "-02:00"

  - platform: tod
    name: Day Time
    after: sunrise
    after_offset: "-02:00"
    before: sunset  
    before_offset: "-01:00"
Here's how that automation would look.
  alias: Front Door Sunset/Sunrise
  description: ''
  trigger:
  - platform: homeassistant
    event: start

  - platform: state
    entity_id:
      - binary_sensor.early_evening
      - binary_sensor.night_time
      - binary_sensor.day_time
    to: 'on'
  action:
  - choose:
      - conditions:
          - condition: state
            entity_id: binary_sensor.early_evening
            state: 'on'
        sequence:
          - type: turn_on
            device_id: 03419912808df6849d866075b5de8421
            entity_id: light.outdoor_front
            domain: light
            brightness_pct: 100

      - conditions:
          - condition: state
            entity_id: binary_sensor.night_time
            state: 'on'
        sequence:
          - type: turn_on
            device_id: 03419912808df6849d866075b5de8421
            entity_id: light.outdoor_front
            domain: light
            brightness_pct: 30

      - conditions:
          - condition: state
            entity_id: binary_sensor.day_time
            state: 'on'
        sequence:
          - type: turn_off
            device_id: 03419912808df6849d866075b5de8421
            entity_id: light.outdoor_front
            domain: light

You show an additional automation that turn the same outdoor front light to 30% again at 23:45. What is that automation for? You already set the light to 30% at 23:30.

At work so I can’t test this so please forgive any syntax errors!

1 Like

i have been trying this automation for my outdoor light with mixed success

it seems to work

but sometimes my light is turning on in the evening but then immediately turning off

the automation trace shows this, any tips?

{
  "trace": {
    "last_step": "action/0/choose/0/sequence/0",
    "run_id": "5c99200b1df3c7a3e3131521817557c1",
    "state": "stopped",
    "script_execution": "finished",
    "timestamp": {
      "start": "2022-01-14T15:18:51.891498+00:00",
      "finish": "2022-01-14T15:18:51.926061+00:00"
    },
    "domain": "automation",
    "item_id": "1641316866498",
    "trigger": "sunset with offset",
    "trace": {
      "trigger/0": [
        {
          "path": "trigger/0",
          "timestamp": "2022-01-14T15:18:51.891874+00:00",
          "changed_variables": {
            "this": {
              "entity_id": "automation.front_door_sun_sunset",
              "state": "on",
              "attributes": {
                "last_triggered": "2022-01-14T05:59:21.150697+00:00",
                "mode": "single",
                "current": 0,
                "id": "1641316866498",
                "friendly_name": "Front Door Sunset/Sunrise"
              },
              "last_changed": "2022-01-13T19:21:58.168131+00:00",
              "last_updated": "2022-01-14T05:59:21.179685+00:00",
              "context": {
                "id": "55e514f33ca596cb95bb58b986d3d2f2",
                "parent_id": null,
                "user_id": null
              }
            },
            "trigger": {
              "id": "sunset",
              "idx": "0",
              "platform": "sun",
              "event": "sunset",
              "offset": {
                "__type": "<class 'datetime.timedelta'>",
                "total_seconds": -3600
              },
              "description": "sunset with offset"
            }
          }
        }
      ],
      "action/0": [
        {
          "path": "action/0",
          "timestamp": "2022-01-14T15:18:51.894671+00:00",
          "changed_variables": {
            "context": {
              "id": "23cbe8d5c99d7f26fc9dd30110223fef",
              "parent_id": null,
              "user_id": null
            }
          },
          "result": {
            "choice": 0
          }
        }
      ],
      "action/0/choose/0": [
        {
          "path": "action/0/choose/0",
          "timestamp": "2022-01-14T15:18:51.895084+00:00",
          "result": {
            "result": true
          }
        }
      ],
      "action/0/choose/0/conditions/0": [
        {
          "path": "action/0/choose/0/conditions/0",
          "timestamp": "2022-01-14T15:18:51.895309+00:00",
          "result": {
            "result": true
          }
        }
      ],
      "action/0/choose/0/sequence/0": [
        {
          "path": "action/0/choose/0/sequence/0",
          "timestamp": "2022-01-14T15:18:51.901569+00:00"
        }
      ]
    },
    "config": {
      "id": "1641316866498",
      "alias": "Front Door Sunset/Sunrise",
      "description": "",
      "trigger": [
        {
          "platform": "sun",
          "event": "sunset",
          "id": "sunset",
          "offset": "-01:00"
        },
        {
          "platform": "time",
          "at": "23:30",
          "id": "dim"
        },
        {
          "platform": "sun",
          "event": "sunrise",
          "offset": "-02:00",
          "id": "sunrise"
        }
      ],
      "condition": [],
      "action": [
        {
          "choose": [
            {
              "conditions": [
                {
                  "condition": "trigger",
                  "id": "sunset"
                }
              ],
              "sequence": [
                {
                  "type": "turn_on",
                  "device_id": "03419912808df6849d866075b5de8421",
                  "entity_id": "light.outdoor_front",
                  "domain": "light",
                  "brightness_pct": 100
                }
              ]
            },
            {
              "conditions": [
                {
                  "condition": "trigger",
                  "id": "dim"
                }
              ],
              "sequence": [
                {
                  "type": "turn_on",
                  "device_id": "03419912808df6849d866075b5de8421",
                  "entity_id": "light.outdoor_front",
                  "domain": "light",
                  "brightness_pct": 30
                }
              ]
            },
            {
              "conditions": [
                {
                  "condition": "trigger",
                  "id": "sunrise"
                }
              ],
              "sequence": [
                {
                  "type": "turn_off",
                  "device_id": "03419912808df6849d866075b5de8421",
                  "entity_id": "light.outdoor_front",
                  "domain": "light"
                }
              ]
            }
          ],
          "default": []
        }
      ]
    },
    "blueprint_inputs": null,
    "context": {
      "id": "23cbe8d5c99d7f26fc9dd30110223fef",
      "parent_id": null,
      "user_id": null
    }
  },
  "logbookEntries": [
    {
      "name": "Front Door Sunset/Sunrise",
      "message": "has been triggered by sunset with offset",
      "source": "sunset with offset",
      "entity_id": "automation.front_door_sun_sunset",
      "context_id": "23cbe8d5c99d7f26fc9dd30110223fef",
      "when": "2022-01-14T15:18:51.892378+00:00",
      "domain": "automation"
    },
    {
      "when": "2022-01-14T15:18:51.918495+00:00",
      "name": "Outdoor Front",
      "state": "on",
      "entity_id": "light.outdoor_front",
      "context_entity_id": "automation.front_door_sun_sunset",
      "context_entity_id_name": "Front Door Sunset/Sunrise",
      "context_event_type": "automation_triggered",
      "context_domain": "automation",
      "context_name": "Front Door Sunset/Sunrise",
      "message": "turned on"
    },
    {
      "when": "2022-01-14T15:18:51.948669+00:00",
      "name": "Outdoor Front",
      "state": "off",
      "entity_id": "light.outdoor_front",
      "context_entity_id": "automation.front_door_sun_sunset",
      "context_entity_id_name": "Front Door Sunset/Sunrise",
      "context_event_type": "automation_triggered",
      "context_domain": "automation",
      "context_name": "Front Door Sunset/Sunrise",
      "message": "turned off"
    }
  ]
}

Triggered by the sunset with offset at 14 January 2022, 15:18:51

Choose: Option 1 executed

Outdoor Front (light.outdoor_front) turned on
Outdoor Front (light.outdoor_front) turned off

Finished at 14 January 2022, 15:18:51 (runtime: 0.04 seconds)

Can we see the automation in Yaml fomat, please. This JSON document is hard to read.

yeah sure - thats a copy of the trace when it errors

alias: Front Door Sunset/Sunrise
description: ''
trigger:
  - platform: sun
    event: sunset
    id: sunset
    offset: '-01:00'
  - platform: time
    at: '23:30'
    id: dim
  - platform: sun
    event: sunrise
    offset: '-02:00'
    id: sunrise
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: sunset
        sequence:
          - type: turn_on
            device_id: 03419912808df6849d866075b5de8421
            entity_id: light.outdoor_front
            domain: light
            brightness_pct: 100
      - conditions:
          - condition: trigger
            id: dim
        sequence:
          - type: turn_on
            device_id: 03419912808df6849d866075b5de8421
            entity_id: light.outdoor_front
            domain: light
            brightness_pct: 30
      - conditions:
          - condition: trigger
            id: sunrise
        sequence:
          - type: turn_off
            device_id: 03419912808df6849d866075b5de8421
            entity_id: light.outdoor_front
            domain: light
    default: []

Hmm … no idea, actually. I was expecting duplicate use of the same trigger id, but that is not the case.

To mitigate: How about making the 3rd option the default action (move it‘s sequence to default)?

i guess ? maybe worth a go. annoying odd behaviour.

alias: Front Door Sunset/Sunrise
description: ''
trigger:
  - platform: sun
    event: sunset
    id: sunset
    offset: '-01:00'
  - platform: time
    at: '23:30'
    id: dim
  - platform: sun
    event: sunrise
    offset: '-02:00'
    id: sunrise
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: sunset
        sequence:
          - type: turn_on
            device_id: 03419912808df6849d866075b5de8421
            entity_id: light.outdoor_front
            domain: light
            brightness_pct: 100
      - conditions:
          - condition: trigger
            id: dim
        sequence:
          - type: turn_on
            device_id: 03419912808df6849d866075b5de8421
            entity_id: light.outdoor_front
            domain: light
            brightness_pct: 30
    default:
      - condition: trigger
        id: sunrise
      - type: turn_off
        device_id: 03419912808df6849d866075b5de8421
        entity_id: light.outdoor_front
        domain: light

default with condition makes no sense to me…

eh?

so how would i get it to turn off at sunrise?

I can see absolutely no reason it should turn on the light and then off again.

Does it happen every time or just occasionally?

well like 50/50
not always but its not reliable at all

Your automation is triggered by sunrise, sunset and time („dim“).

The „conditioned“ options are sunset or time. If neither was the trigger, then it must be sunrise. Thus, no need to use a condition there.

duh! lol

obviously… yeah thanks. i am a bit slow today.

I don’t know what to suggest to be honest. Sorry. It seems weird that it only happens half the time!

I can only imagine that trigger ids ‘sunrise’ and ‘sunset’ collide with reserved keywords or something.