Delay a template from starting - errors on startup of HA

Hey Guys,

I am having this issue where I created a template to do some math from an attribute in an custom integration. The template works fine however when I restart HA, I get errors on this template because the integration takes longer to restart than template. Here is my code for the template:

  • sensor:
    - name: “Hot Tub Temp”
    unique_id: Hot Tub Temp
    unit_of_measurement: “°F”
    state_class: measurement
    device_class: temperature
    state: >
    {% set x = states.climate.my_spa_heater.attributes.current_temperature | float | round(0) %}

        {{ (x) * 9 / 5 + 32 | round(0) }}
    

any ideas on what i can do to delay the template from starting or change my code

- sensor:
      - name: "Hot Tub Temp"
        unique_id: Hot Tub Temp
        unit_of_measurement: "°F"
        state_class: measurement
        device_class: temperature
        state: >
          {% set x = states.climate.my_spa_heater.attributes.current_temperature | float | round(0) %}
          
          {{ (x) * 9 / 5 + 32 | round(0) }}

here was my code sorry about that first post. I did try and avoid doing states.xxx however this give errors or the value is unknown. If I do states( first as noted above, the template does not work. Also this variable is not a sensor but an attribute in a custom integration. I do the above with other templates and it works fine but those have sensor.xxx in them where as this attribute does not.

All I am trying to do is take an attribute inside the custom integration climate.my_spa_heater and convert it to Deg F as the attribute is in Deg C. Any code suggestions?

Try using state_attr() instead of states()

can you provide an example to my code above because if I try state() or state_attr() i get errors with the template. i did the above in my code state.xxx however I do understand this will give errors in startup.

{% set x = states.climate.my_spa_heater.attributes.current_temperature | float | round(0) %}

- sensor:
      - name: "Hot Tub"
        unique_id: Hot Tub Temp
        unit_of_measurement: "°F"
        state_class: measurement
        device_class: temperature
        state: >
          {% set x = state_attr('climate.my_spa_heater.attributes.current_temperature', 'Hot Tub Temp') | float(0)  %}
          
          {{ (x) * 9 / 5 + 32 | round(0) }}

I tried this in the developer-template area to see how the numbers play out. I got the template to work however I get the incorrect math. it gives me a value of 32.0 where i should be around 39. I noticed changing the float(0) to anything other than 0 increases the result of my template (tried 1, 2, 3 or 4) however i am not sure what this does. Currently my raw attribute shows 38.9 Deg C so my template should indicate at the end result 102.02 Deg F

- sensor:
      - name: "Hot Tub"
        unique_id: Hot Tub Temp
        unit_of_measurement: "°F"
        state_class: measurement
        device_class: temperature
        state: >
          {% set x = state_attr('climate.my_spa_heater', 'current_temperature') | float(0)  %}
          
          {{ (x) * 9 / 5 + 32 | round(0) }}

The number in the parenthesis is the default to use if it’s not able convert the value to a float. This is why you’re getting 32 – it’s not a math error, it’s because 0° C is 32° F.

I would guess maybe the attribute name is not quite right? In the dev tools template tab, start with just

{{ state_attr('climate.my_spa_heater.attributes.current_temperature', 'Hot Tub Temp') }}

and experiment with that until it’s giving you the right value.

Francis,

I did your code and its working perfectly. I cleared my logs and rebooted home assistant. no more errors on startup.

This fixed my issue. Will see how it goes so far