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.
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.
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.
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.
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.
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 %}
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.