Pool automatic

Hi there.
Can someone please help me create an automation for 2 tempsensors and 1 switch.

I have a pool and sun heater to that I have a magnetic valve.

Information:
Temp1 = poolwater
Temp2 = rooftop (in the sun)
Switch1 = valve to sun heater.

What I want is that switch1 set to “on” if temp2 is higher than temp1. And then if temp1 gets higher than temp2 the switch1 should set to off.

Is this possible in one automation? I have absolutely no idea how to do this.

I have a suggestion, but make an assumption - at times where the temperatures are oscillating around each other you don’t want the valve to go on-off-on-off-etc. within seconds. So, my suggestion is to run an automation for example every 5 minutes, and compare temperatures.

automation:
  trigger:
    - platform: time_pattern
      minutes: '/5'
      seconds: 00
  action:
    service_template: >
      {% if states.sensor.Temp2.state | float > states.sensor.Temp1.state | float %}
        homeassistant.turn_on
      {% else %}
        homeassistant.turn_off
      {% endif %}
    data:
      entity_id: switch.Switch1

Please note: I haven’t actually tested this.

Personally, I dislike time period automations. What I would do is create a template sensor and make an automation based off that. This is only required because you can’t use the for: attribute for template triggers. There’s a PR for that currently and the functionality should be in the software soon… but until then:

place this in your binary_sensor section

- platform: template
  sensors:
    pool_trigger:
      value_template: >
        {{ states('sensor.temp1') | float > states('sensor.temp2') | float }}

Then your automation would use the for x minutes for turning on/off the pool switch

- alias: Pool Switch
  trigger:
  - platform: state
    entity_id: binary_sensor.pool_temperature
    to: 'off'
    from: 'on'
    for: '00:05:00'
  - platform: state
    entity_id: binary_sensor.pool_temperature
    from: 'off'
    to: 'on'
    for: '00:05:00'
  action:
  - service_template: >
      switch.turn_{{ trigger.to_state.state }}
    entity_id: switch.switch1

So this setup creates an on/off sensor to let you know when the temperature of sensor.temp1 is greater than sensor.temp2. It will update whenever temp1 or temp2 updates. So you don’t need to ‘check it every 5 minutes’. Next, the automation watches the state changes of the on/off sensor. If the state changes and it stays the same for 5 minutes, then the automation will trigger. If it turns on, it will turn on the switch. If it turns off, it will turn off the switch.

Thank you,
This works perfectly and exactly as i wanted.
But one question,
When I open the automation in frontend it says that the “action” is invalid!?

I actually don’t care that much because it works as it supposed to. I’m just curious why it says that and works anyway.

I’m not sure the UI can handle a service_template. This is an ‘advanced’ technique. :man_shrugging:

Ok, I really appreciate your help.
Now I’m struggling with “button”

I want a “button” that turn off the automation and set the switch1 to off if its on. And next press on the button it should activate automation again.

The reason for this is:
When I want to vacuum the pool I don’t want the water to go thru the sunheater due to loss of water pressure.

When vacuum is completed one press on the button activates automation again.

Is this doable?

Scripts are equivalent to buttons. Treat a scripts sequence like the action section of an automation. You can also use a switch template and have the turn on and turn off sections turn on/off the automation and switch.