Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nested trigger based templated is unavailable #70036

Closed
Mariusthvdb opened this issue Apr 14, 2022 · 6 comments
Closed

nested trigger based templated is unavailable #70036

Mariusthvdb opened this issue Apr 14, 2022 · 6 comments

Comments

@Mariusthvdb
Copy link
Contributor

Mariusthvdb commented Apr 14, 2022

The problem

as title

What version of Home Assistant Core has the issue?

2022.5.0.dev20220414

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant OS

Integration causing the issue

template

Link to integration documentation on our website

https://next.home-assistant.io/integrations/template/#trigger-based-template-sensors

Diagnostics information

No response

Example YAML snippet

template:

  - trigger:
      - platform: time_pattern
        # This will update every night
        hours: 0
        minutes: 0
      - platform: homeassistant
        event: start
      - platform: event
        event_type: event_template_reloaded

    sensor:
 
      - unique_id: vandaag
        name: Vandaag
        state: >
          {{states('sensor.dag')}} {{states('sensor.current_day')}} {{states('sensor.maand')}} {{states('sensor.year')}}
        icon: mdi:calendar-today

      - unique_id: dag
        name: Dag
        state: >
          {% set dagen = ['Maandag','Dinsdag','Woensdag','Donderdag','Vrijdag',
                          'Zaterdag','Zondag'] %}
          {% set dag = dagen[now().weekday() ] %}
          {{dag}}

      - unique_id: maand
        name: Maand
        state: >
          {% set maanden = ['Januari','Februari','Maart','April','Mei','Juni','Juli',
                            'Augustus','September','Oktober','November','December'] %}
          {% set maand = maanden[now().month -1] %}
          {{maand}}

      - unique_id: current_day
        name: Current day
        state: >
          {{now().day}}

      - unique_id: year
        name: Year
        state: >
          {{now().year}}

Anything in the logs that might be useful for us?

No response

Additional information

Schermafbeelding 2022-04-14 om 10 23 38

more-info shows dots

Schermafbeelding 2022-04-14 om 10 23 49

using:

      - unique_id: vandaag
        name: Vandaag
        state: >
          {{states('sensor.dag')}} {{now().day}} {{states('sensor.maand')}} {{now().year}}
        icon: mdi:calendar-today

does work correctly:

Schermafbeelding 2022-04-14 om 10 32 57

more-info:

Schermafbeelding 2022-04-14 om 10 33 41

@probot-home-assistant
Copy link

template documentation
template source
(message by IssueLinks)

@probot-home-assistant
Copy link

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!
(message by CodeOwnersMention)

@Mariusthvdb
Copy link
Contributor Author

Mariusthvdb commented Apr 14, 2022

same seems to happen for:

  - trigger:
      - platform: time_pattern
        hours: 0
        minutes: 0
      - platform: homeassistant
        event: start
      - platform: event
        event_type: event_template_reloaded

    binary_sensor:

      - unique_id: christmas_time
        name: Christmas time
        state: >
          {% set today = now().day %}
          {% set month = now().month %}
          {{month == 12 and today >= 24 or month == 1 and today <= 2}}
        icon: >
          mdi:{{iif(is_state('binary_sensor.christmas_time','on'),'string-lights','pine-tree')}}

or without self referencing:

        icon: >
          {% set today = now().day %} {% set month = now().month %}
          {% if month == 12 and today >= 24 or month == 1 and today <= 2 %} mdi:string-lights
          {% else %} mdi:pine-tree
          {% endif %}

which is correctly created at startup, but turns unknown on reloading template entities. Even a manual update entity doesn't change the sate of the icon correctly, so why is this different from event homeassistant start?

@Didgeridrew
Copy link

Didgeridrew commented Apr 30, 2022

core 2022.04.7
supervisor 2022.04.0
Home Assistant OS 7.6

I am experiencing a similar issue with trigger-based template sensors. Whenever there is a templated attribute that references the sensor's state, the state returns "unavailable" and all attributes except friendly_name disappear.

template:
  - trigger:
      - platform: homeassistant
        event: start
      - platform: event
        event_type: event_template_reloaded
    sensor:
      - name: "New Years Day"
        state: >
          {%- set today = now().date() %}
          {% set new_years = today.replace(month=1, day=1) %}
          {% if new_years < now().date() %}
          {% set new_years = today.replace(month=1, day=1, year= (now().year + 1)) %}
          {% endif %}
          {{ strptime(new_years, "%y-%m-%d") }}
        attributes:
          trash_holiday: "off"
          days_until: >
            {{ ((states('sensor.new_years_day')|as_datetime|as_local).date() - now().date()).days }}

issue_triggered_template_sensor

The state and all attributes work normally when configured as a state-based sensor.

template:
  - sensor:
      - name: "New Years state based"
        unique_id: "3731505170994201405206"
        state: >
          {%- set today = now().date() %}
          {% set new_years = today.replace(month=1, day=1) %}
          {% if new_years < now().date() %}
          {% set new_years = today.replace(month=1, day=1, year= (now().year + 1)) %}
          {% endif %}
          {{ strptime(new_years, "%y-%m-%d") }}
        attributes:
          trash_holiday: "off"
          days_until: >
            {{ ((states('sensor.new_years_state_based')|as_datetime|as_local).date() - now().date()).days }}

working_state_based_template_sensor

@Didgeridrew
Copy link

core 2022.05.1

Update: The issue also occurs when using the new this variable in trigger-based template entities.

@github-actions
Copy link

github-actions bot commented Aug 4, 2022

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates.
Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍
This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.

@github-actions github-actions bot added the stale label Aug 4, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Sep 17, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants