Long, 12 hour, Automation Failing

I set both 23:29 and 23.30 just in case.

  alias: Front Door Sunset/Sunrise
  description: ''
  trigger:
  - platform: sun
    event: sunset
    offset: -01:00
  - platform: sun
    event: sunrise
    offset: -02:00
  - platform: time
    at: '23:30'
action:
  service: light.turn_on
  data:
    entity_id: light.outdoor_front
    brightness_pct: >
      {% if states('sensor.time') in ['23:29', '23:30']%}
        30
      {% elif is_state_attr('sun.sun', 'rising', true)%}
        0
      {% elif is_state_attr('sun.sun', 'rising', false)%}
        100
      {%endif %}

thanks,

am not sure i understand this one tho?

if you check the attributes from your sun.sun entity, from next_noon to next_midnight, the attribute rising is false. The other way around from midnight to noon, the rising turns true. That way you can distinguish which was the trigger of your automation. If the trigger was time, it’ll stop in the first if. If it wasn’t, it will check whether the sun is rising or not. If it is rising, it matches the sunrise trigger. If it’s not rising it will match the sunset trigger. Turning on a light to 0 brightness is the same as turning it off. You could do this too:

action:
  service: light.turn_on
  data:
    entity_id: light.outdoor_front
    brightness_pct: >
      {% if trigger.entity_id != 'sun.sun'%}
        30
      {% elif is_state_attr('sun.sun', 'rising', true)%}
        0
      {% elif is_state_attr('sun.sun', 'rising', false)%}
        100
      {%endif %}

We have trigger ids for that.

hmmm i find this a bit confusing and not sure what it achieves

alias: Example Sunrise Sunset Light
description: ''
trigger:
  - id: '100'
    platform: sun
    event: sunset
    offset: '-01:00:00'
  - id: '30'
    platform: time
    at: '23:30:00'
  - id: '0'
    platform: sun
    event: sunrise
    offset: '-02:00:00'
condition: []
action:
  - service: light.turn_on
    target:
      entity_id: light.outdoor_front
    data:
      brightness_pct: '{{ trigger.id }}'
mode: single
1 Like

@123 - Genius!

thanks

when i try and un this though i get this error:

Triggered manually at 17 January 2022, 20:15:02

Call service light.turn_on on entities light.outdoor_front

Stopped because an error was encountered at 17 January 2022, 20:15:02 (runtime: 0.02 seconds)

expected float for dictionary value @ data[‘brightness_pct’]

Try this.

brightness_pct: '{{ trigger.id | float(0) }}'

Creative (mis-) use of trigger id‘s.

Error: Error rendering data template: UndefinedError: ‘dict object’ has no attribute ‘id’

i am very close to giving up on this. frustrating the automation doesnt run as written with the tigger IDs

Well, instead of looking at the summary of what happened - try looking at the actual trace. The one that takes you through exactly what the automation did at each step, including the values that were used at the time. It will tell you in there exactly why the light was turned off after being turned on.

This is the trace… but unfortunately I just don’t understand it. Do you?

{
  "trace": {
    "last_step": "action/0/choose/0/sequence/0",
    "run_id": "9554505e96298cc913e72c7e3f664d98",
    "state": "stopped",
    "script_execution": "finished",
    "timestamp": {
      "start": "2022-01-17T15:23:34.722500+00:00",
      "finish": "2022-01-17T15:23:34.757291+00:00"
    },
    "domain": "automation",
    "item_id": "1641316866498",
    "trigger": "sunset with offset",
    "trace": {
      "trigger/0": [
        {
          "path": "trigger/0",
          "timestamp": "2022-01-17T15:23:34.723089+00:00",
          "changed_variables": {
            "this": {
              "entity_id": "automation.front_door_sun_sunset",
              "state": "on",
              "attributes": {
                "last_triggered": "2022-01-17T05:56:45.397052+00:00",
                "mode": "single",
                "current": 0,
                "id": "1641316866498",
                "friendly_name": "Front Door Sunset/Sunrise"
              },
              "last_changed": "2022-01-16T17:11:26.571421+00:00",
              "last_updated": "2022-01-17T05:56:45.420582+00:00",
              "context": {
                "id": "aa7737c32bb64a7c20aad2168b8bea4d",
                "parent_id": null,
                "user_id": null
              }
            },
            "trigger": {
              "id": "turnonsunset",
              "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-17T15:23:34.726029+00:00",
          "changed_variables": {
            "context": {
              "id": "8d8a3fce511d48af9afbc12212e49689",
              "parent_id": null,
              "user_id": null
            }
          },
          "result": {
            "choice": 0
          }
        }
      ],
      "action/0/choose/0": [
        {
          "path": "action/0/choose/0",
          "timestamp": "2022-01-17T15:23:34.726580+00:00",
          "result": {
            "result": true
          }
        }
      ],
      "action/0/choose/0/conditions/0": [
        {
          "path": "action/0/choose/0/conditions/0",
          "timestamp": "2022-01-17T15:23:34.726800+00:00",
          "result": {
            "result": true
          }
        }
      ],
      "action/0/choose/0/sequence/0": [
        {
          "path": "action/0/choose/0/sequence/0",
          "timestamp": "2022-01-17T15:23:34.733190+00:00"
        }
      ]
    },
    "config": {
      "id": "1641316866498",
      "alias": "Front Door Sunset/Sunrise",
      "description": "",
      "trigger": [
        {
          "platform": "sun",
          "event": "sunset",
          "offset": "-01:00",
          "id": "turnonsunset"
        },
        {
          "platform": "time",
          "at": "23:30",
          "id": "dim"
        },
        {
          "platform": "sun",
          "event": "sunrise",
          "offset": "-02:00",
          "id": "turnoffsunrise"
        }
      ],
      "condition": [],
      "action": [
        {
          "choose": [
            {
              "conditions": [
                {
                  "condition": "trigger",
                  "id": "turnonsunset"
                }
              ],
              "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": [
            {
              "type": "turn_off",
              "device_id": "03419912808df6849d866075b5de8421",
              "entity_id": "light.outdoor_front",
              "domain": "light"
            }
          ]
        }
      ]
    },
    "blueprint_inputs": null,
    "context": {
      "id": "8d8a3fce511d48af9afbc12212e49689",
      "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": "8d8a3fce511d48af9afbc12212e49689",
      "when": "2022-01-17T15:23:34.723735+00:00",
      "domain": "automation"
    },
    {
      "when": "2022-01-17T15:23:34.750862+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-17T15:23:34.782981+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"
    }
  ]
}

I can understand it yes, I can’t see any reason at all in there why the light is being turned off.
I would remove the ability for the automation to turn the light off at all, to rule out whether the automation is in fact responsible (it’s possible that it is being turned off from a different automation or integration).

The trace is quiet easy to read (and there is a visual representation available for it) and the automation does exactly what it is expected to do … except for this:

That‘s the last action, turning the light off almost immediately after it has been turned on. I don‘t get where this is coming from.

light.outdoor_front: Is this a single device (smart bulb, switch or similar)? Could it be faulty? I just lost an in-wall switch not being able to keep the internal relay. Even on manual key press, it turned on and off immediately.

1 Like

Yeah that’s my thought too, it’s either being turned off by something else, or there is a problem with the device. Although it appears in the logbook entries, there is no sign in the automation itself that it has done anything other than turn the light on.

Thanks !

It’s a Hue bulb

I added it via Deconz

It doesnt cause any errors elsewhere

And that’s the only automation it’s in. It’s only present in HA.

Also the fact that the turn off action is part of the trace confirms it is this automation doing it? Right?

Do not manually trigger automations to test them when the automation employs the trigger variable in its condition or action. Why? Because the trigger variable is undefined when the automation is triggered manually as opposed to when it’s triggered by one of its triggers.

This is explained here: Testing your automation

tl;dr
Example is fine. Failure is due to invalid testing technique.

Ahh ok that makes sense for that idea

But that implementation avoids the fact this doesn’t work as it should


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`Preformatted text`

I didn’t know about that. When was this implemented?