Sensor settings and incorrect data when restarting the router

Hi, I’m a novice user, but Home Assistant is great. I bought a Shelly 3EM to measure the energy consumed. Everything works great until I restart the router.

Shelly 3EM is connected via the ShellyForHass plugin in version 0.2.0. Everything runs on Raspberry Pi 4 and Home Assistant in version 0.118.3. In configuration.yaml I set a formula for the “platform: template”, which adds up the absolute consumption of the individual electrical phases.

Looks like this:

sensor:
  - platform: template
    sensors:
      energy_consumption_total:
        friendly_name: Energy Consumption Total
        icon_template: mdi:flash
        unit_of_measurement: kWh
        value_template: "{{ states('sensor.shelly_shem_3_40f52001972c_1_total_consumption')|float + states('sensor.shelly_shem_3_40f52001972c_2_total_consumption')|float + states('sensor.shelly_shem_3_40f52001972c_3_total_consumption')|float}}"

Everything works fine until I restart or turn off the router to which my devices are connected for more than a few seconds. Then anomalies appear in the graph based on “energy_consumption_total”, which also affects all other variables that are derived from them.

Shelly 3EM

The strange thing is that the sensors that the formula works with ("'sensor.shelly_shem_3_40f52001972c_1_total_consumption., Etc.") did not detect any anomalies.

Shelly 3EM phase1

Where do you think the problem is? Shelly EM3 sensors appear to be OK according to the graph. So is the problem in the formula that adds them, or directly in the Home Assistant kernel?

We’ve been to many places on the internet, but haven’t found an answer. Can anyone think of finding and fixing the cause of the error?

Most likely the state of those is going to ‘unknown’ on reboot.

states(‘blah’) | float will return 0.0 on all strings that cannot be converted. (If you wanted a different default, you would do {{states('blah') | float(10.0)}} )

Thus, you’ll have, for a brief moment:

"{{ 0.0 + 0.0 + 0.0}}"

Try setting the availability template. You can check if any one sensor reports unknown, or it might be better to check if all 3 are unknown if one happens to drop out.

availability_template: "{{ states('sensor.shelly_shem_3_40f52001972c_2_total_consumption') != 'unknown'}}

I’m not sure if that will work or not. My guess is it wouldn’t plot any points that are marked as unavailable…but it’s only a guess.

Thanks for the directions. However, this cannot be a problem directly in the Home Assitant core. I think that if the sensors are not available, then there should be a hole in the graph correctly, not a value of 0, which then distorts the whole measurement.

Not for a template sensor. {{states('sensor.that.doesnt.exist') |float }} = 0.0

The template sensor still exists…because you never told it when it shouldn’t exist. Without specifying availabiliy_template, the sensor is always marked available, thus will output the value_template whenever ANY of those sensors change state.

And, it doesn’t matter what those sensors change state to. This will update, and calculate 0.0 if those sensors all report a non number value.

And do you think this should solve the situation?

!= 'unknown'}}

Yeah, there is a chance that updating the availability_template to reflect when things are bad could help. Or just add it to the value template itself.

This will add the total power only if all 3 sensors are in a good state. If not, it will return the last good state. Only bad side is if any one of these sensors turn off, this value won’t update, even with the other 2 still reporting.

value_template: >
  {% set p1 = states('sensor.shelly_shem_3_40f52001972c_1_total_consumption')|float('bad') %}
  {% set p2 = states('sensor.shelly_shem_3_40f52001972c_2_total_consumption')|float('bad') %}
  {% set p3 = states('sensor.shelly_shem_3_40f52001972c_3_total_consumption')|float('bad') %}
  {% if p1 != 'bad' and p2 != 'bad' and p3 != 'bad' %}
    {{ p1 + p2 + p3 }}
  {% else %}
    {{ states('sensor.energy_consumption_total') }}
  {% endif %}
2 Likes

This could probably work, but shouldn’t the system monitor these states and availability itself, so that it’s correct and users don’t have to hack it?

It does. Except for template sensors.

I mean, some template sensors want to know when other sensors are available or not. If it’s something you care about, you need to do it yourself.

You could also just do states.sensor.shelly_shem_3_40f52001972c_1_total_consumption.state which will just throw an error when the sensor is unavailable. I’m not sure what happens to the template sensor in this case. My guess is the error would cause the evaluation to halt and it would just remain at its previous value.

The thing that’s messing you up is using the states('XXX') function which returns a string even if the thing isn’t there, rather than throw an exception.

Here he solves exactly the same problem as mine. Issue with Utility Meter after rebooting

How can I debug the sensors and have the values returned to me over time?

I found several other threads that solve the same problem