Template sensor should only update when sensor has a value

I’ve searched all over on this forum and other forums, but I can’t get it to work properly, so I need your help :wink:

I use EnergyZero’s HACS integration and it works well. This retrieves gas prices and electricity prices from the EnergyZero API. This is used by a number of dynamic energy providers in the Netherlands.

Sometimes it can happen that the new gas price is not available in the morning at 6 o’clock. As a result, there are big gaps in my gas prices, so the energy dashboard doesn’t give the correct totals for my gas usage.

In addition, the API only passes on delivery costs, so I add the extra costs myself. For this I use a template sensor. See the code below:

- platform: template
  sensors:
    gasprice_incl:
      value_template: >
        {% set gas = states('sensor.energyzero_today_gas_current_hour_price') | float + 0.6754 %}
        {% if gas > 0 %}
        {{ gas | float }}
        {% endif %}
      entity_id: sensor.gasprice_incl
      unit_of_measurement: "EUR/m³"

You can already see here that I only want to update the value if the sensor value is greater than 0.

In short, only if the sensor reports a new value do I want to update the template sensor, otherwise I want to keep the current value.

Am I doing this correctly and am I slightly off? Or am I completely wrong and should I take a completely different approach?

Thanks in advance for your help!

1 Like

If you switch to the current format you can use a trigger-based template sensors and filter out the state changes that you don’t want to track.

template:
  - trigger:
      - platform: state
        entity_id: sensor.energyzero_today_gas_current_hour_price
        not_to:
          - unavailable
          - unknown
    sensor:
      - name: Gasprice Incl
        state: >
          {% set gas = trigger.to_state.state | float(0) + 0.6754 %}
          {% if gas > 0 %} {{ gas }}
          {% else %} {{ states('sensor.gasprice_incl') }}
          {% endif %}
        unique_id: gasprice_incl
        unit_of_measurement: "EUR/m³"

Okay, that looks promising. One more question then: I now have all my sensors (including the template sensors) in the sensors.yaml.
But I suspect that your piece of code doesn’t belong there, is that right?

Correct, the sensors.yaml file likely has a top level configuration variable “sensor”, for the new format the top level variable needs to be “template”. There are a few different ways to include files in your configuration. The basic method is just to have all the template entities in one file. Personally, I prefer using packages

So far it looks good, the code is in the right place and valid.
I just have to wait until early tomorrow, because then the sensor will be triggered.

I did some small tests via the template editor, but that doesn’t really test well, so I’ll wait and see in the morning.

If everything works I will update the post tomorrow :+1:

Unfortunately, it still doesn’t work flawlessly…
Firstly, I have the problem that it stays empty after a restart of HA, which is strange because the sensor then also gets an update.
Second, the template sensor is currently empty, while the real sensor does have a value and that shouldn’t happen if I’m right.

The code I’m using now is as follows:

template:
  - trigger:
      - platform: state
        entity_id: sensor.energyzero_today_gas_current_hour_price
        not_to: ["unavailable", "unknown"]
    sensor:
      - name: Gasprice Incl
        state: >
          {% set gas = trigger.to_state.state | float(0) + 0.6754 %}
          {% if gas > 0 %} {{ gas }}
          {% else %} {{ states('sensor.gasprice_incl') }}
          {% endif %}
        unique_id: sensor.gasprice_incl
        unit_of_measurement: "EUR/m³"

The real sensor has a big gap again, like every morning between +/- 06:00 and +/- 08:30. But the template sensor remains Unknown.

Could it be a case sensitive problem? I don’t know which values ​​are exactly ‘unknown’ or ‘unavailable’.

Anyone have an idea?