Noobie needs help on templated automation triggers!

Thanks for reading! I am old, but have been playing around with HA for months. Now I am getting more involved, but am having trouble with what should be a simple thing - triggering an automation with a value template. Basically I am doing this to eventually get min and max temps for day, week, month, year with date and time f the or min or max. But I wanted to start off with the minimal version :slight_smile:

alias: daily set max temp
description: set or reset daily max temperature
trigger:
  - platform: time
    at: "00:00:00"
    id: daily_reset
  - platform: template
    value_template: >-
      {{ states('input_number.max_daily_temp')|int ==
      states('input_number.max_temp_reset')|int }}
    id: new_max
  - platform: template
    value_template: >-
      {{ states('sensor.gw2000b_outdoor_temperature')|float >
      states('input_number.max_daily_temp')|float }}
    id: new_max
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - daily_reset
        sequence:
          - service: input_number.set_value
            target:
              entity_id: input_number.max_daily_temp
            data:
              value: input_number.max_temp_reset
          - service: notify.alexa_media_my_show
            data:
              message: hey - it is daily reset!
              data:
                type: tts
      - conditions:
          - condition: trigger
            id:
              - new_max
        sequence:
          - service: input_number.set_value
            target:
              entity_id: input_number.max_daily_temp
            data:
              value: "{{ states('sensor.gw2000b_outdoor_temperature')|float }}"
          - service: notify.alexa_media_my_show
            data:
              message: >-
                new max temp is {{states('sensor.gw2000b_outdoor_temperature')}}
                degrees
              data:
                type: tts
          - service: input_number.increment
            data: {}
            target:
              entity_id: input_number.new_max_count
    default:
      - service: notify.alexa_media_my_show
        metadata: {}
        data:
          message: >-
            this temperature reading of {{
            states('sensor.gw2000b_outdoor_temperature')}} degrees and the 
            input number max daily value was
            {{states('input_number.max_daily_temp')}}
          data:
            type: tts
      - service: input_number.increment
        target:
          entity_id: input_number.default_action_no_new_max_count
        data: {}
mode: single

Thanks for all your time and effort!

edit: the daily reset works

Normally I would do this kind of thing with Template sensors rather than an automation and Input number helpers. As written, your second trigger and default action don’t seem to serve a purpose.

alias: daily set max temp
description: set or reset daily max temperature
trigger:
  - platform: time
    at: "00:00:00"
  - platform: numeric_state
    entity_id: sensor.gw2000b_outdoor_temperature
    above: input_number.max_daily_temp
action:
  - variables:
      message: |
        {% "hey - it is daily reset!" if trigger.platform == 'time' else 
        "new max temp is "~states('sensor.gw2000b_outdoor_temperature')~"degrees." %}
  - service: input_number.set_value
    target:
      entity_id: input_number.max_daily_temp
    data:
      value: "{{ states('sensor.gw2000b_outdoor_temperature') }}"
  - service: notify.alexa_media_my_show
    data:
      message: "{{ message }}"
    data:
      type: tts
mode: single

Thanks for your reply! The reason I used that second one was to make sure the trigger was false on the first go around after reset. At least that seemed to be the documentation for the automation in the editor - it said the template must change from false to true, hence setting it up for being false. Very confusing documentation and test in the editor on how things are evaluated :slight_smile:

I have no firm desire to do everything in an automation. How would this be better (easier?) in a template sensor?

I would probably do something like this:

template:
  - sensor:
      - name: "Min temperature"
        unit_of_measurement: "°C"
        state: >
          {% set current_temp = states('sensor.gw2000b_outdoor_temperature') %}
          {% if this.state|float(999) > current_temp %}
          {{ current_temp }}
          {% else %}
          {{ this.state|float(999) }}
          {% endif %}

With some tinkering, it could probably be made into a single line template, but there’s really no need to be that efficient.

Let me walk you through what this sensor is doing:

First, we get our current_temp from our sensor. Then, we compare it to our current state (the current “minimum” value), and see if it’s lower. If it’s lower, we set the state to our current_temp, and if not, we keep the state at the current state.

this is a special variable that refers to itself, and is used specifically for self-referencing.

Lastly, you’ll notice the |float(999) filter that I’ve slapped on this.state. The purpose of this filter is to ensure that the output of this.state is a floating point number so that the comparison can work. In the event that it is not a floating point number or something that can be converted to a floating point number (for example, if the state is unavailable or unknown), the filter spits out the default 999. I picked this number since it should be larger than anything your sensor is spitting out (or else you’ve got bigger problems).

If you want to reset it every day/week/month, then you could extend it like:

template:
  - trigger:
	  - platform: time_pattern
		hours: 0
		minutes: 0
	  - platform: state
		entity_id:
		  - sensor.gw2000b_outdoor_temperature
	sensor:
      - name: "Min day temperature"
        unit_of_measurement: "°C"
        state: >
          {% set current_temp = states('sensor.gw2000b_outdoor_temperature') %}
		  {% if this.state|float(999) > current_temp or trigger.platform == 'time_pattern' %}
          {{ current_temp }}
          {% else %}
          {{ this.state|float(999) }}
          {% endif %}

In this case, we use a time_pattern to reset at midnight every night. As soon as you define a trigger for a template sensor, it stops automatically triggering on referenced entity changes, so we also have to explicitly tell the template to update whenever our sensor’s state changes.

We’ve also modified our first logical statement. When we reset at midnight (aka, use a time_pattern trigger), the current temperature would become our new day’s minimum (as well as its maximum), which is the same outcome as when the current_temp is lower than our currently recorded minimum.

trigger is a special variable that gives us the trigger data, and in this case, we use trigger.platform to determine which platform actually triggered. Since we only have one time_pattern trigger defined, we can safely assume that when we see a time_pattern trigger, we’re resetting at midnight.

Hopefully that helps!

Ok… it’s not that any trigger needs to be false. The two Template triggers are completely independent, one changing to false has no effect on the other.

It’s not necessarily easier, but it maintains the distinction of Input Helpers which are meant to reflect direct user input.

It also keeps the number of automations down… some users hate having a lot of automations in the list of the Automations menu.

template:
  - trigger:
      - platform: state
        entity_id: sensor.gw2000b_outdoor_temperature
        not_to:
          - unavailable
          - unknown
      - platform: time
        at: "00:00:00"
    action:
      - variables:
          source: "{{ states('sensor.gw2000b_outdoor_temperature') | float(0) }}"
    sensor:
      - name: Temp Max Daily
        unit_of_measurement: "°F"
        state: >
          {% set current = this.state | float(source)  %}
          {{ source if trigger.platform == 'time' else [source, current] | max }}
      - name: Temp Min Daily
        unit_of_measurement: "°F"
        state: >
          {% set current = this.state | float(source)  %}
          {{ source if trigger.platform == 'time' else [source, current] | min }}
      - name: Temp Max Monthly
        unit_of_measurement: "°F"
        state: >
          {% set current = this.state | float(source)  %}
          {{ source if (trigger.platform == 'time' and now().day ==1) else [source, current] | max }}
      - name: Temp Min Monthly
        unit_of_measurement: "°F"
        state: >
          {% set current = this.state | float(source)  %}
          {{ source if (trigger.platform == 'time' and now().day ==1) else [source, current] | min }}

Thanks! I initially tried doing that but never got it to trigger. Your version looks much better than what I was trying though…

Tomorrow I will look at what I was trying to do in the template sensor and post it before I mark your solution.

I have no doubt it will provide some laughs:)

I couldn’t bring myself to expose my feeble attempts lol Thank you so much for your solution! Much more elegant!

I’m happy to help!

One of the things that makes Home Assistant so great, but also so difficult for newbies, is that it supports a million different ways of doing everything. If you’re trying to find a way to do X, as you look through dozens of posts and examples, instead of finding the straightforward solution repeated again and again, which allows you to reverse engineer and adapt for your use case, you’ll instead probably find a dozen different ways to do it. None of them are “right” or “wrong”, just different. And until you have more experience, it’s often difficult to figure out the pros and cons of the many different methods.

Something that I recommend to all of my friends who use Home Assistant is to really dive in to the templating system. Jinja is (in my opinion) a terrible basterdization of Python, and I fought against it when I first got started. However, once I really started to use templates, I found that a lot of problems I wanted to solve suddenly became a lot easier. And template sensors, in particular, are a gateway to unlocking many of the features of Home Assistant.

For example, I have a template sensor binary_sensor.front_outside_lights. When this sensor is on, my front lights should be on, and when off, lights should be off. Thanks to the template system, that one sensor captures a ton of logic that would be difficult to put into or maintain in an automation. But thanks to that sensor, my automation is very simple: when binary_sensor.front_outside_lights changes state, automatically turn_on or turn_off the light.front_outside_lights accordingly.

You make it look easy lol. I am trying to do too much , and getting tripped up because things are so spread out in the docs, or are less simple to use than the examples. And searching for stuff often leads to things that have been change. And then there is the fun of trying to figure out what the trace back in logs is trying to tell one.

But I will get there unless I forget everything. I’m old, but not quite at that point yet lol

Thanks for your aid.

Don’t worry, that still happens to me :smile:

My bible for templating is this document: Templating - Home Assistant. This document has gotten a lot better over the years.

However, for understanding jinja, and especially jinja controls, the official documentation is extremely helpful. Start here: Template Designer Documentation — Jinja Documentation (3.1.x)

Also, take full advantage of the Developer Tools template editor. Whenever I’m working through complicated templates, I’m always testing them in the editor so I can easily see what’s going on. Also, when I’m done testing a template, I always just comment it out so I’ll always have it for reference. I just looked, and I have ~100 different templates from over the years, which is a great tool if I can’t remember exactly how I accomplished something in the past.

It may look easy, but it took me many years to get to this point. You’ll get there too!

Thanks so much! You have given me some very good ideas amend much to think on!