Change State of Temperature Sensor (avoid unknown)

I have a pool temperature sensor (pool_temp) lthat will only read the temperature if the pool pump is running. When the pump is not running, the temperature reflects unknown.

I want to create a custom temperature sensor (current_temperature) to avoid the reading of unknown

I created current_temperature sensor. The idea of the flow is to trigger on a change in pool_temp and pass that value to current_temperature unless the state of pool_temp = unknown.

The question is, how do I pass the value of pool_temp to current_temperature in node red?

Where is this coming from? (is it direct into Node Red , is it a HA sensor?)

HA Sensor created in sensors.yaml

should by default be in msg.payload.

But what node passes the message through.

If I use a change node, what do I put in in order to tell the node to change the state of current_temperature.

There is no call service node (that I see) that allows you to change the state of a sensor.

It’s because you (generally) can’t change the state of a sensor

And why I asked where it came from.

What you’re trying to do is usually done in HA using something called a template sensor

(you’d create a second sensor based on the first sensor with the values you don’t want filtered out) and you want to do this as a template sensor in HA because it will stay available even if Node Red is down.

Never tried it before, but just tested this to change the state of a physical sensor and it worked.

I thought about that, so I created the following template sensor, but then realized the actual sensor does not have a ‘last_known_temperature’ entity that I saw:

- platform: template
    sensors:
      current_pool_temperature:
        value_template: >
          {% if is_state('sensor.pool_temp', 'unknown') %}
            {{ state_attr('sensor.pool_temp', 'last_known_temperature') }}
          {% else %}
            {{ state('sensor.pool_temp') }}
          {% endif %}

So, as an alternative, I thought about pushing the temperature from sensor.pool_temp to a new sensor that would always accept the temperature from sensor.pool_temp UNLESS the state = unknown

Exactly. You’re basically going do that same thing in the template sensor. Base it on a trigger (when your source data changes) and evaluate if you need to update the template data… If not, drop the update.

Think of it as I’m going to update myself only if I have a valid state from that…

So I am almost there:

Here is the template

- platform: template
    sensors:
      current_pool_temperature:
        value_template: >
          {% if is_state('sensor.pool_temp', 'unknown') %}
            {{ states('sensor.current_pool_temperature') }}
          {% else %}
            {{ states('sensor.pool_temp') }}
          {% endif %}
        unit_of_measurement: "°F"
        availability_template: "{{ is_state('sensor.pool_temp', 'unknown') }}"

When I run it through developer and register a temperature from sensor.pool_temp it reports accurate in developer.

However when I actually add the template, sensor.current_pool_temperature reports unknown when sensor.pool_temp is unknown and reports ‘unavailable’ when sensor.pool temp is NOT unknown.

Here is the automation:

alias: Update Current Pool Temp
description: “”

trigger:
  - platform: state
    entity_id:
      - sensor.pool_temp
condition: []
action:
  - service: homeassistant.update_entity
    data: {}
    target:
      entity_id: sensor.current_pool_temperature
mode: single

Ok… Just walking myself through this. I made progress, but the template does not work as intended.

Since the sensor.current_pool_temperature always read ‘unavailable’, I updated the state to ‘99’ in the developer tab.

From what I can tell, when the sensor.pool_temp reads ‘unknown’, the sensor.current_temperature changes to ‘99’.

So for example:

If sensor.pool_temp is 90, then sensor.current_pool_temperature is also 90, however when sensor.pool_temp changes to unknown sensor.current_pool_temperature changes to 99 (which is what I manually entered), but I intend the sensor to read 90 (which was the sensor.pool_temp reading)

Any thoughts!?!?!

You have to create a sensor entity node in NR, then msg.payload sent to that node appears as the sensor value.