Trying to test template

Trying pirate weather and wanting to calculate enthalpy and found yaml code with the formula but it isn’t working. Code is:

template:
  - sensor:
      - name: "Outdoor Humidity"
        unique_id: outdoor_humidity_pirateweather
        unit_of_measurement: "%"
        device_class: humidity
        state_class: measurement
        state: "{{ state_attr('weather.pirateweather', 'humidity') }}"    
        
  - sensor:
      - name: "Enthalpy Outdoor"
        unique_id: enthalpy_outdoor
        state_class: "measurement"
        unit_of_measurement: "BTU/lb"
        state: >
          {% set T = states('sensor.kaln_temperature') | float(none) %}
          {% set RH = states('sensor.outdoor_humidity_pirateweather') | float(none) %}
          
          {% if T is not none and RH is not none %}
            {# Calculate saturation vapor pressure and enthalpy using standard psychrometric formulas #}
            {% set Psat = 0.000000007401234 * T**4 - 0.000000493526794 * T**3 + 0.000071281097208 * T**2 - 0.000489806163078 * T + 0.039762055806989 %}
            {% set Pw = (RH / 100.0) * Psat %}
            {% set W = 0.62198 * Pw / (14.696 - Pw) %}
            {{ (0.24 * T + W * (1061.2 + 0.444 * T)) | round(2) }}
          {% else %}
            unavailable
          {% endif %}

I know the first block to get outdoor humidity works as I can display it in a card. To figure out where the issue with the enthalpy calculation block I tried to put parts into the template form under tools..

Entered:

states('sensor.outdoor_humidity_pirateweather')

and got this message:
This template does not listen for any events and will not update automatically.

float(none) should be float(0). Copied the code from somewhere. Maybe this was an old way to do it? It works now.

The real cause is the else rendering “unavailable”… setting a default of 0 for the float filters only masks it, and means the sensor will never be “unavailable” anyway. It will just blithely give you incorrect values… and, it makes the else pointless.

It would be better to use a proper availability template.

Also, Pirateweather should already provide a standalone humidity sensor, so your template sensor is likely redundant. Maybe you disabled it at some point?

template:
  - sensor:
      - name: "Enthalpy Outdoor"
        unique_id: enthalpy_outdoor
        state_class: "measurement"
        unit_of_measurement: "BTU/lb"
        state: >
          {% set T = states('sensor.kaln_temperature') | float %}
          {% set RH = states('sensor.pirateweather_humidity') | float %}
          {# Calculate saturation vapor pressure and enthalpy using standard psychrometric formulas #}
          {% set Psat = 0.000000007401234 * T**4 - 0.000000493526794 * T**3 + 0.000071281097208 * T**2 - 0.000489806163078 * T + 0.039762055806989 %}
          {% set Pw = (RH / 100.0) * Psat %}
          {% set W = 0.62198 * Pw / (14.696 - Pw) %}
          {{ (0.24 * T + W * (1061.2 + 0.444 * T)) | round(2) }}
        availability: |
          {{ has_value('sensor.kaln_temperature') and has_value('sensor.pirateweather_humidity') }}  

EDIT: Removed extraneous if line that Edwin_D pointed out below.

I still have some errors. It returns a number but not a correct one. I think the problem is in the pirate weather humidity value. If I use:
state: “{{ state_attr(‘weather.pirateweather’, ‘humidity’) }}”
it returns 47 which is the proper value.
If I enter
{{states(‘sensor.kaln_temperature’)}}
I get 80.4 which is also corrent
if I enter
{{states(‘sensor.pirateweather_humidity’)|float(0)}}
it returns zero.

Thanks for your other comments but it appears pirate weather does not provide humidity as a sensor value but as an attribute (although I just started mucking with this and could be wrong…)
I did some checking at pirateweather is said to generate sensor values and I have the sensor box checked. But for some reason it isn’t working. It seems that RH in the enthalpy block is not getting the value for outdoor_humidity_pirateweather.

This if is useless now and has no endif, so the template has an error now.

Figured out I was using the wrong name for my outdoor humidiy - should be the name converted to lower case and spaces replaced with underscores rather than the unique_id. And I had tried to put a comment in the middle of the enthalpy block to keep track of changes and that seemed to mess things up - not sure why but I removed it and the calculation now gives the expected value.

You probably used a YAML comment in a Jinja block, which doesn’t work

Bad:

{% set foo = 'bar' %}
# output the data
{{ foo }}

Good (using Jinja comments)

{% set foo = 'bar' %}
{# output the data #}
{{ foo }}