I have a question about a template sensor I’ve just set up.
I’ve just added a Sonoff Pow R2 to my washer/dryer, with the eventual intention of inferring the current status of the cycle by the current/time relationship, but that’s a topic for another day.
Right now, I’m looking to set up a template sensor to give me the maximum current seen (across any and all cycles. This is just to keep an eye on the maximum that the Sonoff will see).
I’m aware of the statistics sensor, but I don’t really need all the other data. Especially if I expend the sample size to cover as long a time frame as possible to give me a “true” maximum, rather than say just a daily maximum.
I re-purposed another value template I found that looks to compare two given values and output the greater of the two. I’m giving the comparator the “current current” (sensor.washdry_current) and itself, the “max current” (sensor.washdry_current_max):
washdry_current_max:
friendly_name: 'WashDry Current Max'
unit_of_measurement: "A"
value_template: >
{% set currentcurrent1 = (states.sensor.washdry_current.state) %}
{% set maxcurrent2 = (states.sensor.washdry_current_max.state) %}
{% macro max(X, Y) -%} {{X|float if X|float > Y|float else Y|float }}{%- endmacro %}
{{ max(currentcurrent1, maxcurrent2) }}
Things seemed to be working fine to start with; the max value quickly increased as it saw a greater value. However, it then had a point where it reduced, then later it set itself to zero.
I have a feeling its either because I’m referencing the output within the comparison itself, or perhaps something goes a bit wrong if there’s an “unknown” output from the Sonoff at any one time. It’s the first time I’m using this type of value template, so I could do with a hand.
Looks like it’s working as it should during normal operation. Thanks very much. Is there any way to retain the value after a system restart? It looks like it currently resets itself every time I reboot
The “sensor.washdry_current” is an MQTT reading coming from a Sonoff Pow R2, not sure if the first reading after a restart is immediate or not.
The other one “sensor.washdry_current_max” is the template sensor itself, the running total if you will. But if its not retaining values then this is probably where the “unknown” comes from.
Either way, I haven’t seen the value at the exact point of a restart (i.e. it could have had a single MQTT reading through by the time I’ve looked), but the template sensor is then showing “0.0” by the time I check it. I think once the restart has occurred, the choice is between “0.0” from the sonoff (washing machine is off) and “unknown” (template sensor has reset itself to an unknown value).
Is there a way to retain the previous value outright, or perhaps something could be made in Node Red to almost create a “backup” max value that is then queried on a restart?
I agree. Template Sensors are always evaluated on startup and, at that moment, have no existing value (or previous value, depending on one’s perspective). It’s kind of like asking it to compute its value using its current value … which hasn’t been computed yet.