Template sensor gives weird values

I have a template sensor that is adding two numeric sensors together. The result is a sensor that drops values randomly, these drops do not appear in either of the individual sensors. Why does this happen?

The two individual sensors:

Result sensor with in same timeframe:

template:
{{ (states('sensor.modbus_inverter_batterya') | float(0) + states('sensor.modbus_inverter_500v_batterya') | float(0)) | round(1) }}

The result of this is that I cant use the sensor for a trigger in my automation, I want it to trigger when the value stays above 0 for 20minutes which hardly ever because of the drops

One or both of your devices is going into unavailable /unknown state and not reporting a numeric state and your float(0) is defaulting that sensors data to zero.

1 Like

Okay, so after further inspection I noticed that the individual values from the two sensors sometimes get the ā€œunavailableā€ state from the modbus integration. These states equals ā€œ0ā€ I guess, thus giving these valuesā€¦ Can I get around this problem without using more sensors to filter this out? Like making hassio not recording the unavailable state in the logbook at all?

A bandpass-filter comes to mind but wont work because a sudden drop in value is quite natural in this environment. Suggestions?

saw your post when I posted mine :slight_smile:
Can i make it not record anything instead or is there any easy fix?? Would be great if the ā€œlast available valueā€ could be used in these cases

Try something like thisā€¦ seems a similar problem

Also

1 Like

Thanks! Iā€™ll give that a try

Use an availability filter on your template so your template sensor is unavailable if either of the source sensors is.

1 Like

You donā€™t mention if you are using the new or legacy template sensor format and did not show your entire sensor, so this example is for the new format as it is what you should be using:

  state: "{{ (states('sensor.modbus_inverter_batterya') | float(0) + states('sensor.modbus_inverter_500v_batterya') | float(0)) | round(1) }}"
  availability: "{{ has_value('sensor.modbus_inverter_batterya') and has_value('sensor.modbus_inverter_500v_batterya') }}"

The legacy format would be:

  value_template: "{{ (states('sensor.modbus_inverter_batterya') | float(0) + states('sensor.modbus_inverter_500v_batterya') | float(0)) | round(1) }}"
  availability_template: "{{ has_value('sensor.modbus_inverter_batterya') and has_value('sensor.modbus_inverter_500v_batterya') }}"

This will result in gaps in your graph if either of the source sensors are unavailable rather than odd values where one or the other sensor is set to 0 by the defaults for the filters you have used.

1 Like

I think I run the legacy templates for most of my sensors unfortunately, too many of them to fix atm. But your fix worked flawlessly! No more dips in the graphs, thank you!

1 Like

No problem.

There is no need to update your existing sensors if they are working for you, but you should strongly consider the new format for any new sensors you create.

Yes I have already been forced to create templates of the new version due to some limitations in some cases. Iā€™ll let it be a matter of the future