Temperature statistics

hello,
I want to collect dumb statistics value for a temperature sensor.
with the integration add-on “variable” I have setup 30 variables for each day of a month (31 is forgotten)
every day at 08:00 I store the temperature in a variable something like
t1_{{now.day()}}={{state(sensor.thermo1.temperature)}} *** it works***

now each 30 of a month I want to compute the mean of the 30 days and store in the 12 variables
t1_jan , t1_fev… t1_dec. how can I do this ?

thanks for your help

Post the automation you are using.

alias: dailytemp
description: “”
trigger:

  • platform: time
    at: “08:00:00”
    condition: []
    action:
  • service: var.set
    data:
    entity_id: var.day
    value: “{{ now().day }}”
  • service: var.set
    data:
    entity_id: var.t1_{{states(‘var.day’)}}
    value: “{{ (states(‘sensor.thermo1_temperature’) | int) }}”
    mode: single

Then I have try a similaire way for the monthly but I not able to add number, so I try this a template but it does not work.

alias: monthlytemp
description: “”
trigger:

  • platform: time
    at: “08:00:00”
    condition: []
    action:
  • service: var.set
    data:
    entity_id: var.month
    value: “{{ now().month }}”
  • service: var.set
    data:
    entity_id: var.t1_m{{states(‘var.month’)}}
    value: “{{ (states(‘sensor.t1_mean’) | int) }}”

and the template definiton:

  t1_mean:
    friendly_name: 'sensor thermo1 avg temp'
    value_template: "{{ average[(states('var.t1_1') | int),(states('var.t1_2') | int),(states('var.t1_3') | int),  \

(states(‘var.t1_4’) | int),(states(‘var.t1_5’) | int),(states(‘var.t1_6’) | int),(states(‘var.t1_7’) | int), \
(states(‘var.t1_8’) | int),(states(‘var.t1_9’) | int),(states(‘var.t1_10’) | int),(states(‘var.t1_11’) | int),
(states(‘var.t1_12’) | int),(states(‘var.t1_13’) | int),(states(‘var.t1_14’) | int),(states(‘var.t1_15’) | int),
(states(‘var.t1_16’) | int),(states(‘var.t1_17’) | int),(states(‘var.t1_18’) | int),(states(‘var.t1_19’) | int),
(states(‘var.t1_20’) | int),(states(‘var.t1_21’) | int),(states(‘var.t1_22’) | int),(states(‘var.t1_23’) | int),
(states(‘var.t1_24’) | int),(states(‘var.t1_25’) | int),(states(‘var.t1_26’) | int),(states(‘var.t1_27’) | int),
(states(‘var.t1_28’) | int),(states(‘var.t1_29’) | int),(states(‘var.t1_30’)| int)] }}"

syntax is ok, but it never compute. please help

Copy-paste the following template into the Template Editor to test it. Don’t be tempted to change the 31 to 30. The range() function requires the second number to be one higher than the maximum you want.

      {% set ns = namespace(total= 0) %}
      {% for i in range(1, 31) %}
        {% set ns.total = ns.total + states('var.t1_' ~ loop.index) | int(0) %}
      {% endfor %}
      {{ ns.total / 30 }}

If it reports the correct average value of all sensors in January, you can use the template for sensor.t1_mean

  t1_mean:
    friendly_name: 'sensor thermo1 avg temp'
    value_template: >
      {% set ns = namespace(total = 0) %}
      {% for i in range(1, 31) %}
        {% set ns.total = ns.total + states('var.t1_' ~ loop.index) | int(0) %}
      {% endfor %}
      {{ ns.total / 30 }}

whaaaaou works perfectly.
thanks very munch.

and if a value change the mean change as well (as documented)
now I need to be more accurate in temperature because my (| int) round the value (22.6 become 22)

Change the int(0) to float(0).

  t1_mean:
    friendly_name: 'sensor thermo1 avg temp'
    value_template: >
      {% set ns = namespace(total = 0) %}
      {% for i in range(1, 31) %}
        {% set ns.total = ns.total + states('var.t1_' ~ loop.index) | float(0) %}
      {% endfor %}
      {{ ns.total / 30 }}