Why does one trigger template sensor work, and the other not?

Hello!
I have to trigger template sensors, which are almost same, they should show the min and max temperature of the day:

  - trigger:
    - platform: time
      at: "00:00:00"
    - platform: template
      value_template: "{{ states('sensor.weather_temperature') | float(0) < states('sensor.weather_temperature_min') | float(0) }}"
    sensor:
      - name: "Weather Temperature Min"
        unique_id: weather_temperature_min
        unit_of_measurement: "°C"
        device_class: temperature
        icon: mdi:thermometer-chevron-down
        state: "{{ states('sensor.weather_temperature') | float(0) }}"
        attributes:
          datetime: "{{ now() }}"

  - trigger:
    - platform: time
      at: "00:00:00"
    - platform: template
      value_template: "{{ states('sensor.weather_temperature') | float(0) > states('sensor.weather_temperature_max') | float(0) }}"
    sensor:
      - name: "Weather Temperature Max"
        unique_id: weather_temperature_max
        unit_of_measurement: "°C"
        device_class: temperature
        icon: mdi:thermometer-chevron-up
        state: "{{ states('sensor.weather_temperature') | float(0)}}"
        attributes:
          datetime: "{{ now() }}"

This is in a templats.yaml file, which is linked in configuration.yaml with template: !include templates.yaml.

My problem is, the max value is working flawlessly, the min not, and I don’t understand why
image

It’s the same function, and one template value is working and one not, I don’t understand why.

Thank you for your help
Dominik

Probably because the temperature minimum sensor has not triggered yet becasue the first time it triggers this

states('sensor.weather_temperature_min') | float(0) 

Will be the default value, 0 as sensor.weather_temperature_min is unknown.

You your temperature has to go below 0 degrees to trigger.

Thank you! I’m an idiot, I changed it to this:

  - trigger:
    - platform: time
      at: "00:00:00"
    - platform: template
      value_template: "{{ states('sensor.weather_temperature') | float(99) < states('sensor.weather_temperature_min') | float(99) }}"
    sensor:
      - name: "Weather Temperature Min"
        unique_id: weather_temperature_min
        unit_of_measurement: "°C"
        device_class: temperature
        icon: mdi:thermometer-chevron-down
        state: "{{ states('sensor.weather_temperature') }}"
        attributes:
          datetime: "{{ now() }}"

  - trigger:
    - platform: time
      at: "00:00:00"
    - platform: template
      value_template: "{{ states('sensor.weather_temperature') | float(-99) > states('sensor.weather_temperature_max') | float(-99) }}"
    sensor:
      - name: "Weather Temperature Max"
        unique_id: weather_temperature_max
        unit_of_measurement: "°C"
        device_class: temperature
        icon: mdi:thermometer-chevron-up
        state: "{{ states('sensor.weather_temperature')}}"
        attributes:
          datetime: "{{ now() }}"

I think this should set a correct value, as soon as a valid value is delivered from sensor.weather_temperature. But sensor.weather_temperature_min is unknown… Is there another thing I’m missing?

I’ve been using a sensor like this for a while. I acknowledge that it seems overly complicated, but it works. I’ve tried a number of ways to simplify this, but every one of them breaks the sensor…

trigger:
  - platform: time
    at: "00:00"
    id: reset
  - platform: state
    entity_id: sensor.weather_temperature
    not_to:
      - unknown
      - unavailable
sensor:
  - name: "Weather Temperature Min"
    unique_id: weather_temperature_min
    unit_of_measurement: "°C"
    device_class: temperature
    icon: mdi:thermometer-chevron-down
    state: |
      {% set source = states('sensor.weather_temperature') | float(0) %}
      {% if trigger.id == 'reset' %}
        {{ source }}
      {% else %}
        {% set current = (this.state or trigger.to_state.state) | float(source) %}
        {{ [source, current] | min }}
      {% endif %}
    attributes:
      datetime: |
        {% set source = states('sensor.weather_temperature') | float(0) %}
        {% set current = (this.state or trigger.to_state.state) | float(source) %}
        {{ now()  if current < source else this.attributes.datetime | default(now(),1) }}
  - name: "Weather Temperature Max"
    unique_id: weather_temperature_max
    unit_of_measurement: "°C"
    device_class: temperature
    icon: mdi:thermometer-chevron-up
    state: |        
      {% set source = states('sensor.weather_temperature') | float(0) %}
      {% if trigger.id == 'reset' %}
        {{ source }}
      {% else %}
        {% set current = (this.state or trigger.to_state.state) | float(source) %}
        {{ [source, current] | max }}
      {% endif %}
    attributes:
      datetime: |
        {% set source = states('sensor.weather_temperature') | float(0) %}
        {% set current = (this.state or trigger.to_state.state) | float(source) %}
        {{ now() if current > source else this.attributes.datetime | default(now(),1)}}
3 Likes

Thank you very much, that is working flawlessly!