Binary Sensor value template with substraction of two different temperature sensor's value

Dear Community,

I’d like to make a new automation to control my hot water circulation valve. For this I have a temperature sensor in my hot water tank, and another one measuring the water’s temperature inside the circulation pipe.

I want to start the circulation valve if the difference between the pipe’s and the tank’s water temperature equals or bigger than -8 degrees celcius (meaning when I have 45 degrees water temp in the tank, and 37 degrees water temp in the pipe, the valve kicks in, and delivers 45 degrees hot water to the sink), and stop the valve if the difference equals or smaller than -1 degree celsius (and of course consider some conditions, like we’re at home, alarm system is on/off, etc.) to save energy as much as possible. Currently it is operated by a dumb daily schedule, which is inefficient and also inconvenient at the same time.

To trigger the two separate automations with different conditions to turn on and off the valve, I thought of creating a binary sensor which turns ON if the difference of the two temperature sensor falls into the required range, and turn OFF, if into the other. State change of this binary sensor would trigger my automations respectively.

For the love of God I could not fabricate the needed mathematical calculus within a value template, thus I’m asking for the help of the beloved HA community. Tried to make it happen in developer tools, failed, tried to look for similar solutions in the forums, also failed, my bad, but not my ignorance :frowning:

Logic I’d like to achieve:

  • binary sensor state is ON, while:
    sensor.shelly1_8caab57452f2_temperature (pipe temp) MINUS sensor.daikin_altherma_dhwtanktemp (water tank temp) EQUALS OR LESS than -8 degrees
  • binary sensor state is OFF, while:
    sensor.shelly1_8caab57452f2_temperature (pipe temp) MINUS sensor.daikin_altherma_dhwtanktemp (water tank temp) EQUALS OR GREAT than -1 degrees

Many thanks in advance, if you have advice on the whole automation setup as well, would be also greatly appreciated.

Template binary sensor aligned to your “Logic I’d like to achieve” (which doesn’t match your initial wording):

template:
  - binary_sensor:
      - name: "Circulation valve operation"
        state: >
          {% set diff = states('sensor.shelly1_8caab57452f2_temperature')|float(0) - 
                        states('sensor.daikin_altherma_dhwtanktemp')|float(0) %}
          {% if diff <= -8 %}
            on
          {% elif diff >= -1 %}
            off
          {% else %}
            {{ this.state }}
          {% endif %}

The {% else %} statement retains the existing value if the temperature difference isn’t in either of your conditions.

1 Like

wow, thank you very much for your very very fast and effective help @Troon !!!

yes, and I see the mistake I made between the first paragraph and the “logic” part, and the logic part is the correct, hence the aimed negative temp difference, thank you very much!!!

1 Like

Dear @Troon

Thanks again for your help, it almost works flawlessly, but your formula is perfect! I bumped into a small obstacle on the automation part: since the trigger is the actual state change, I’m having a hard time to start the automation, where I have a condition to only run the automation in a certain time range (between 6:00 and 23:59), but the state of this binary sensor already changed to ON before this time range. If it is already on before the time condition passes, the automation will not start. It will, obviously, when the next time the sensor will turn on, but it’s not what I’d like to achieve. What I’d like to have in this case, is this: IF the time condition passes, so it’s 06:00:01, and at the same moment the sensor state IS already ON, then fire action.

Can anyone give me some advice how to handle situations like this, automation wise?

My automation as it is right now:

alias: Turn hot water circluation ON
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.cirkulacios_homerseklet_igeny
    to: "on"
    for:
      hours: 0
      minutes: 0
      seconds: 5
condition:
  - condition: time
    after: "06:00:00"
    before: "23:59:00"
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
      - sat
      - sun
  - condition: or
    conditions:
      - condition: device
        device_id: XXXXX
        domain: device_tracker
        entity_id: device_tracker.1
        type: is_home
      - condition: device
        device_id: XXXXX
        domain: device_tracker
        entity_id: device_tracker.2
        type: is_home
      - condition: state
        entity_id: alarm_control_panel.area_1
        state: disarmed
action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.shellyplug_s_51d5f9
mode: single

Add an additional Time trigger for 06:00 (maybe 06:01 to avoid clashing with your time condition), and an additional condition that the sensor is on.

1 Like

ah, clever, so it’s a 3rd automation, trigger wil be time only (06:01:00), condition binary sensor state is on (and device trackers and alarm control panel state)

should’ve thought it.

many thanks!!!

No, it’s a build of your existing one:

alias: Turn hot water circluation ON
description: ""
trigger:
  - platform: state
    entity_id: binary_sensor.cirkulacios_homerseklet_igeny
    to: "on"
    for: "00:05:00"
  - platform: time
    at: "06:01:00"
condition:
  - condition: time
    after: "06:00:00"
    before: "23:59:00"
  - condition: state
    entity_id: binary_sensor.cirkulacios_homerseklet_igeny
    state: "on"
  - condition: or
    conditions:
      - condition: device
        device_id: XXXXX
        domain: device_tracker
        entity_id: device_tracker.1
        type: is_home
      - condition: device
        device_id: XXXXX
        domain: device_tracker
        entity_id: device_tracker.2
        type: is_home
      - condition: state
        entity_id: alarm_control_panel.area_1
        state: disarmed
action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.shellyplug_s_51d5f9
mode: single
1 Like

ah, nice, multiple triggers, and an additional condition, got it, many thanks :smiley:

Dear @Troon

I’d like to bother you a little bit more, I hope you don’t mind.

I noticed, that sometimes the pipe’s temp seeks above the water tank temp. Since we are substracting tank water temp from pipe temp, the math gives positive number, and the circulation does not stop, thus draining the temp further from the tank…

could you help me to modify the binary sensor value template to also switch the state to OFF in case the pipe temp is HIGHER than the tank water temp?

Here’s a graph to show you what I mean:

It should already do that, as positive numbers are greater than -1. Can you paste a history chart for just the second arrow section, also showing the binary sensor as well as the valve position? I wonder if there are spurious measurements that are interfering with the “for 5 minutes” in the trigger…

1 Like

you mean like this?

This is the actual Turn off valve automation:

alias: Melegvíz keringetés kikapcsol
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.cirkulacios_homerseklet_igeny
    to: "off"
    for:
      hours: 0
      minutes: 0
      seconds: 0
  - platform: time
    at: "00:01:00"
condition: []
action:
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.shellyplug_s_51d5f9
mode: single

That looks like the sensor is working as expected, but the valve is not following it.

Why did the valve not turn off at about 18:10, and why did it turn off at about 00:40 instead of 00:01? Is there anything in the log, or traces from the automation?

Is the “valve operation” line showing switch.shellyplug_s_51d5f9 or some other entity?

Is the “valve operation” line showing switch.shellyplug_s_51d5f9 or some other entity?

correct.

Why did the valve not turn off at about 18:10, and why did it turn off at about 00:40 instead of 00:01? Is there anything in the log, or traces from the automation?

hm, indeed, strange, I don’t see any error message in the log, automation traces only gives 5 recent trigger point to check, so I don’t know what happened at 18:14 yesterday, when the binary sensor changed to off, but the automation did not get triggered:

according to the log I was still awake at 00:40, and turned the valve off directly :smiley:

This is the lgbook of the valve (shellyplug) itself:
Melegvíz keringetés bekapcsol=Turn ON valve
Melegvíz keringetés kikapcsol=Turn OFF valve

Turn off automatiion did not fire at all.

I’m really lost on why it was not firing…

now I caught the problem live:

 Logger: homeassistant.components.automation.melegviz_keringetes_kikapcsol
Source: components/automation/__init__.py:580
Integration: Automation (documentation, issues)
First occurred: 13 June 2023 at 14:29:09 (4 occurrences)
Last logged: 10:39:50

    Error while executing automation automation.melegviz_keringetes_kikapcsol: must contain at least one of entity_id, device_id, area_id.
    Error while executing automation automation.melegviz_keringetes_kikapcsol: Setting state for entity shellyplug-s-51D5F9 failed, state: {'turn': 'off'}, error: DeviceConnectionError()