core: Sun trigger does not generate context

The problem

When an automation is triggered, normally the context is inherited from the trigger. This then flows into the parent_id of any effects caused by the automation which is useful to be able to determine if a state change was made as a result of an automation running.

Unfortunately some triggers, such as Sun and Time of Day do not appear to set a context. The result is that if the automation calls eg the light.turn_on service, the affected light entities have a parent_id of None making it appear that the state change of the light entities was not due to an automation.

The expected behaviour would be that if an automation is triggered and no context id is set from the trigger that the automation itself would generate a fresh context id.

Oddly in such cases the Logbook still correctly determines that the entity was changed by the automation though. I had previously thought this used the context to determine the source of a state change, but clearly this isn’t the case.

It could be that using the context.parent_id is not the right pattern to determine this and using whatever mechanism Logbook is is the right way, but looking at traces I can’t see any other values to indicate the source of the change so I’m thinking context is the only way.

What version of Home Assistant Core has the issue?

core-2022.3.3

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant Container

Integration causing the issue

No response

Link to integration documentation on our website

No response

Diagnostics information

No response

Example YAML snippet

No response

Anything in the logs that might be useful for us?

No response

Additional information

Output from an automation that was triggered by a light being turned off by an automation triggered by the sun. Note the from_state.context.id and to_state.context.id is unchanged, and in both cases context.parent_id is null.

I’d have expected the context.id to change every time a state change occurs.

trigger:
  id: '1'
  idx: '1'
  platform: state
  entity_id: light.white_light
  from_state:
    entity_id: light.white_light
    state: 'on'
    attributes:
      effect_list: []
      supported_color_modes:
        - onoff
      color_mode: onoff
      friendly_name: White Light
      supported_features: 4
    last_changed: '2022-03-12T10:31:17.462289+00:00'
    last_updated: '2022-03-12T10:31:17.462289+00:00'
    context:
      id: bf564e624c270a8221cc70dd5ca1e4ba
      parent_id: null
      user_id: null
  to_state:
    entity_id: light.white_light
    state: 'off'
    attributes:
      effect_list: []
      supported_color_modes:
        - onoff
      friendly_name: White Light
      supported_features: 4
    last_changed: '2022-03-12T12:31:17.550258+00:00'
    last_updated: '2022-03-12T12:31:17.550258+00:00'
    context:
      id: bf564e624c270a8221cc70dd5ca1e4ba
      parent_id: null
      user_id: null
  for: null
  attribute: null
  description: state of light.white_light

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 5
  • Comments: 26 (2 by maintainers)

Most upvoted comments

Still relevant for core 2024.1.2

Same for template triggers.