home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
73.89k stars 30.96k forks source link

Self referencing template select does not render state #117844

Open oeiber opened 6 months ago

oeiber commented 6 months ago

The problem

Self referencing template select does not render its state during startup.

The entity‘s state will remain „unknown“ until rereading template yaml using developer tools.

What version of Home Assistant Core has the issue?

core-2024.5.4

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant Supervised

Integration causing the issue

template select

Link to integration documentation on our website

https://www.home-assistant.io/integrations/template/

Diagnostics information

No response

Example YAML snippet

- select:
  - name: lwp01_mode
    unique_id: select.lwp01_mode
    options: '{{ ["Sommer", "Auto", "Urlaub", "Party", "2. Wärmeerzeuger"] }}'
    state:  '{{ this.attributes.options[states("sensor.lwp01_reading_mode") | int] }}'
    availability: '{{ states("sensor.lwp01_reading_mode") not in ["unknown", "unavailable"] }}'
    optimistic: true
    select_option:
    - service: modbus.write_register
      data:
        address: 5015
        slave: 1
        value: '{{ this.attributes.options.index(option) }}'
        hub: lwp01

Anything in the logs that might be useful for us?

2024-05-20 17:23:54.268 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: list object has no element 2 when rendering '{{ this.attributes.options[states("sensor.lwp01_reading_mode") | int] }}'

Additional information

No response

home-assistant[bot] commented 6 months ago

Hey there @phracturedblue, @tetienne, @home-assistant/core, mind taking a look at this issue as it has been labeled with an integration (template) you are listed as a code owner for? Thanks!

Code owner commands Code owners of `template` can trigger bot actions by commenting: - `@home-assistant close` Closes the issue. - `@home-assistant rename Awesome new title` Renames the issue. - `@home-assistant reopen` Reopen the issue. - `@home-assistant unassign template` Removes the current integration label and assignees on the issue, add the integration domain after the command. - `@home-assistant add-label needs-more-information` Add a label (needs-more-information, problem in dependency, problem in custom component) to the issue. - `@home-assistant remove-label needs-more-information` Remove a label (needs-more-information, problem in dependency, problem in custom component) on the issue.

(message by CodeOwnersMention)


template documentation template source (message by IssueLinks)

oeiber commented 4 months ago

can anybody confirm the issue?

mvaneijken commented 2 months ago

I can confirm this, having the same issue, but in this case referencing another entity. This is the test I did (based on this example):

- select:
    - name: "Porch Camera Day-Night Mode"
      unique_id: porch_camera_day_night_mode
      state: "{{ state_attr('camera.buienradar', 'friendly_name') }}"
      options: "{{ ['off', 'on', 'auto'] }}"
      select_option:
        - action: tts.google_say
          data:
            cache: false
            entity_id: media_player.nestaudio
            message: Test

This results in an unknown state: image