Cannot check against unknown state

I got the following automation with which I want to toggle my LED lights in the kitchen when I turn on my kitchen lights on the ceiling. It works well but when Home Assistant gets restarted because I updated it for example the LED lights will always toggle. So I thought a condition could help but it does not help.

trigger:
  - platform: state
    entity_id:
      - light.kuche_deckenlichter
condition:
  - condition: template
    value_template: "{{ state_attr('trigger.entity_id', 'from_state.state') != 'unknown' }}"
action:
  - type: toggle
    device_id: f1f18ee3fd89b0521a04b334946c7d24
    entity_id: light.kuche_led
    domain: light
mode: single

If I have a look into the changed variables in the run that is causing the automation to run I see the following (from_state_state is unknown as you can see, so what is wrong):

this:
  entity_id: automation.kuche_an_led_band_an
  state: 'on'
  attributes:
    last_triggered: '2023-07-03T14:44:35.129135+00:00'
    mode: single
    current: 0
    id: '1655848910187'
    friendly_name: Küche an = LED Band an (und aus)
  last_changed: '2023-07-03T14:45:58.871231+00:00'
  last_updated: '2023-07-03T14:45:58.871231+00:00'
  context:
    id: 01H4E3FBYQCY122J619APPF1YN
    parent_id: null
    user_id: null
trigger:
  id: '0'
  idx: '0'
  alias: null
  platform: state
  entity_id: light.kuche_deckenlichter
  from_state:
    entity_id: light.kuche_deckenlichter
    state: unknown
    attributes:
      effect_list:
        - blink
        - breathe
        - candle
        - channel_change
        - finish_effect
        - okay
        - stop_effect
        - stop_hue_effect
      supported_color_modes:
        - brightness
      entity_id:
        - light.hue_filament_fenster
        - light.hue_filament_mitte
        - light.hue_filament_tur
      icon: mdi:lightbulb-group
      friendly_name: Küche Deckenlichter
      supported_features: 44
    last_changed: '2023-07-03T14:49:30.154471+00:00'
    last_updated: '2023-07-03T14:49:30.154471+00:00'
    context:
      id: 01H4E3NT9AY178NZ5HFACNBT43
      parent_id: null
      user_id: null
  to_state:
    entity_id: light.kuche_deckenlichter
    state: 'off'
    attributes:
      effect_list:
        - blink
        - breathe
        - candle
        - channel_change
        - finish_effect
        - okay
        - stop_effect
        - stop_hue_effect
      supported_color_modes:
        - brightness
      entity_id:
        - light.hue_filament_fenster
        - light.hue_filament_mitte
        - light.hue_filament_tur
      icon: mdi:lightbulb-group
      friendly_name: Küche Deckenlichter
      supported_features: 44
    last_changed: '2023-07-03T14:50:03.195349+00:00'
    last_updated: '2023-07-03T14:50:03.195349+00:00'
    context:
      id: 01H4E3PTHSR0T0WHGKJBN4E3JT
      parent_id: null
      user_id: null
  for: null
  attribute: null
  description: state of light.kuche_deckenlichter
trigger:
  - platform: state
    entity_id:
      - light.kuche_deckenlichter
    from:
      - 'off'
      - 'on'
    to:
      - 'on'
      - 'off'
condition: []
action:
  - type: toggle
    device_id: f1f18ee3fd89b0521a04b334946c7d24
    entity_id: light.kuche_led
    domain: light
mode: single

Specify the exact state-changes you want the State Trigger to detect (on → off and off → on). All other state-changes will be ignored (including any changes to the value of the light’s attributes).

References:
State Trigger

Works well but why is it not possible to test it like I tried?

Because the Template Condition you created is invalid. In the state_attr() function, 'from.state' is not the name of an attribute.

Perhaps what you were attempting to do is this:

condition:
  - condition: template
    value_template: "{{ trigger.from_state.state != 'unknown' }}"

However, that wouldn’t be completely effective because your State Trigger would also trigger for changes in the value of the light’s attributes.

Another alternative; it only triggers for state-changes (not attribute changes) and not if the previous state was unknown.

trigger:
  - platform: state
    entity_id:
      - light.kuche_deckenlichter
    not_from:
      - unknown
1 Like