Help with template helper sensor

Hello,

Just need some help with trying to tweak a template helper sensor I created to track status of my HVAC system based on ecobee status. The code I have current and seems to work fine is:

{% set attributes = state_attr('climate.my_ecobee_4', 'equipment_running') %}
{% if 'heatPump2' in attributes %}
  Heat Pump Stage 2
{% elif 'heatPump' in attributes %}
  Heat Pump
{% elif 'auxHeat1' in attributes %}
  Furnace Stage 1
{% elif 'auxHeat2' in attributes %}
  Furnace Stage 2
{% elif 'compCool1' in attributes %}
  Cooling Stage 1
{% elif 'compCool2' in attributes %}
  Cooling Stage 2
{% elif 'fan' in attributes %}
  Fan
{% else %}
  Idle
{% endif %}

What I was to add to it is to track when the heat pump goes on defrost cycle. Based monitoring my supply air temperature using a sensor, I can see a dip in temperature while the unit is running on defrost and ecobee is still calling for heat and then after cycle completes the temperature rises again. How can I track the defrost by looking for this pattern and adding it the template sensor above. Below picture shows in red the temperature dips when defrost is running:

I was thinking tracking negative steep temperature drop trend followed by steep rise while ecobee status is heating (on heat pump) would be the way to identify when defrost cycle is running but I donā€™t know how to capture that in my template sensor. Any help with this would be really appreciated.

Set up a Trend binary sensor then add if/elif clauses that combine the two conditions as necessary:

{% set attributes = state_attr('climate.my_ecobee_4', 'equipment_running') %}
{% set falling = is_state('binary_sensor.temp_falling', 'on') %}
{% if 'heatPump2' in attributes and falling %}
  Defrost - Heat Pump Stage 2
{% elif 'heatPump2' in attributes %}
  Heat Pump Stage 2
{% elif 'heatPump' in attributes and falling %}
  Defrost - Heat Pump
{% elif 'heatPump' in attributes %}
  Heat Pump
{% elif 'auxHeat1' in attributes %}
  Furnace Stage 1
{% elif 'auxHeat2' in attributes %}
  Furnace Stage 2
{% elif 'compCool1' in attributes %}
  Cooling Stage 1
{% elif 'compCool2' in attributes %}
  Cooling Stage 2
{% elif 'fan' in attributes %}
  Fan
{% else %}
  Idle
{% endif %}

Thanks for the code and hint regarding the template sensor. Trying to create a template sensor that captures the steep temperature drop followed by a quick rise within a 10 to 30 minute window of a defrost cycle but having a hard time with the parameters for it. Any advice on what parameters would be ideal for the template based on the graph above?

I think I figured out the trend parameters that seems to be working to relatively quickly indicate when defrost is running and off, thank you for the help with the script syntax, really appreciate it.

One last question but itā€™s less urgent, what do I need to add to the script to change icon based on the returned state?

I tried adding the following code in config yaml but got errors:

template:
- sensor:
  - name: HVAC Status
    state: >
      {% set attributes = state_attr('climate.my_ecobee_4', 'equipment_running') %}
        {% set falling = is_state('binary_sensor.hvac_supply_trend', 'on') %}
        {% if 'heatPump2' in attributes and falling %}
          Defrost - Heat Pump Stage 2
        {% elif 'heatPump2' in attributes %}
          Heat Pump Stage 2
        {% elif 'heatPump' in attributes and falling %}
          Defrost - Heat Pump
        {% elif 'heatPump' in attributes %}
          Heat Pump
        {% elif 'auxHeat1' in attributes %}
          Furnace Stage 1
        {% elif 'auxHeat2' in attributes %}
          Furnace Stage 2
        {% elif 'compCool1' in attributes %}
          Cooling
        {% elif 'compCool2' in attributes %}
          Cooling Stage 2
        {% elif 'fan' in attributes %}
          Fan
        {% else %}
          Idle
        {% endif %}
          icon: >
        {% if is_state('sensor.hvac_status', 'Defrost - Heat Pump') %}
        mdi:snowflake-melt
        {% elif is_state('sensor.hvac_status', 'Heat Pump')
        mdi:heat-pump-outline
        {% elif is_state('sensor.hvac_status', 'Furnace Stage 1')
        mdi:fire
        {% elif is_state('sensor.hvac_status', 'Furnace Stage 2')
        mdi:fire
        {% elif is_state('sensor.hvac_status', 'Cooling')
        mdi:air-conditioner
        {% elif is_state('sensor.hvac_status', 'Fan')
        mdi:fan
        {% else %}
        mdi:hvac-off
        {% endif %}

The error I get when I reload home assistant is:

Invalid config for ā€˜templateā€™ at configuration.yaml, line 53: invalid template (TemplateSyntaxError: expected token ā€˜end of statement blockā€™, got ā€˜mdiā€™) for dictionary value ā€˜sensor->0->stateā€™, got "{% set attributes = state_attr(ā€˜climate.my_ecobee_4ā€™, ā€˜equipment_runningā€™) %}\n {% set falling = is_state(ā€˜binary_sensor.hvac_supply_trendā€™, ā€˜onā€™) %}\n {% if ā€˜heatPump2ā€™ in attributes and falling %}\n Defrost - Heat Pump Stage 2\n {% elif ā€˜heatPump2ā€™ in attributes %}\n Heat Pump Stage 2\n {% elif ā€˜heatPumpā€™ in attributes and falling %}\n Defrost - Heat Pump\n {% elif ā€˜heatPumpā€™ in attributes %}\n Heat Pump\n {% elif ā€˜auxHeat1ā€™ in attributes %}\n Furnace Stage 1\n {% eliā€¦

Incorrect indentation of the icon: option.

It should be vertically aligned with the state: option.

1 Like

Please drop you last(next) question and mark Didgeriā€™s answer as Solution , this to keep question/solutions separate

You can Not use the ā€œiconā€ template like you do, partly itā€™s in the middle of nowhere

There are several solutions for this, and varies in syntax depended upon where

I fixed the indentation but still same issue:

template:
- sensor:
  - name: HVAC Status
    state: >
      {% set attributes = state_attr('climate.my_ecobee_4', 'equipment_running') %}
        {% set falling = is_state('binary_sensor.hvac_supply_trend', 'on') %}
        {% if 'heatPump2' in attributes and falling %}
          Defrost - Heat Pump Stage 2
        {% elif 'heatPump2' in attributes %}
          Heat Pump Stage 2
        {% elif 'heatPump' in attributes and falling %}
          Defrost - Heat Pump
        {% elif 'heatPump' in attributes %}
          Heat Pump
        {% elif 'auxHeat1' in attributes %}
          Furnace Stage 1
        {% elif 'auxHeat2' in attributes %}
          Furnace Stage 2
        {% elif 'compCool1' in attributes %}
          Cooling
        {% elif 'compCool2' in attributes %}
          Cooling Stage 2
        {% elif 'fan' in attributes %}
          Fan
        {% else %}
          Idle
        {% endif %}
    icon: >
        {% if is_state('sensor.hvac_status', 'Defrost - Heat Pump') %}
        mdi:snowflake-melt
        {% elif is_state('sensor.hvac_status', 'Heat Pump')
        mdi:heat-pump-outline
        {% elif is_state('sensor.hvac_status', 'Furnace Stage 1')
        mdi:fire
        {% elif is_state('sensor.hvac_status', 'Furnace Stage 2')
        mdi:fire
        {% elif is_state('sensor.hvac_status', 'Cooling')
        mdi:air-conditioner
        {% elif is_state('sensor.hvac_status', 'Fan')
        mdi:fan
        {% else %}
        Idle
        mdi:hvac-off
        {% endif %}

Invalid config for ā€˜templateā€™ at configuration.yaml, line 75: invalid template (TemplateSyntaxError: expected token ā€˜end of statement blockā€™, got ā€˜mdiā€™) for dictionary value ā€˜sensor->0->iconā€™, got ā€œ{% if is_state(ā€˜sensor.hvac_statusā€™, ā€˜Defrost - Heat Pumpā€™) %} mdi:snowflake-melt {% elif is_state(ā€˜sensor.hvac_statusā€™, ā€˜Heat Pumpā€™) mdi:heat-pump-outline {% elif is_state(ā€˜sensor.hvac_statusā€™, ā€˜Furnace Stage 1ā€™) mdi:fire {% elif is_state(ā€˜sensor.hvac_statusā€™, ā€˜Furnace Stage 2ā€™) mdi:fire {% elif is_state(ā€˜sensor.hvac_statusā€™, ā€˜Coolingā€™) mdi:air-conditioner {% elif is_state(ā€˜sensor.hvac_statusā€™, ā€˜Fanā€™) mdi:fan {% else %} mdi:hvac-off {% endif %}\nā€

For future reference, donā€™t mark a post with the Solution tag if youā€™re still getting errors trying to implement it.

The latest error is because the last icon value is invalid (remove Idle).

1 Like

Try this, i wonā€™t say it works, because iā€™ve never used it for a template-sensor, But in a card which supports template for the icon:element

Anyhow, the first row needs indented ( Look at your first Template ! )

haha, i never noticed the Idle ( But it should work regardless ) :slight_smile:

      icon: >-
        {% if is_state('sensor.moon_phase', 'new_moon')%} mdi:moon-new 
        {% elif is_state('sensor.moon_phase', 'waxing_crescent')%}
        mdi:moon-waxing-crescent {% elif is_state('sensor.moon_phase',
        'first_quarter')%} mdi:moon-first-quarter {% elif
        is_state('sensor.moon_phase', 'waxing_gibbous')%}
        mdi:moon-waxing-gibbous {% elif is_state('sensor.moon_phase',
        'full_moon')%} mdi:moon-full {% elif
        is_state('sensor.moon_phase',  'waning_gibbous')%}
        mdi:moon-waning-gibbous {% elif is_state('sensor.moon_phase',
        'last_quarter')%} mdi:moon-last-quarter {% elif
        is_state('sensor.moon_phase', 'waning_crescent')%}
        mdi:moon-waning-crescent {% else %} away !!!  {% endif %}

And Please take a look at your ā€œlastā€ template ! , This is one other reason reason not to start NEW question in a Topic which are solved ! , we are filling the topic with bullocks

Well initial question was resolved so I had marked it as such based on your request above. I should have split the second question to another post. I appreciate your guidance, got it working with below:

template:
- sensor:
  - name: HVAC Status
    state: >
      {% set attributes = state_attr('climate.my_ecobee_4', 'equipment_running') %}
        {% set falling = is_state('binary_sensor.hvac_supply_trend', 'on') %}
        {% if 'heatPump2' in attributes and falling %}
          Defrost - Heat Pump Stage 2
        {% elif 'heatPump2' in attributes %}
          Heat Pump Stage 2
        {% elif 'heatPump' in attributes and falling %}
          Defrost - Heat Pump
        {% elif 'heatPump' in attributes %}
          Heat Pump
        {% elif 'auxHeat1' in attributes %}
          Furnace Stage 1
        {% elif 'auxHeat2' in attributes %}
          Furnace Stage 2
        {% elif 'compCool1' in attributes %}
          Cooling
        {% elif 'compCool2' in attributes %}
          Cooling Stage 2
        {% elif 'fan' in attributes %}
          Fan
        {% else %}
          Idle
        {% endif %}
    icon: >-
        {% if is_state('sensor.hvac_status', 'Heat Pump')%} mdi:heat-pump-outline
        {% elif is_state('sensor.hvac_status', 'Defrost - Heat Pump')%} mdi:snowflake-melt
        {% elif is_state('sensor.hvac_status', 'Furnace Stage 1')%} mdi:fire
        {% elif is_state('sensor.hvac_status', 'Furnace Stage 2')%} mdi:fire-circle
        {% elif is_state('sensor.hvac_status', 'Cooling')%} mdi:air-conditioner
        {% elif is_state('sensor.hvac_status', 'Fan')%} mdi:fan
        {% else %} mdi:hvac-off  {% endif %}
1 Like