Template binary sensor become unavailable

Hi everyone,

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 ?

This is my code :

template:
    # Chargeur moto 
  - trigger:
      - id: "on"
        platform: numeric_state
        above: 200
        entity_id: sensor.prise_chargeur_moto_electricalmeasurement
      - id: "off"
        platform: numeric_state
        below: 40
        entity_id: sensor.prise_chargeur_moto_electricalmeasurement
    binary_sensor:
      - name: "Chargement moto"
        unique_id: chargement_moto
        state: >
            {% if trigger.id == 'off' %} False
            {% elif  trigger.id == 'on' %} True
            {% else %} Unknown
            {% endif %}
        icon: >-
            {% if trigger.id == 'on' %} mdi:motorbike-electric
            {% else %} mdi:motorbike-off
            {% endif %}
        attributes:
            last_ended: >
                {% if trigger.id == 'off' %}  {{ now() }}
                {% else %} {{ this.attributes.last_ended }}
                {% endif %}
            last_started: >
                {% if trigger.id == 'on' %}  {{ now() }}
                {% else %} {{ this.attributes.last_started }}
                {% endif %}
            duration: >
                {% if trigger.id == 'off' %} {{ (as_timestamp(this.attributes.last_ended) - as_timestamp(this.attributes.last_started)  / 60) | round()}} Minutes
                {% elif  trigger.id == 'on' %} {{ this.attributes.duration }}
                {% else %} Unkown
                {% endif %}

Protip: just paste that code in Developer tools → Templates, and see what happens to the rendered output when you charge or disconnect your moto.

1 Like

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.

      - id: "off"
        platform: numeric_state
        below: 40
        entity_id: sensor.prise_chargeur_moto_electricalmeasurement

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?

            {% if trigger.id == 'off' %} False
            {% elif  trigger.id == 'on' %} True
            {% else %} Unknown
            {% endif %}

It can be reduced to this:

            {{ trigger.id == 'on' }}

Thank you for your help

My first try was with this code

{{ trigger.id == 'on' }}

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.

This is what I would do. 2 sensors, one telling you if it’s charging, the other with the duration it’s been charging (or had charged)

template:
    # Chargeur moto 
  - trigger:
      - id: "on"
        above: 200
        <<: &charger_moto
          platform: numeric_state
          entity_id: sensor.prise_chargeur_moto_electricalmeasurement
          variables:
            triggered_at: "{{ now() }}"
            result: "{{ trigger.id == 'on' }}"
            icon: "mdi:motorbike-{{ 'electric' if result else 'off' }}"
            start: "{{ triggered_at if result else None }}"
            end: "{{ triggered_at if not result else None }}"
      - id: "off"
        below: 40
        <<: *charger_moto
    binary_sensor:
      - name: "Chargement Moto"
        unique_id: chargement_moto
        state: "{{ result }}"
        icon: "{{ icon }}"
        attributes:
          start: >
            {{ start or this.attributes.start | default(None) }}
          end: >
            {{ end or this.attributes.end | default(None) }}
  - sensor:
      - name: "Chargement Moto Duration"
        unique_id: chargement_moto_duration
        device_class: duration
        unit_of_measurement: s
        state: >
          {% set start = state_attr('binary_sensor.chargement_moto', 'start') %}
          {% set end = state_attr('binary_sensor.chargement_moto', 'end') %}
          {% if start and end %}
             {% if start > end %}
               {{ (now() - start).total_seconds() }}
             {% else %}
               {{ (end - start).total_seconds() }}
             {% endif %}
          {% elif start and not end %}
            {{ (now() - start).total_seconds() }}
          {% else %}
            0
          {% endif %}

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.

Thanks for your help and time. I will try your code.

I’ve never seen this kind of code in yaml config, can you explain what it does or point some documentation at me ?

        <<: &charger_moto
        <<: *charger_moto

The Template Sensor is behaving that way because that’s how you designed it to behave with your choice of Numeric State Triggers.

Based on the charging curve’s values, I would try this:

  - trigger:
      - id: "on"
        platform: numeric_state
        entity_id: sensor.prise_chargeur_moto_electricalmeasurement
        above: 825
      - id: "off"
        platform: numeric_state
        entity_id: sensor.prise_chargeur_moto_electricalmeasurement
        below: 800

Thanks @petro for your code. It works the last charge cycle.

And for those who wants more detail on this :

You could check Anchor in yaml