Template trigger in automation wont trigger

Folks, I got an automation to set my climate controll status like away, none and boost.

boost is preheating the office, away speaks for itself, and none is when beeing in.

there are two time triggers, and those work as intended. the flow chart indicates they are doing what I intend them to do.

then there is a template that should calculate the time needed to get the room to temp before I show up.

meaning, if its cold, start 2h early, if its freezing, start 8h early… thats primarily because of weekends as a preheat function after the romm is frozen solid for the better part of 3 days, because working on a cold floor and cold desk haunts me the rest of the day.

Hows the best way to troubleshoot this?

Thanks

here is the full yaml from that automation…

alias: SM_Heating
description: ""
trigger:
  - platform: template
    value_template: |-
      {% set target = 294.66 %}
      {% set k = states('sensor.ofice_temp')|float(0) + 273.16 %}
      {% set hours = (0.33 * (target-k)**2) | round(2) %}
      {% if hours > 8 %}
        {{ now() >= today_at("8:00") + timedelta(days=1) - timedelta(hours=hours) }}
      {% else %}
        {{ now() >= today_at("8:00") - timedelta(hours=hours) }}
      {% endif %}
    id: boost
  - platform: time
    at: "08:00"
    id: home
  - platform: time
    at: "16:15"
    id: away
condition:
  - condition: or
    conditions:
      - condition: time
        weekday:
          - mon
          - tue
          - wed
          - thu
          - fri
      - condition: time
        after: "18:00:00"
        weekday:
          - sun
action:
  - choose:
      - conditions:
          - condition: trigger
            id: boost
        sequence:
          - service: climate.set_preset_mode
            data:
              preset_mode: comfort
            target:
              entity_id: climate.officeheating
      - conditions:
          - condition: trigger
            id: home
        sequence:
          - service: climate.set_preset_mode
            data:
              preset_mode: none
            target:
              entity_id:
                - climate.officeheating
                - climate.restroomheating
      - conditions:
          - condition: trigger
            id: away
        sequence:
          - service: climate.set_preset_mode
            data:
              preset_mode: away
            target:
              entity_id:
                - climate.officeheating
                - climate.restroomheating
mode: single

At a complete guess, typo in sensor.ofice_temp?

Note also that your hours variable is positive even if the office temperature is already over the target, and boost mode will be set.

no i dont think so, because actually i use a location refference, so for the forum i just changed it to a meaningfull for every one refference.

And indeed im sure i have it right, since knowing myself, i absolutly only copy paste those stuff to prevent typos.

so thanks for catching it.

im staring at the code and your text, and right now cant figgure out what your saying…

I simplify it a bit and convert to celcius.

    value_template: |-
      {% set target = 21 %}
      {% set c = states('sensor.office_temp')|float(0) %}
      {% set hours = (0.33 * (target-c)**2) | round(2) %}
      {% if hours > 8 %}
        {{ now() >= today_at("8:00") + timedelta(days=1) - timedelta(hours=hours) }}
      {% else %}
        {{ now() >= today_at("8:00") - timedelta(hours=hours) }}
      {% endif %}

first i set the target to 21
then i set c at the current office temp
then i calculate how many hours early i need to start heating
then i check if that amount is bigger than the 8 hours that we have before we open office. if so start is at the day before, if not, it starts in the early hours of the workday.

So my calculation in case of beeing warm already would be:

0,33 x (21-23) ^2

factor of heating power times (target temp minus current temp) to the power of 2

at least thats how i figgured it

Template triggers only trigger when the template itself resolves false, then starts resolving true. If you implemented the automation and the template was already resolving true, a trigger will not occur.

And any real number to the power of two, negative or positive, is positive. That may not matter to you if the thermostats are working fine — just thought it was worth noting.

What is not working how you want it to? You can paste your trigger template into the template editor (Developer Tools / Template) and see what it returns — remove the {% if %}, {% else %} and {% endif %} and you can see both outputs at once.

The trigger will only fire when the overall output from the template changes from false to true (docs). Does that tie in with your logic?

thanks for the short refresher, indeed your right, now that you said it, I clearly remember the math lesseon back in school :slight_smile: .

and now, no I did not intend the trigger to switch from false to true, i wanted it to trigger the automation at the time when the condition in the template is matched

so how do i make my template switch from false to true at the very moment i want it to fire the boost? I dont know in advance when that will be, since the temp each day is different and therefore the preheating period.

Do i have to run a “every minute” trigger to evaluate my template 1440 times a day just to hit once a day the right moment?

You already are — the template includes now() so will be evaluated every minute.

Just need to make sure that your template output changes from false to true when you want it to.

i think i change it to other sources, sources that change say once a minute or so and have it make an obvious but meaning less change like switching a lamp. then when the trigger on itsself work, ill implement it into the heating.

I suggest you use your template to create a Template Binary Sensor so you can observe its history and gain a better understanding of its behavior. Its history will show when it turns on/off.

template:
  - binary_sensor:
      - name: Heating
        state: >
          {% set target = 21 %}
          {% set c = states('sensor.office_temp')|float(0) %}
          {% set hours = (0.33 * (target-c)**2) | round(2) %}
          {% set t = today_at("8:00") + timedelta(days = iif(hours > 8, 1, 0)) - timedelta(hours=hours) %}
          {{ now() >= t }}
1 Like

Im doing something wrong here

I tested this template in the template editor

{% set hours = 10.7 %}

{% if hours > 8 %}
  P--aulus is at {{hours}}
  {{ now() >= today_at("8:00") + timedelta(days=1) - timedelta(hours=hours) }}
{% else %}
  S--aulus is at {{hours}}
  {{ now() >= today_at("8:00") - timedelta(hours=hours) }}
{% endif %}

it comes back as true up to 8 and starting with 10.8
8.1 to 10.7 is false.

i dont understand that. right now is 21:17 localc on the server

the P–aulus and S–aulus comes back as expected. but the true and false is, well, i dont get it.

well after some reconsideration.

its now about 2,8 hours before midnight. so 8+2.8=10.8

when i give it 10.7 its false, then at the same time i give it 10.8 and its true, so the switch over schould be just fine and trigger.

its kind of working

i tried it with 2 decimals right on cue it ticked over from false to true, so actually my automations should be working

Just to let you know that before I suggested you create a Template Binary Sensor, I experimented with your template. I tried different values of the variable c and observed the resulting behavior of the variable t.

Its behavior was somewhat unexpected (‘kind of working’ would be a fair assessment) and, rather than report my results, I thought it would be best if you observed them yourself (via the history of a Template Binary Sensor).

1 Like

your cute :slight_smile: thanks

so since that does not work as expected… how do I make my heater start at the right moment to get the location warmed up before I start to work?

Any hints would be apreciated.

Did you create and observe the behavior of the Template Binary Sensor I had suggested?

I did, I usually do what folks with more experience than myself tell me :slight_smile:

Here is the graph, I dont know how to export it otherwise.

keep in mind when thinking about that graph, it runs off a bouncing temp. here is the temp for the same time next to the graph :slight_smile: guess that helps.

This temp curv is the result of the heating automation from a FritzBox… very sofisticated. Im totally impressed. In real live one does not notice the temp changes that much, and I think the heat inside the sensor is fooling the temp output from the current running throught that fritz dect Plug unit.

Since this does not do much in helping to figgur out the “sort of working”:

I will run a second binary sensor with same code and off a stable temp number, that should trigger it precicely once by switching from fals to true then…

I probably also need to set a timeframe for that template like only after 10 pm and before 8 am for realworld application.

I’ll be honest and admit that I’m not fully vested in this topic; my interest was limited to providing a means of checking if the algorithm you created meets your intended goal. I think it’s clear from the Template Binary Sensor’s history graph that the algorithm doesn’t meet your goal. You’ll need to revise the algorithm (and I have no suggestions on that matter).