Creating a differential temperature sensor with hysteresis

Must be being switched on from somewhere else, that automation won’t fire if the sensor is pending. That’s the reason I introduced the pending state so I could add the condition.

Also, even if it did skip the condition somehow, unless the sensor is ‘pump_on’ then the result will be to switch off the pump.

So, if we’re happy it definitely was ‘pending’ it was switched on by some other means. If it was on pump_on I’ll need to have another look at the template.

Can you check the logbook and see which is the case?

Seems switching on and off OK, just not off when state is pending? There are no other automations pertaining to the solar_pump. Could the broker retain message be doing anything?

Hmm, OK, try this for the sensor…

sensor:
  - platform: template
    sensors:
      temp_diff:
        friendly_name: Temperature Difference
        value_template: >
          {% if ((states.sensor.solar_temperature.state | float ) - (states.sensor.pool_temperature.state | float))
            < (states.input_number.pump_off_offset.state | float ) %} pump_off
          {% elif ((states.sensor.solar_temperature.state | float ) - (states.sensor.pool_temperature.state | float))
            > (states.input_number.pump_on_offset.state | float ) %} pump_on
          {% else %} pending {% endif %}

And this for the automation…

automation:
  - id: ID12
    alias: 'Pump hot water if hotter than pool - stop if not'
    initial_state: 'on'
    trigger:
      platform: state
      entity_id: sensor.temp_diff
    action:
      entity_id: switch.solar_pump
      service_template: >
        {% if is_state ('sensor.temp_diff' , 'pump_on') %} switch.turn_on
        {% else %} switch.turn_off {% endif %}

I’ve just reordered the template to make sure it double checks if it should be off before it checks if it should be on, which will hopefully get rid of any false positives (should’ve done that in the first place, sorry!)

Then I’ve removed the condition to ensure that any state change that isn’t ‘pump_on’ switches it off. The automation will fire a little more frequently, but should ensure that the pump is always off when it should be.

Let me know :+1:

Seems you may have nailed it! Thank you so much.

1 Like

No worries :+1:

@anon43302295 My template sensors have died after 0.66 so my ‘Pool Pump Hours Running’ just shows 0 all the time. You see that too?

Everything is fine here, what’s the code for the hours running one?

Seems this one might be the problem?

- platform: history_stats
  name: Pool Pump running today
  entity_id: sensor.pool_pump_status
  state: 'Running'
  type: time
  start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
  end: '{{ now() }}'

Here is all three sensors. Nothing changed with those.

## Pool pump status sensor as switch is hidden:
- platform: template
  sensors:
    pool_pump_status:
      value_template: '{% if is_state("switch.pool_pump", "on") %}Running{% else %}Stopped{% endif %}'
      friendly_name: 'Pool Pump Status'

## Creates a history sensor based on how long the pool pump has been in a
## status of 'running' defined in the aboved value template:
- platform: history_stats
  name: Pool Pump running today
  entity_id: sensor.pool_pump_status
  state: 'Running'
  type: time
  start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
  end: '{{ now() }}'

## Look for the attribute named 'value' from the above sensor to create
## a new sensor using that attribute:
- platform: template
  sensors:
    pool_pump_time_on:
      value_template: '{{ states.sensor.pool_pump_running_today.attributes.value }}'
      friendly_name: 'Pool Pump Hours Running'

Ummm, off the top of my head I’m stumped there tbh.

Let me have a head scratch and come back to you :+1:

Thanks, appreciated :+1:

1 Like

@anon43302295 Sorted! History Graph not working after 0.66.x

1 Like

Oh mate, I’m so sorry I completely forgot about this! Glad you got sorted :+1:

Hi. I have just started with Home Assistant and your differential temperature sensor with hysteresis is just what I am looking for. Would you be willing to share your final code with any improvement you may have added. Did you include a weekday scheduler with your final version? Thanks

Sure. Here you go https://gist.github.com/xbmcnut/299d32f0591d8eef3643af00eb2739fb

Thanks for your prompt response really appreciate your help.
As I am a novice with Home assistant, do I just add this to my configuration.yaml?

Hi I am a beginner with home assistant and I am intending to build a differential temperature sensor with hysteresis to control my swimming pool solar system. I originally contacted xbmcnut and he has provided an initial response about his system but has only supplied some of the details of the setup at this stage. I see from the post that you were involved in the initial coding and wondered if you have the complete package for this project.
I would appreciate any help that you may be able to offer. Thanks

All the code I did is in this thread :man_shrugging:

Thanks for your prompt reply