(Yet another) Cannot get template-based automations to work

Hi all - yet another “I can’t get my automation to work”. I’ve read the docs and associated threads and apparently the information isn’t sinking in.

I am trying to query OpenWeatherMap for the outside air temperature (OAT) and do something if is below 0F. Pretty simple…

OpenWeatherMap does not return an integer for the query, but instead returns a string. I know you can’t do a compare on a string, so it needs to be converted to a number.

I am trying this as a template trigger, but no matter what temperature you set, it always fires.

alias: oat_test
description: "Alert if OAT less than 0F"
trigger:
  - platform: template
    value_template: >-
      {% if states.sensor.openweathermap_temperature | float < 0 %} true {% endif %}
condition: []
action:
  - service: notify.persistent_notification
    metadata: {}
    data:
      message: It is cold
      title: Brr
mode: single

From the Developer Tools > Templates:

{{ states.sensor.openweathermap_temperature }}

Result type: string

<template TemplateState(<state sensor.openweathermap_temperature=19.06; state_class=measurement, unit_of_measurement=°F, attribution=Data provided by OpenWeatherMap, device_class=temperature, friendly_name=OpenWeatherMap Temperature @ 2024-01-15T00:29:54.701818-08:00>)>

I’m sure whatever I’m doing is on the far side of stupid, but I cannot get this to work?

If you test the automation manually it skips the triggers and conditions and just runs the actions. Also there is no need for a template trigger, e.g.

trigger:
  - platform: numeric_state
    entity_id: sensor.openweathermap_temperature
    below: 0

But if you were to use a template sensor it would be like this:

trigger:
  - platform: template
    value_template: >
      {{ states('sensor.openweathermap_temperature') | float(0) < 0 }}

To expand on Tom’s response, I think the reason why it didn’t work for you is because you need to add .state to the end of states.sensor.openweathermap_temperature. As you showed in your post, without .state you get the entire content of the object, not the state of it (which should be just 19.06).

However as Tom points out, there are better ways to access entity states and even in templates you should use states('entity_id') over states.entity_id.state. I just thought I’d add this in case you wanted to know WHY yours may have not worked.

Thanks @tom_l and @hawksj - I do appreciate the help.

For debugging, I took Tom’s suggestion and also set the notification threshold over 100 degrees, and the automation is still firing.

I added a 1 minute repeat, again for debugging to make sure it kicks off the automation.

alias: oat_test
description: Alert if OAT less than 0F
trigger:
  - platform: time_pattern
    minutes: /1
  - platform: numeric_state
    entity_id: sensor.openweathermap_temperature
    above: 100
condition: []
action:
  - service: notify.persistent_notification
    metadata: {}
    data:
      message: It is hot
      title: Hawt
mode: single

What?

You added a second trigger that is going to fire every minute regardless of the temperature?

What are you actually trying to do?

The numeric_state trigger will work… but that’s not proven by the code you have above because any of the triggers will cause an automation to run (even if the conditions for others are false). You aren’t testing the numeric_state trigger with that, you’re testing the one minute loop.

There should be no need to force a test ahead of the real thing but if you really wanted to just to demonstrate to yourself that numeric_state works, I would do it with a temporary input_number helper so you can change it manually and test if the notification fires below a certain threshold.

Or just by changing the value of the sensor in Developer Tools-> States.

Yes I had considered that but I tend to dislike changing the values of read-only sensors in Developer Tools, I had trouble triggering automations via Node Red when I did it this way. I doubt it messes up the long term statistics but personally I’d rather make a test that resembles the real thing.

Well this isn’t it:

trigger:
  - platform: time_pattern
    minutes: /1

Definitely not! I would do something like

trigger:
  - platform: numeric_state
    entity_id: input_number.test_temperature
    above: 100
  - platform: numeric_state
    entity_id: sensor.openweathermap_temperature
    above: 100

And delete the test after. Ultimately Dev Tools probably works fine though

I think I have it sorted - I didn’t understand how/when HA would trigger on the given conditions. As I think I understand it now, it only triggers on a state change, not on a polling cycle?

What am I trying to do? Ultimately, I want an automation that will change my Ecobee set point based on the OAT. If the OAT is less than 25F, change the set point to 68F, and then periodically check to see if the set point has been changed manually. If the set point has been changed manually, put it back to 68.

I am exploring the TREND and DERIVATIVE sensors to see if the temperature is increasing or decreasing through a certain point. The idea being that if the OAT falls below 25, turn the thermostat down. When the OAT rises above 25 (from a colder state), turn the thermostat up.

Oh, wait… there is a THRESHOLD sensor? Fuh…

Sorry to have wasted your time.