I only want to record sensor values when another item is switched on

Hi

I’ve looked around for answers but I don’t think I have found a savvy way to achieve the following.

I only want to record temperature values from a probe when another device (a pump) is switched on.

I have a temperature probe which reliably records to HA. I also have the pump status (running/not running) via toggle/switch. The thing is that it is reporting the temperature of water in my pool by placing the probe within the pipes near my pump. When the pump is running, this is a good representation of the pool temperature, but in the evening the water in the narrow pipe drops significantly. This make it SEEM like the pool cooled off a lot during the night when in fact it did not.

I think I would like to only record the temperature when the pump is running, and ignore it otherwise.

Happy to use NodeRed, but would love for this to be config based.

1 Like

Have you tried creating a template sensor where the state depends on the pump running?

....
 {% if states.PUMP = on % }
{{ states.TEMPSENSOR }}
{% else %}
{{ whatevervalue }}
{% endif %}
2 Likes

This seems promising. Going to try this out and will share my results here.

I think the trick will be to determine what to return for “whatevervalue”.

Zero would work.
You will still record data, but when the pump is off, the data recorded will be whatevervalue.

So, I figured that I can just leave the else out of the template. My expectation is that I will not get a value recorded, but perhaps I will get an unpleasant surprise with a blank string or something. Regardless, I am going to try this for tonight and will look at the data tomorrow.

  - sensor: 
     - name: "pool_water_temperature_adjusted"
        state_class: measurement
        unit_of_measurement: °F
        device_class: temperature
        icon: mdi:pool-thermometer
        state: >
          {% if is_state('input_boolean.pool_pump_running', 'on') %}
            {{ states('sensor.pool_water_temperature_2') }}
          {% endif %}

If that doesn’t do what I want, then I was thinking that I could return the last result as the current result. Not sure if this will work as I expected. Comments welcome.

  - sensor:
     - name: "pool_water_temperature_adjusted"
        state_class: measurement
        unit_of_measurement: °F
        device_class: temperature
        icon: mdi:pool-thermometer
        state: >
          {% if is_state('input_boolean.pool_pump_running', 'on') %}
            {{ states('sensor.pool_water_temperature_2') }}
          {% else %}
            {{ states('sensor.pool_water_temperature_adjusted') }}
          {% endif %}

And if that doesn’t work… I can create an input number to record the temperature at the time the pump turned off. Use an automation to set that value, and return that input_number in the {% else %} block.

Lots of options. The template sensor was the right direction. Thanks

The second version is correct. You need the else case. The other option is:

        state: >
          {% if is_state('input_boolean.pool_pump_running', 'on') %}
            {{ states('sensor.pool_water_temperature_2') }}
          {% else %}
            unavailable
          {% endif %}

This a perfect case for an availability template:

state: "{{ states('sensor.pool_water_temperature_2') }}"
availability: "{{ is_state('input_boolean.pool_pump_running', 'on') }}"
1 Like

It is, but only if they want state/unavailable.

1 Like

OK. So I got a working solution that works for me. I added an input_number which holds the temperature that the pool water was at the time the pump is turned off. I then have an automation to set that when the pool turns off. I return that value in the else clause of my template sensor.

In essence, this allows me to pretend that the pool water stayed at the same temperature over night, and then there is a drop when the pump starts running in the morning. Note that in this screen shot it looks like the value spiked at around 6pm, but that is because that is when I decided to add the input_number to the solution.

Template Sensor

      - name: "Pool Water Temperature Adjusted"
        state_class: measurement
        unit_of_measurement: °F
        device_class: temperature
        icon: mdi:pool-thermometer
        state: >
          {% if is_state('input_boolean.pool_pump_running', 'on') %}
            {{ states('sensor.pool_water_temperature_2') }}
          {% else %}
            {{ states('input_number.end_of_day_pool_water_temperature') }}
          {% endif %}

Automation Action

service: input_number.set_value
data:
  value: "{{states('sensor.pool_water_temperature_2')}}"
target:
  entity_id: input_number.end_of_day_pool_water_temperature

Magic! Just used this for the same setup on my pool temp sensor

Going to try and find a way to get the 1hr average from the sensor 2hrs after the pump has been on