I’m trying to make my first template binary sensor working. I’m trying to detect if my electrical motorbike is charging or not. For this I’ve a zigbee plug who can monitor the power consumption. My yaml code works well when it’s charging but it goes unavailable instead of going off. What am I doing wrong ?
A Numeric State Trigger is designed to trigger only when the entity’s value crosses the threshold value. Therefore in order to trigger the following Numeric State Trigger, the consumed power must decrease from a value above 40 to below 40.
Is that what you observe when the motorbike stops charging? That power consumption drops from above 40 to below 40?
In addition, the following template is odd because it can report up to three states but there are only two possible values for trigger.id. Under what circumstances do you foresee that the value of trigger.id could be something other than on or off?
But it has the same result, that’s why I’ve tried a more detailled code. The sensor provide the watts consumed so yes, it goes below 40 when the charge is finished. This is below what the sensor provide on a charge cycle.
The graph shows that after it finishes charging, it takes almost 30 minutes until the power decreases below 200. That means the Template Sensor won’t report off until approximately 30 minutes after it has stopped charging.
Your ‘detailed code’ will never report the string ‘Unknown’ because the value of trigger.id can’t be anything other than off or on.
In addition, there’s no need to make the template explicitly report boolean values False and True. The template simply needs to evaluate to a boolean value.
The sensor show that the charger is still using power (watt) so the charge is not over. It just mean that for the end of charging the charger does use it full power. But the end of the charge is not when the values just decrease.
It was just to test if my ‘if’ test above was right. If the unknown state appear, that means I dit not write the code well, because like you said this should never happen. If the sensor was working right this part would be deleted.