Calculate daily average but depending on another variable

Hello,

I do have my gas heater connected via MQTT to Homeassistant. I delivers two values, the boiler temperature and the position of the valve. The valve switches between the heating system and the hotwater system. What I want to do now is to calculate the daily average temperature, but count only the values when the valve is switched to heating. At the moment I am using the average sensor to calculate the values:

Any ideas how this can be done?
Best regards from Germany
Florian

You need to create a template sensor that only records the heating temperature.

something like this:

template:
  - sensor:
      name: "Heating Temperature"
      device_class: temperature
      state_class: measurement
      unit_of_measurement: "°C" # or °F
      state: >
        {% if is_state('binary_sensor.your_valve', 'on') %}
          {{ states('sensor.your_temperature_sensor') }}
        {% else %}
          unknown
        {% endif %}

Note: you will probably have to change the valve sensor, I don’t know if it is a binary sensor or a sensor and if it is off or on when heating or some other value if a sensor as you did not provide this state information. Entity ids are good info to provide in future questions too.

Then feed this new sensor (sensor.heating_temperature) to your average integration.

Thanks for this solution, it works like a charm!
Much easier than expected :grin:

I have two additional question:
I want to do a long term analysis and therefore want to compare the average heating temperature and the outside temperature. How can I trigger the calculation of the averages only once a day (e.g. 23:59) and store it?

The second question is can there be a plot daily average heating temp vs. Outside temperature? If this is not possible in Homeassistant I will export the daily averages and analyse them in Excel or MATLAB.

  1. time triggered template sensor or automation.

  2. influxdb and Grafana.

I’m failing in defining a triggered template sensor as I already have a state triggered template sensor. Can someone explain me how this need to be combined?

#template sensors
template:
  - sensor:
      name: "Heizung_Kessel_bereinigt"
      device_class: temperature
      state_class: measurement
      unit_of_measurement: "°C" # or °F
      state: >
        {% if (states('sensor.heizung_ventil')|int > 1500) %}
          {{ states('sensor.heizung_kessel') }}
        {% else %}
          unknown
        {% endif %}


  - trigger:
      - platform: time
        at: '23:59:59'
        sensor:
          - name: "mean_heizung_kessel_daily"
            device_class: temperature
            state_class: measurement
            unit_of_measurement: "°C" # or °F
            state: >
              {{ states('sensor.vl_temp_gemittelt_ohneww') }}

  - trigger:
      - platform: time
        at: '23:59:59'
        sensor:
          name: "mean_aussentemp_daily"
          device_class: temperature
          state_class: measurement
          unit_of_measurement: "°C" # or °F
          state: >
            {{ states('sensor.aussentemp_gemittelt') }}

Actually this does not make sense. How can it be an average if you are only taking one reading?

The average is calculated over the whole day by the use of the average sensor:

 - platform: average
      name: 'VL_Temp_gemittelt_ohneWW'
      start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
      end: '{{ now() }}'      
      entities:
      - sensor.heizung_kessel_bereinigt

      
    - platform: average
      name: 'Aussentemp_gemittelt'
      start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
      end: '{{ now() }}'
      entities:
      - sensor.Aussentemperatur

I want to use the template sensor to store the average over the whole day to another sensor which only contains one value per day. Then I want to create a plot which shows the daily average outside temperature on the x-axis a d the daily average heating temperature on the x-axis with one data points per day.

You don’t need a sensor for each day to do this. All the previous days averages are stored in the recorder history.

All you need is a better graph card. Both the mini-graph-card and apexcharts can do this. e.g. this is one sensor graphed for 7 days (apexcharts):

Screenshot 2021-12-08 at 19-57-15 Overview - Home Assistant

type: custom:apexcharts-card
apex_config:
  chart:
    height: 140%
  dataLabels:
    background:
      enabled: false
    style:
      colors:
        - var(--primary-text-color)
graph_span: 1w
span:
  end: day
header:
  show: true
  title: Daily Rainfall
experimental:
  color_threshold: true
series:
  - entity: sensor.weatherflow_precipitation_today
    type: column
    show:
      datalabels: true
    group_by:
      func: last
      duration: 1d
    unit: mm
    color_threshold:
      - color: '#039BE5'
        value: 0
      - color: '#0da035'
        value: 2.5
      - color: '#e0b400'
        value: 5
      - color: '#e45e65'
        value: 10

Mini-graph-card:

Screenshot 2021-12-08 at 20-00-21 Overview - Home Assistant

aggregate_func: last
color_thresholds:
  - color: '#e45e65'
    value: 0
  - color: '#039BE5'
    value: 12
  - color: '#e0b400'
    value: 18
  - color: '#0da035'
    value: 24
color_thresholds_transition: hard
entities:
  - sensor.energy_to_grid_daily
group_by: date
hour24: true
hours_to_show: 168
name: Daily Energy To Grid
show:
  extrema: true
  fill: fade
  graph: bar
  labels: false
smoothing: false
lower_bound: 0
type: custom:mini-graph-card

Both of these cards are available through HACS.

Thanks for your reply, that will help for the visualisation in home assistant. But is there a way to create a sensor that holds one value per day? Because my thoughts are that this is much easier to store only that single average value to influxdb and then to make long-term visualisation within grafana.

You can do exactly the same thing in Grafana with one sensor. You just group the data by days:

The “Last” selector is essentially your 23:59:59 trigger for the group of a day’s data.

Okay thanks, I’ll try this in grafana.
Just for my understanding: Can you tell me what should be the right syntax for the definition of both time and stat triggered template sensors I posted above?

Looks to me like you have the correct syntax. What error are you getting?

This is the error message I see in the logfile after restarting Home Assistant:

Logger: homeassistant.config
Source: config.py:464
First occurred: 7. Dezember 2021, 22:25:39 (8 occurrences)
Last logged: 7. Dezember 2021, 22:35:21

Invalid config for [template]: [sensor] is an invalid option for [template]. Check: template->sensor. (See ?, line ?).

Ah, now I see it. sensor: needs to be indented the same amount as trigger:

  - trigger:
      - platform: time
        at: '23:59:59'
    sensor:
      - name: "mean_heizung_kessel_daily"
        device_class: temperature
        state_class: measurement
        unit_of_measurement: "°C" # or °F
        state: >
          {{ states('sensor.vl_temp_gemittelt_ohneww') }}

  - trigger:
      - platform: time
        at: '23:59:59'
    sensor:
      name: "mean_aussentemp_daily"
      device_class: temperature
      state_class: measurement
      unit_of_measurement: "°C" # or °F
      state: >
        {{ states('sensor.aussentemp_gemittelt') }}

Ah got it. Thank you so much for your help.
I’m still new to Homeassistant and trying to get familiar with YAML :slight_smile: