Create a template sensor that only updates when another state is on

Trying to create a template sensor for pool temperature that only updates when the pump is on and holds the last temperature when pump is off. I have tried the following in my config.yaml but when the pump if off it says “unavailable” I have researched this and trying many things and many restarts I have not had any success

template:
  - sensor:
      - name: current_pool_temperature
        unique_id: id_pool_temperature
        state: "{{ states('sensor.water_temp_temperature') }}"
        availability: "{{ is_state('switch.shellyplus2pm_d48afc787034_switch_0', 'on') }}"
        unit_of_measurement: "°F"
        device_class: temperature
        state_class: measurement
        #delay_on:
          #minutes: 1

You’ve mixed up a few different things there…

  • delay_on is only for binary_sensor
  • The template you have used for availability would not allow it to “hold. the last temperature when pump is off”, it would set the state to “unavailable”.
template:
  - sensor:
      - name: current_pool_temperature
        unique_id: id_pool_temperature
        state: |
          {% set pump_on = is_state('switch.shellyplus2pm_d48afc787034_switch_0', 'on') %}
          {% if this is not defined and not pump_on %}
            - 100
          {% elif pump_on %}
            {{ states('sensor.water_temp_temperature') }}
          {% else %}
            {{ this.state | default(0,1) }}
          {% endif %}
        unit_of_measurement: "°F"
        device_class: temperature
        state_class: measurement

The “-100” in the first clause is a placeholder for a default value of your choosing, it can be any number.

1 Like

Thanks I will give that a try. Oh yea that was a different thing from awhile back I was trying to get the sensor to not update until the pump was on for 1 min but I omitted it cause it didnt work

You could switch to a trigger-based template sensor instead and get that functionality… the templating is a little different and will require a second sensor to insure that delay, but it’s do-able:

template:
  - binary_sensor:
      - name: Pump On Delayed
        state: "{{ is_state('switch.shellyplus2pm_d48afc787034_switch_0', 'on') }}"
        delay_on: "00:01:00"
        availability: |
          {{ has_value('switch.shellyplus2pm_d48afc787034_switch_0') }}
  - trigger:
      - trigger: state
        entity_id: sensor.water_temp_temperature
        to: ~   
      - id: pump
        trigger: state
        entity_id:  binary_sensor.pump_on_delayed
        to: 
          - 'on'
          - 'off' 
    condition:
      - or:
          - condition: trigger
            id: pump
          - condition: state
            entity_id: binary_sensor.pump_on_delayed
            state: 'on'
    sensor:
      - name: current_pool_temperature
        unique_id: id_pool_temperature
        state: "{{ states('sensor.water_temp_temperature') | default(-100, 1) }}"
        unit_of_measurement: "°F"
        device_class: temperature
        state_class: measurement
1 Like
- trigger:
    - trigger: state
      entity_id: sensor.water_temp_temperature
      to: ~  
  condition:
    - condition: state
      entity_id:  switch.shellyplus2pm_d48afc787034_switch_0
      state: "on"
      for: 60

Gives you the same effect without having to create a new entity.

2 Likes

Unless the temperature is really stable… :upside_down_face:

Works great thanks!