Template as trigger never triggers

Hello :slight_smile:

I try to send a notification when the outdoor temperature is about to reach the indoor temperature (or vice versa).

I have 2 sensors and created a derivative helper from the outdoor sensor.

Now I made the following template as trigger but it won’t trigger the automation.

The template listen to the following events:

  • Entity: sensor.0x00124b0025031c97_4_temperature_2 → outdoor sensor
  • Entity: sensor.0x00124b0025033e5e_3_temperature → indoor sensor
  • Entity: sensor.derivee_temperature_sud → derivative from outdoor sensor
{## store temperatures in var ##}
{% set temp_int = float(states.sensor["0x00124b0025033e5e_3_temperature"].state) %}
{% set temp_ext = float(states.sensor["0x00124b0025031c97_4_temperature_2"].state) %}

{## calculate the difference between the two ##}
{% set temp_diff =  float(states.sensor["0x00124b0025031c97_4_temperature_2"].state) - float(states.sensor["0x00124b0025033e5e_3_temperature"].state) %}

{## predict the temperature in 30 minutes given the current temperature and the derivative (mili Celcius/minute) ##}
{% set derivate = float(states.sensor.derivee_temperature_sud.state) * 30 / 1000 %}
{% set predicted_temp_ext = temp_ext + derivate %}

{## if the predicted outdoor temperature (in 30min) is above the indoor temperature, trigger the actions. ##}
{% if predicted_temp_ext > temp_int %}
  true
{% endif %}

The result in dev tool is ‘true’.

Automation trace is empty (I also checked that notification is working using time pattern as trigger).

What am I doing wrong ?

According to the logs (I enabled them all).

I get

2022-05-02 11:54:48 ERROR (Thread-186) [homeassistant.helpers.template] Template variable error: 'state' is undefined when rendering ...

Why !? :frowning: copy/pasting it in the Developper Tools Template is working flawlessly …

Please include your full automation trigger in future.

Heed this warning:

https://www.home-assistant.io/docs/configuration/templating/#states

Provide default values for your float functions.

Consider using float filters instead of functions for readability.

Set your template to provide a true or false result. e.g.

{{ predicted_temp_ext > temp_int }}

Consider editing your entity ids to be something human readable that does not begin with a number so that you don’t have to use square bracket notation.

Hi @tom_l
Thank you for your response and tips.

I updated the automation as follows:

- id: '1651479244973'
  alias: Ouverture des fenêtres
  description: ''
  trigger:
  - platform: template
    value_template: '{## Store temperatures in var ##}

      {% set temp_int = float(states(''sensor.0x00124b0025033e5e_3_temperature''),
      0) %}

      {% set temp_ext = float(states(''sensor.0x00124b0025031c97_4_temperature_2''),
      0) %}


      {## calculate the difference between the two ##}

      {% set temp_diff =  temp_ext - temp_int %}


      {## predict the temperature in 30 minutes given the current temperature and
      the derivative ##}

      {% set derivate = float(states(''sensor.derivee_temperature_sud''), 0) * 30
      / 1000 %}

      {% set predicted_temp_ext = temp_ext + derivate %}


      {{ predicted_temp_ext > temp_int }}'
  condition: []
  action:
  - service: notify.mobile_app_f3
    data:
      message: Temp C
      title: Changement de température
  mode: single

config/home-assistant.log doesn’t show errors anymore but the automation is still not triggered.

Consider editing your entity ids to be something human readable that does not begin with a number so that you don’t have to use square bracket notation.

Thanks for the tip, I’ll consider it (changing the entity ID from mqtt devices causes issues with HA).

You know there’s a derivative integration that calculates the actual derivative?

The example in the docs makes a sensor that calculates the temperature change over the last have hour, exactly what you are trying to do.

Hi @petro

Indeed, that’s the object I’m using for sensor.derivee_temperature_sud

It is the derivative from the outdoor temperature (sensor.0x00124b0025031c97_4_temperature_2). I created it using the UI but I can’t find it in the yaml files in /config.

The line (spliced in two in my code above)

predicted_temp_ext = temp_ext + float(states(''sensor.derivee_temperature_sud''), 0) * 30 / 1000

Predict the outdoor temperature in 30minutes (the derivative is in m°C/Minute hence the division by 1000 to get it in °C/Minute

I added this line:

{% set v = states.sensor.time.state %}

So that the automation is evaluated each minute, which is the case:

triggered by event: <Event state_changed[L]: entity_id=sensor.time, old_state=<state sensor.time=14:19; icon=mdi:clock, friendly_name=Time @ 2022-05-02T14:19:00.017892+02:00>, new_state=<state sensor.time=14:20; icon=mdi:clock, friendly_name=Time @ 2022-05-0
2T14:20:00.005513+02:00>>

However it is still not working :frowning:

2022-05-02 14:23:00 DEBUG (MainThread) [homeassistant.helpers.event] Template group [TrackTemplate(template=Template("{## Store temperatures in var ##}
{% set temp_int = float(states('sensor.0x00124b0025033e5e_3_temperature'), 0) %}
{% set temp_ext = float(states('sensor.0x00124b0025031c97_4_temperature_2'), 0) %}

{## calculate the difference between the two ##}
{% set temp_diff =  temp_ext - temp_int %}

{## predict the temperature in 30 minutes given the current temperature and the derivative ##}
{% set derivate = float(states('sensor.derivee_temperature_sud'), 0) * 30 / 1000 %}
{% set predicted_temp_ext = temp_ext + derivate %}

{## optional: use absolute values  ##}
{% set abs_temp_diff = int(temp_diff - derivate) | abs %}
{% set abs_derivate = derivate | abs %}

{% set v = states.sensor.time.state %}

{{ predicted_temp_ext >= temp_int }}"), variables=None, rate_limit=None)] listens for {'all': False, 'entities': {'sensor.derivee_temperature_sud', 'sensor.time', 'sensor.0x00124b0025031c97_4_temperature_2', 'sensor.0x00124b0025033e5e_3_temperature'}, 'domains': set(), 'time': False}, re-render
 blocker by super template: False

I tried to change the condition to predicted_temp_ext >= temp_int or predicted_temp_ext > temp_int

Does template trigger even work ?

The triggering system is definitely not working

I replaced the condition with {{ true }}

{{ true }}"), variables={'this': {'entity_id': 'automation.ouverture_des_fenetres', 'state': 'on', 'attributes': {'last_triggered': datetime.datetime(2022, 5, 2, 8, 54, 0, 23078, tzinfo=datetime.timezone.utc), 'mode': 'single', 'current': 0, 'id': '1651479244973', 'friendly_name': 'Ouverture des fe
nêtres'}, 'last_changed': '2022-05-02T12:29:47.462708+00:00', 'last_updated': '2022-05-02T12:29:47.462708+00:00', 'context': {'id': '37208cb411f883a9ad4e6c4397f570eb', 'parent_id': None, 'user_id': None}}}, rate_limit=None)] listens for {'all': False, 'entities': {'sensor.derivee_temperature_sud',
'sensor.time', 'sensor.0x00124b0025031c97_4_temperature_2', 'sensor.0x00124b0025033e5e_3_temperature'}, 'domains': set(), 'time': False}, re-render blocker by super template: False

still nothing

Template triggers only trigger when the template resolves false, then later resolves true.

I tried with

{{ now().minute % 2 == 0 }}

which indeed works. So as far as I understand, saving the automation reset it states to NOT false ?
So manually return false then true won’t work.

Ok it actually now works.

In order for the template to be triggered, it first need to change to false then true without being edited / saved, otherwise it will never trigger.

I find the whole really painful to debug, I hoped it would be easier and that there was more sugar coat.