Only read / record value while another state is on

I have a pool pump that is controlled with a sonoff basic. (MQTT)
I have also connected a temperature sensor to the basic, to give me the pool temperature in home assistant.
These 2 values are displaying and working correctly in home assistant.
The temperature sensor is connected to the pipework so it only reads an accurate pool temperature while the pump is running, all other times it tends to follow the ambient outdoor temperature.
The issue is the temperature, i would like a value that is only updated when the pump is running and also maintain the maximum value attained during the pump runtime.
this way i have a constant (reasonably accurate) display of the pool temperature even while the pump is off.
is there a way to set up home assistant to only read a sensor value when a switch is ā€˜onā€™ ?

Not sure if itā€™s possible to do exactly what you want, but maybe a compromise is to create a template sensor whose value is the value of the temperature sensor when the pump is on, and its own previous value when the pump is off. Thatā€™s very easy to do. Then you could also create a Statistics Sensor that would provide the max value of the template sensor via one of its attributes.

1 Like

I am bumping this old thread because while it references a possible solution, there isnā€™t one documented. I have the same exact scenario as @Jon_ctrl. This is what I tried:

  - sensor:
    - unique_id: pool_temperature_pump_on
      name: Pool Temperature with Pump On
      state: >
        {% if is_state('switch.pool_pump', 'on') %}
          {{ states('sensor.pool_heater_water_temperature') }}
        {% else %}
          {{ states('sensor.pool_temperature_pump_on') }}
        {% endif %}

ā€¦but, of course, with the pump off, the value of the template sensor goes to ā€œunknown.ā€ What I really want is for this template sensor to remain at the last ā€œvalidā€ value when the pump is off.

EDIT: I found this thread and the following seems to work:

  - sensor:
    - unique_id: pool_temperature_corrected
      name: Pool Temperature Corrected
      device_class: temperature
      unit_of_measurement: "Ā°F"
      state: >
        {% set x = states('sensor.pool_heater_water_temperature')|float %}
        {% if is_state('switch.pool_pump', 'on') and x > 0 %}
          {{ x }}
        {% else %}
          {% set x = states('sensor.pool_temperature_corrected')|float %}
          {{ x if x > 0 else 1 }}
        {% endif %}

It will require some more testing, but so far so good.

So that thread is pretty old. Therefore you should make a couple adjustments to accommodate changes made in newer versions of HA:

- sensor:
    - unique_id: pool_temperature_corrected
      name: Pool Temperature Corrected
      device_class: temperature
      unit_of_measurement: "Ā°F"
      state: >
        {% set x = states('sensor.pool_heater_water_temperature')|float(-1) %}
        {% if is_state('switch.pool_pump', 'on') and x > 0 %}
          {{ x }}
        {% else %}
          {% set x = this.state|float(-1) %}
          {{ x if x > 0 else 1 }}
        {% endif %}

Basically thereā€™s just two specific changes you should make to modernize it:

  1. At the time that template was written the float filter defaulted to 0 when given a non-numeric value. Thatā€™s been deprecated. You must do float(#) if you want it to default to something for non-numerics. Currently it will still work but youā€™ll get a warning logged telling that will stop working very soon.
  2. In 2022.5 you are able to use this to refer to the state of the current sensor. This is a better approach and not just because its shorter. If you do states('sensor.pool_temperature_corrected') then this template will listen for changes to itself and trigger updates when it changes. Thatā€™s not ideal and can create performance problems or infinite loops if you arenā€™t careful. Using this to self-reference avoids this problem and is essentially a safe self-reference.
3 Likes

Iā€™m getting an unexpected value from this template:

        {% set x = states('sensor.pool_heater_water_temperature')|float(-1) %}
        {% if is_state('binary_sensor.pool_temperature_sensor_stabilized', 'on') and x > 0 %}
          {{ x }}
        {% else %}
          {% set x = this.state|float(-1) %}
          {{ x if x > 0 else 1 }}
        {% endif %}

There are times when binary_sensor.pool_temperature_sensor_stabilized is in state off that the template value is on, rather than a float value. Any reason why that might happen?

EDIT: Iā€™m investigating in History to see what might have happened. It looks like binary_sensor.pool_temperature_sensor_stabilized went unavailable for 12 seconds on Saturday morning, when it was off prior to and directly after that. While I donā€™t know why that would trigger a change in the value of the template, it happened at exactly the same time as the template value changing to on.

Not sure but you could add an availability template to protect against bad input by having it go unavailable