Template Sensor exclude value

Hey there!

I have defined two template min/max sensors in my configuration to get the min and max temperature from my weather station including the last 24 hours.
But i noticed, that during my nightly backup and restart the temperature drops to 0 for a second, so either my min or my max sensor is always 0.

How am i able to exclude the value 0 from these two sensors?
This would be my config for them:

- platform: statistics
  entity_id: sensor.aussentemperatur
  name: Außentemperatur Statistik
  sampling_size: 1000
  max_age: "24:00:00"
  precision: 2 #default=2.

- platform: template
  sensors:
    aussentemperatur_temp_max:
      value_template: "{{ state_attr('sensor.aussentemperatur_statistik', 'max_value') }}"
    aussentemperatur_temp_min:
      value_template: "{{ state_attr('sensor.aussentemperatur_statistik', 'min_value') }}"

Hope someone has a solution for me :slight_smile:
Greetings

This will keep the last value if the new value is 0.

- platform: template
  sensors:
    aussentemperatur_temp_max:
      value_template: >
        {% set max = state_attr('sensor.aussentemperatur_statistik', 'max_value')|float(0) %}
        {{ max if max != 0 else states('sensor.aussentemperatur_temp_max') }}
    aussentemperatur_temp_min:
      value_template: >
        {% set min = state_attr('sensor.aussentemperatur_statistik', 'min_value')float(0) }}
        {{ min if min != 0 else states('sensor.aussentemperatur_temp_min') }}

You might want to add a friendly_name, unit_of_measurement and a state_class to each too.

- platform: template
  sensors:
    aussentemperatur_temp_max:
      friendly_name: "Außentemperatur Max"
      unit_of_measurement: "°C"
      state_class: measurement
      value_template: >
        {% set max = state_attr('sensor.aussentemperatur_statistik', 'max_value')|float(0) %}
        {{ max if max != 0 else states('sensor.aussentemperatur_temp_max') }}
    aussentemperatur_temp_min:
      friendly_name: "Außentemperatur Min"
      unit_of_measurement: "°C"
      state_class: measurement
      value_template: >
        {% set min = state_attr('sensor.aussentemperatur_statistik', 'min_value')|float(0) }}
        {{ min if min != 0 else states('sensor.aussentemperatur_temp_min') }}
1 Like

One issue is that this is outdoor temperature and outdoor temperature could be 0.
Can’t updates be discarded during a restart somehow? “If uptime > 1”?

That looks great thank you!

But I’m getting the following error, when checking the configuration:

Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: expected token 'end of statement block', got 'float') for dictionary value @ data['sensors']['aussentemperatur_temp_min']['value_template']. Got "{% set min = state_attr('sensor.aussentemperatur_statistik', 'min_value')float(0) }} {{ min if min != 0 else states('aussentemperatur_temp_min') }}\n". (See ?, line ?).

Do you know why?

@Hellis81 No that’s not the problem, it’s always at 3am, when the backup starts and the real temperature is not going from 5 to 0 to 5 degrees in a second.

In the min sensor, this:

Should be:
)|float(0)

1 Like

The time is irrelevant. Not sure why it would have to be 5 degrees either.

Whenever the temperature is 0 then it will be discarded.
If the temperature is 0 the whole day then you won’t get any data for that day.

Thanks that worked!

But it seems that i have another problem now, maybe it’s after the update to 2021.12, the statistics sensor does not have a max_/min_value attribute anymore, seems like i have to make two statistic sensor with state_characteristic: value_min/_max configuration.
But i don’t know how to make a template sensor from these sensors, because template sensors are made from attributes or? That’s why you write them like this state_attr('sensor.aussentemperatur_statistik', 'min_value').

Edit:
Nevermind, got it working

    aussentemperatur_temp_max:
      friendly_name: "Hoch (24h)"
      unit_of_measurement: "°C"
      #      state_class: measurement
      value_template: >
        {% set max = states('sensor.aussentemperatur_statistik_max')|float(0) %}
        {{ max if max != 0 else states('aussentemperatur_temp_max') }}

changed state_attr with states

Againg thank for your help! :slight_smile:

@tom_l Could you please help me once more?
These are the statistic sensors in my sensor.yaml:

- platform: statistics
  entity_id: sensor.aussentemperatur
  name: Außentemperatur Statistik Max
  sampling_size: 1000
  max_age:
    hours: 24
  precision: 2
  state_characteristic: value_max
- platform: statistics
  entity_id: sensor.aussentemperatur
  name: Außentemperatur Statistik Min
  sampling_size: 1000
  max_age:
    hours: 24
  precision: 2
  state_characteristic: value_min

These are the template sensors in my template.yaml:

- sensor:
    - name: "Hoch (24h)"
      unit_of_measurement: "°C"
      state_class: measurement
      state: >
        {% set max = states('sensor.aussentemperatur_statistik_max')|float(0) %}
        {{ max if max != 0 else states('aussentemperatur_temp_max') }}

    - name: "Tief (24h)"
      unit_of_measurement: "°C"
      state_class: measurement
      state: >
        {% set min = states('sensor.aussentemperatur_statistik_min')|float(0) %}
        {{ min if min != 0 else states('aussentemperatur_temp_min') }}

They are working fine, now the temperature at my house is 5.5 degrees, template sensor displays that.
But if i’m setting the value to 0 or 0.0 the sensor says unknown. Same for the min sensor.
Thought, that the max sensor should stay at 5.5?

Looks like you have changed to the new template format too. This takes the entity id from the name. So sensor.hoch_24h and sensor.tief_24h I think. Check this in the Developer Tools / States page. Then replace the sensors that hold the last measurement in your templates:

- sensor:
    - name: "Hoch (24h)"
      unit_of_measurement: "°C"
      state_class: measurement
      state: >
        {% set max = states('sensor.aussentemperatur_statistik_max')|float(0) %}
        {{ max if max != 0 else states('sensor.hoch_24h') }}

    - name: "Tief (24h)"
      unit_of_measurement: "°C"
      state_class: measurement
      state: >
        {% set min = states('sensor.aussentemperatur_statistik_min')|float(0) %}
        {{ min if min != 0 else states('sensor.tief_24h') }}
1 Like

Yeah, i thought i need to do it anyway, so why not now^^.

But it worked, thanks again for your help :slight_smile: !

1 Like

The new format has pros and cons. Mostly pros. It’s worth doing.

Also I actually missed the sensor. part before aussentemperatur in the original templates. I fixed it up so it does not confuse anyone else.

1 Like

hello me after the last update the old configuration does not work

  • platform: template
    sensors:
    teplota_vonku_max:
    value_template: “{{ state_attr(‘sensor.teplota_vonku_24h’, ‘max_value’) }}”
    teplota_vonku_min:
    value_template: “{{ state_attr(‘sensor.teplota_vonku_24h’, ‘min_value’) }}”

I tried yours and I have an error in the log
error
Invalid config for [sensor.template]: [state_class] is an invalid option for [sensor.template]. Check: sensor.template->sensors->teplota_vonku_24max->state_class. (See ?, line ?).

  - platform: statistics
    entity_id: sensor.teplota_vonk
    name: Teplota vonku 24h
    sampling_size: 1000
    max_age: "24:00:00"
    precision: 2 #default=2.



   - platform: template
    sensors:
      teplota_vonku_24max:
        friendly_name: "teplota vonku 24hmax"
        unit_of_measurement: "°C"
        state_class: measurement
        value_template: >
          {% set max = state_attr('teplota_vonku_24max')|float(0) %}
          {{ max if max != 0 else states('sensor.teplota_vonku_24h') }}
      teplota_vonku_24min:
        friendly_name: "teplota vonku 24hmin"
        unit_of_measurement: "°C"
        state_class: measurement
        value_template: >
          {% set min = state_attr('teplota_vonku_24min')|float(0) %}
          {{ min if min != 0 else states('sensor.teplota_vonku_24h') }}