Relative temp difference

Hi,
I would like to use a temperature sensor above my stove to measure a relative temperature difference to turn the kitchen fan on/off, see pic.
Any suggestions on how to achieve this?
It has to be relative because I don’t have airconditioning and the room temperature in winter and summer can be quite different.

One way is to create a template sensor that calculates the temperature difference. Then make automations that switch the fan on or off using a numerical state trigger that monitors this difference.

sensor:

- platform: template
  sensors:
    temperature_difference:
      friendly_name: "Temperature Difference"
      unit_of_measurement: '°C'
      value_template: >
        {{ states('sensor.stove_top_temp_sensor_here')|float - states('sensor.room_temp_sensor_here')|float }}

Automations:

alias: stove_fan_on
trigger:
  platform: numeric_state
  entity_id: sensor.temperature_difference
  above: 10
action:
  service: switch.turn_on
  entity_id: switch.stove_fan
alias: stove_fan_off
trigger:
  platform: numeric_state
  entity_id: sensor.temperature_difference
  below: 5
action:
  service: switch.turn_off
  entity_id: switch.stove_fan

You would have to reload automations (rather than restart) if fine tuning the trip points. If that is too much hassle you could create a couple of input numbers for the on and off trip point temperature differentials. The automation triggers would then have to be template triggers. But honestly once you get the values right I don’t see it changing much so it’s probably not worth the hassle.

4 Likes

Thank you very much! That did it

1 Like

Just came across this and I am using it successfully to turn on a ceiling fan. Sometimes the temperature difference is expressed in 15 decimal places though! How could I make it display in just one or two decimals?
Thanks.

{{ ( states('sensor.stove_top_temp_sensor_here')|float(0) - states('sensor.room_temp_sensor_here')|float(0) )|round(2) }}

Perfect, thank you.

Thank you very much. I used the template to evaluate the difference between the wall thermostat and a Tasmota thermostat on the fire stove to control whether a window must be opened before the cooker hood can be used.

I have created a sensor. But how can I chart the results in history graph?

My sensor is:

template:
  - binary_sensor: 
      - name: temp_diff_gh
        unit_of_measurement: '°C'
        value_template: >
         {{ states('sensor.0x60a423fffe3c5d9b_temperature')|float - states('sensor.0xa4c138ec379fb0d6_temperature')|float }}
1 Like

Hi, im trying to get the following to work for hours now, but dont know what is wrong, keep telling me Message malformed: extra keys not allowed @ data['0']

Can anyone please help.

- alias: Pomp WP wisselaar in en uitschakelen 
  platform: template
  sensors:
    temperature_difference:
      friendly_name: Temperature Difference
      unit_of_measurement: °C
      value_template: >
        {{ ( states('sensor.tasmota_basic_ds18b20_ds18b20_temperature')|float(0) - states('sensor.thr316d_ht1_ds18b20_temperature')|float(0) )|round(2) }}
      alias: pommp aan
      trigger:
      platform: numeric_state
      entity_id: sensor.temperature_difference
      above: 10
    action:
      service: switch.turn_on
      entity_id: switch.4ch_ht8_pomp_hottub_ww_warmtepomp
      alias: pomp uit
  trigger:
      platform: numeric_state
      entity_id: sensor.temperature_difference
      below: 5
  action:
      service: switch.turn_off
      entity_id: switch.4ch_ht8_pomp_hottub_ww_warmtepomp

This whole sensor definition does not belong in your automaton configuration.

Put this in your configuration.yaml file, do a config check then if that passes restart home assistant:

template:
  - sensor:
      - name: Temperature Difference
        unit_of_measurement: °C
        device_class: temperature
        state_class: measurement
        state: >
          {{ ( states('sensor.tasmota_basic_ds18b20_ds18b20_temperature')|float(0) - states('sensor.thr316d_ht1_ds18b20_temperature')|float(0) )|round(2) }}
        availability: >
          {{ has_value('sensor.tasmota_basic_ds18b20_ds18b20_temperature') and has_value('sensor.thr316d_ht1_ds18b20_temperature') }}

Then write your two automations (in yaml or using the UI):

    trigger:
      platform: numeric_state
      entity_id: sensor.temperature_difference
      above: 10
    action:
      service: switch.turn_on
      entity_id: switch.4ch_ht8_pomp_hottub_ww_warmtepomp
      alias: pomp uit
    trigger:
      platform: numeric_state
      entity_id: sensor.temperature_difference
      below: 5
    action:
      service: switch.turn_off
      entity_id: switch.4ch_ht8_pomp_hottub_ww_warmtepomp

Hi Tom

Thx it works, j never realised I had to creatie a difference sensor its clear now

I have 1 order thing.

I only want to heat sertain times, but when the temp is alraidy below the trigger point the heater wont trigger

Is need a trigger wich not only trigger on the setpoint switch but also everything below

Is that possible?

The trick is not in the setpoint trigger, but to also add a time trigger for the start time. And you should duplicate the temperature test in the conditions.

Hi Edwin

Thx for the reply

Id dit i gues, but again no heating this morning

Can you see what I did worg?

alias: Badkamer - verwarming 20 graden
description: ""
trigger:
  - platform: numeric_state
    entity_id:
      - sensor.th16_bk1_si7021_temperature
    above: 20
    id: Badkamer 20+
    enabled: true
  - platform: numeric_state
    entity_id:
      - sensor.th16_bk1_si7021_temperature
    below: 19.5
    id: Badkamer 20-
condition:
  - condition: time
    after: "08:00:00"
    weekday:
      - sun
      - sat
    before: "10:00:00"
    enabled: true
  - condition: time
    after: "20:00:00"
    weekday:
      - sun
      - sat
    before: "23:30:00"
    enabled: true
  - condition: time
    before: "16:00:00"
    after: "07:00:00"
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
    enabled: true
  - condition: time
    before: "23:30:00"
    after: "21:30:00"
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
    enabled: true
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - Badkamer 20+
        sequence:
          - service: switch.turn_off
            data: {}
            target:
              entity_id: switch.th16_bk1_badkamer_verwarming
          - service: notify.mobile_app_iphone_van_henk
            data:
              message: Badkamer verwarming uit
              title: Melding
      - conditions:
          - condition: trigger
            id:
              - Badkamer 20-
        sequence:
          - service: switch.turn_on
            data: {}
            target:
              entity_id: switch.th16_bk1_badkamer_verwarming
          - service: notify.mobile_app_iphone_van_henk
            data:
              message: Badkamer verwarming aan
              title: Melding
mode: single

What I said. the only moments that this is automation is evaluated is when the temperature crosses a threshold. If the time conditions block it, nothing happens. So, instead of just temperature triggers and time conditions, you also need time triggers and temperature conditions. You want it to check at the start of the time block what the temperature is.

I would also introduce a schedule helper entity (create one in the helpers section). It would greatly simplify the automation. Alternatively you could use a local calendar entity if some weeks are different than others.

Hi Edwin.

oke, so this will work then?`
or would you advise me to add extra triggers

alias: Badkamer - verwarming 20 graden
description: ""
trigger:
  - platform: state
    entity_id:
      - schedule.badkamer_verwarming_aan
    from: "off"
    to: "on"
    id: Schema aan
  - platform: state
    entity_id:
      - schedule.badkamer_verwarming_aan
    from: "on"
    to: "off"
    id: schema uit
condition:
  - type: is_temperature
    condition: device
    device_id: 04cd8782f6dcc41f19fdac6384a2ed62
    entity_id: e8f72faa0ce2bce9da2473f3946b9e19
    domain: sensor
    below: 20
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - schema uit
        sequence:
          - service: switch.turn_off
            data: {}
            target:
              entity_id: switch.th16_bk1_badkamer_verwarming
          - service: notify.mobile_app_iphone_van_henk
            data:
              message: Badkamer verwarming uit
              title: Melding
      - conditions:
          - condition: trigger
            id:
              - Schema aan
        sequence:
          - service: switch.turn_on
            data: {}
            target:
              entity_id: switch.th16_bk1_badkamer_verwarming
          - service: notify.mobile_app_iphone_van_henk
            data:
              message: Badkamer verwarming aan
              title: Melding
mode: single

This would do the situations you missed beforehand, but would then miss temperatures dropping while the schedule is already on. So you also need the original triggers you had for temperatures crossing thresholds. Those were fine - you need both as triggers.

There is a threshold entity for that as well in the helpers section by the way, I use them a lot. They create a binary sensor that goes on or off when the numerical state is outside bounds. I’d recommend that too - it is a matter of taste what you like best.

Always when you have multiple conditions that need to be true for something to happen, then you need multiple triggers to look for the corresponding changes too. So HA needs to check both when the schedule changes and when the temperature changes (triggers), and then use both the state of the schedule and the temperature to decide what to do.

Oke Edwin,

I start working on that

One more question if i may

Ik have added a second template sensor but its not showing afther restart
What is wrong?

this one: Temp verschil solarkoepel uit-hottub

template:
  - sensor:
      - name: "Temp verschil wp wisselaar-hottub"
        unit_of_measurement: "°C"
        device_class: "temperature"
        state_class: "measurement"
        state: >
          {{ ( states('sensor.tasmota_basic_ds18b20_ds18b20_temperature')|float(0) - states('sensor.thr316d_ht1_ds18b20_temperature')|float(0) )|round(2) }}
        availability: >
          {{ has_value('sensor.tasmota_basic_ds18b20_ds18b20_temperature') and has_value('sensor.thr316d_ht1_ds18b20_temperature') }}

      - name: "Temp verschil solarkoepel uit-hottub"
        unit_of_measurement: "°C"
        device_class: "temperature"
        state_class: "measurement"
        state: >
          {{ ( states('sensor.thr316d_ht4_ds18b20_temperature')|float(0) - states('sensor.thr316d_ht1_ds18b20_temperature')|float(0) )|round(2) }}
        availability: >
          {{ has_value('sensor.thr316d_ht4_ds18b20_temperature') and has_value('sensor.thr316d_ht1_ds18b20_temperature') }}

found the example here:

Adding multiple sensors values into one! - Configuration - Home Assistant Community (home-assistant.io)

oke i think i have it like this right?

alias: Badkamer - verwarming 20 graden
description: ""
trigger:
  - platform: state
    entity_id:
      - schedule.badkamer_verwarming_aan
    from: "off"
    to: "on"
    id: Schema aan
  - platform: state
    entity_id:
      - schedule.badkamer_verwarming_aan
    from: "on"
    to: "off"
    id: schema uit
  - platform: state
    entity_id:
      - binary_sensor.temperatuur_badkamer
    from: "on"
    to: "off"
    id: Temperatuur schema Badkamer uit
  - platform: state
    entity_id:
      - binary_sensor.temperatuur_badkamerv
    from: "off"
    to: "on"
    id: Temperatuur schema Badkamer aan
condition:
  - type: is_temperature
    condition: device
    device_id: 04cd8782f6dcc41f19fdac6384a2ed62
    entity_id: e8f72faa0ce2bce9da2473f3946b9e19
    domain: sensor
    below: 20
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - schema uit
              - Temperatuur schema Badkamer uit
        sequence:
          - service: switch.turn_off
            data: {}
            target:
              entity_id: switch.th16_bk1_badkamer_verwarming
          - service: notify.mobile_app_iphone_van_henk
            data:
              message: Badkamer verwarming uit
              title: Melding
      - conditions:
          - condition: trigger
            id:
              - Schema aan
              - Temperatuur schema Badkamer aan
        sequence:
          - service: switch.turn_on
            data: {}
            target:
              entity_id: switch.th16_bk1_badkamer_verwarming
          - service: notify.mobile_app_iphone_van_henk
            data:
              message: Badkamer verwarming aan
              title: Melding
mode: single

As for the template sensors in your previous post, I see no obvious reason why they would be wrong. Do the logs say anything?

As for your new automation:

The triggers look fine.

The condition I’m not so sure. If the temp is above 20 the automation never does anything. Also not turn the heating off. That seems wrong to me. I think this condition should move to the appropriate choices.

Personally (but I know I’m one of few here looking at all examples you find in the forum) I’d make two automations. One to turn the heating on, one to turn it off. Most people want it together in one automation, but usually it makes stuff way more complicated because, as you see here as well, you need a choose or an if to separate the two actions.

The condition can stay the way it is if you only dedicate the automation to turning the heating on. In pseudocode (because I’m currently not in a position to write actual code easily):

The heating on automation:

trigger:
  - schedule turns on
  - temperature drops to below threshold
condition:
  - temperature is low 
  - schedule is on
action:
  - turn heating on

The heating off version:

trigger:
 - schedule turns off
 - temperature rises above threshold
action:
 - turn heating off

Personally I think this is way easier to read and understand, and less code. And every automation has a single purpose.

And one last tip: stay away from device triggers and conditions. Use (numeric) state instead. If you ever change the sensor you’ll be much happier.

Hi Edwin,

Thank you so mutch for all the help!

After reading your trigger i manished to put it in one automation as follows.

alias: Badkamer - verwarming 20 graden
description: ""
trigger:
  - platform: state
    entity_id:
      - schedule.badkamer_verwarming_aan
    from: "off"
    to: "on"
    id: Tijd om te verwarmen
  - platform: state
    entity_id:
      - schedule.badkamer_verwarming_aan
    from: "on"
    to: "off"
    id: Tijd om niet te verwarmen
  - platform: state
    entity_id:
      - binary_sensor.temperatuur_badkamer
    from: "on"
    to: "off"
    id: Badkamer warm
  - platform: state
    entity_id:
      - binary_sensor.temperatuur_badkamer
    from: "off"
    to: "on"
    id: Badkamer koud
condition: []
action:
  - choose:
      - conditions:
          - condition: or
            conditions:
              - condition: trigger
                id:
                  - Tijd om niet te verwarmen
                  - Badkamer warm
        sequence:
          - service: switch.turn_off
            data: {}
            target:
              entity_id: switch.th16_bk1_badkamer_verwarming
          - service: notify.mobile_app_iphone_van_henk
            data:
              message: Badkamer verwarming uit
              title: Melding
        alias: Verwarming uit
      - conditions:
          - condition: or
            conditions:
              - condition: trigger
                id:
                  - Tijd om te verwarmen
                  - Badkamer koud
        sequence:
          - service: switch.turn_on
            data: {}
            target:
              entity_id: switch.th16_bk1_badkamer_verwarming
          - service: notify.mobile_app_iphone_van_henk
            data:
              message: Badkamer verwarming aan
              title: Melding
        alias: Verwarming aan
mode: single

I meshed up the condition bij using ‘and’ but it should be ‘or’

i think its oke now, it gives me a cleaner result to have it in 1 automation i think

Have yourself a good weekend

ps:

My template works now as well

I`m pretty happy with the last one, even when 1 of both temp sensors drop out the avarage sensor continue to work, it a very important one and it should

# Temp verschil tussen hottub en WP wisselaar

  - sensor:
      - name: "Temp verschil wp wisselaar-hottub"
        unit_of_measurement: "°C"
        device_class: "temperature"
        state_class: "measurement"
        state: >
          {{ ( states('sensor.tasmota_basic_ds18b20_ds18b20_temperature')|float(0) - states('sensor.thr316d_ht1_ds18b20_temperature')|float(0) )|round(2) }}
        availability: >
          {{ has_value('sensor.tasmota_basic_ds18b20_ds18b20_temperature') and has_value('sensor.thr316d_ht1_ds18b20_temperature') }}

# Temp verschil tussen hottub en solarkoepel uit

      - name: "Temp verschil solarkoepel uit-hottub"
        unit_of_measurement: "°C"
        device_class: "temperature"
        state_class: "measurement"
        state: >
          {{ ( states('sensor.thr316d_ht4_ds18b20_temperature')|float(0) - states('sensor.thr316d_ht1_ds18b20_temperature')|float(0) )|round(2) }}
        availability: >
          {{ has_value('sensor.thr316d_ht4_ds18b20_temperature') and has_value('sensor.thr316d_ht1_ds18b20_temperature') }}
          
# Temp verschil tussen hottub en solarkoepel uit

      - name: "Temp verschil solarkoepel-hottub"
        unit_of_measurement: "°C"
        device_class: "temperature"
        state_class: "measurement"
        state: >
          {{ ( states('sensor.thr316d_ht3_si7021_temperature')|float(0) - states('sensor.thr316d_ht1_ds18b20_temperature')|float(0) )|round(2) }}
        availability: >
          {{ has_value('sensor.thr316d_ht3_si7021_temperature') and has_value('sensor.thr316d_ht1_ds18b20_temperature') }}
          
# Gemiddelde kachel temperatuur van kachel temp 1 en 2 kan zelfs 1 vande 2 uitvallen en word dan ignored

      - name: "Gemiddelde kachel temp"
        unique_id: sensor.gemiddelde_kachel_temp
        unit_of_measurement: "°C"
        device_class: "temperature"
        state_class: "measurement"
        state: >
         {% set t = [states('sensor.thr316d_ht2_ds18b20_temperature'), states('sensor.th16_ht7_ds18b20_temperature')]
                   | reject('eq', 'unavailable') | map('float') | list %}
         {% set qty = t|count %}
         {{ (t|sum/qty)|round(1) if qty > 0 else states('sensor.gemiddelde_kachel_temp') }}

1 Like