Woud like to create a Template-Sensor with condition

Hi, i woud like to crate a template sensor with condition.

This is my sensor:

ladeenergie_extern:
device_class: energy
friendly_name: “Ladeenergie_Extern_kWh”
unit_of_measurement: “kWh”
value_template: “{{ (states(‘sensor.ladeenergie_gesamt_kwh’) | float(0) - states(‘sensor.wattpilot_ladeenergie_gesamt_kwh’) | float(0)) | round(3) }}”

I want, that the sensor only write data, when the chargingcable ist:

condition:
- binary_sensor.charging_cable_connected
from: “on”
to: “off”

this is necessary because the sensor.wattpilot updates every second and the charging energy_total sensor only when the cable is unplugged, so i always get negative values.

Please help. Thanks

Firstly, please format your pasted config correctly for the forum in future.

Secondly, you should be using the new template sensor format for new sensors.

Thirdly, that is not a valid condition. State conditions do not have a from and to, only a state.

So assuming you only want the template sensor to update when the binary sensor is off:

configuration.yaml

template:
  - sensor:
      - name: "Ladeenergie Extern kWh"
        device_class: energy
        unit_of_measurement: "kWh"
        state: >
          {% if is_state('binary_sensor.charging_cable_connected', 'off') %}
            {{ (states('sensor.ladeenergie_gesamt_kwh') | float(0) - states('sensor.wattpilot_ladeenergie_gesamt_kwh') | float(0)) | round(3) }}
          {% else %}
            {{ this.state }}
          {% endif %}
        availability: "{{ states('sensor.ladeenergie_gesamt_kwh') | is_number and states('sensor.wattpilot_ladeenergie_gesamt_kwh') | is _number }}"

This will update whenever the binary sensor is off and either of the source sensors change. It will keep the last state when the binary sensor turns on.

If you only want it to update when the binary sensor changes from on to off, then you have to use a triggered template sensor. Like this:

template:
  - trigger:
      platform: state
      entity_id: binary_sensor.charging_cable_connected
      from: "on"
      to: "off"
    sensor:
      - name: "Ladeenergie Extern kWh"
        device_class: energy
        unit_of_measurement: "kWh"
        state: "{{ (states('sensor.ladeenergie_gesamt_kwh') | float(0) - states('sensor.wattpilot_ladeenergie_gesamt_kwh') | float(0)) | round(3) }}"
        availability: "{{ states('sensor.ladeenergie_gesamt_kwh') | is_number and states('sensor.wattpilot_ladeenergie_gesamt_kwh') | is _number }}"

thanks for helping.
i become this error in the logs.
TemplateSyntaxError: expected token ‘end of print statement’, got ‘_number’

Show your correctly formatted config.

template:

  • trigger:
    platform: state
    entity_id: binary_sensor.tmbjc7ny1nf053278_charging_cable_connected
    from: “on”
    to: “off”
    sensor:
    • name: “Ladeenergie Extern kWh Cable off”
      device_class: energy
      unit_of_measurement: “kWh”
      state: “{{ (states(‘sensor.skoda_ladeenergie_gesamt_kwh_2’) | float(0) - states(‘sensor.wattpilot_ladeenergie_gesamt_kwh’) | float(0)) | round(3) }}”
      availability: “{{ states(‘sensor.skoda_ladeenergie_gesamt_kwh_2’) | is_number and states(‘sensor.wattpilot_ladeenergie_gesamt_kwh’) | is _number }}”
type or paste code here

sorry, how can i poste the code?

Try again.

By reading that link.

template:
  - trigger:
      platform: state
      entity_id: binary_sensor.tmbjc7ny1nf053278_charging_cable_connected
      from: "on"
      to: "off"
    sensor:
      - name: "Ladeenergie Extern kWh Cable off"
        device_class: energy
        unit_of_measurement: "kWh"
        state: "{{ (states('sensor.skoda_ladeenergie_gesamt_kwh_2') | float(0) - states('sensor.wattpilot_ladeenergie_gesamt_kwh') | float(0)) | round(3) }}"
        availability: "{{ states('sensor.skoda_ladeenergie_gesamt_kwh_2') | is_number and states('sensor.wattpilot_ladeenergie_gesamt_kwh') | is _number }}"

You have a space in is_number in the last line.

It is a good idea to re-type suggested code and try to understand it as you go, as opposed to blindly copying and pasting. The error was in @tom_l’s original code above, but that’s an easy mistake to make on the forum as we are suggesting code without actually putting it on our systems.

If you’d re-typed it in you would have noticed the error.

1 Like

Thank you so much :wink:
Now it works for me.

Can i add this code to sensors.yaml.
If i write this in the sensors.yaml i become an error.

It’s not under sensor:, it’s under template:. This is a “modern configuration” template sensor:

Take a look at the top of your configuration.yaml. You’ll probably see something like:

sensor: !include sensors.yaml

You don’t want that, though.

Now see if you have a top-level template: — it might be lower down the file or in a line like:

template: !include templates.yaml

If you have an !include line like that, put the code in the referenced file but without the first template: line.

If you don’t have a line like that or any other template: section, you can either add one to use a separate file; or paste the entire code with the first template: line into configuration.yaml.

Thanks for helping, i am a beginner :wink:

Only for information:

I have more template sensors in sensor.yaml like this:

  - platform: template
    sensors:
    
      wattpilot_ladeenergie_gesamt:
        device_class: energy
        friendly_name: "Ladeenergie_Gesamt"
        unit_of_measurement: "kWh"
        value_template: "{{ ((states ('sensor.wattpilot_totally_charged') | float *0.001)) | round(3) }}"

when i test (more possibilities) the code in sensors.yaml - no function - error.

Those are legacy format sensors, that sit under the sensor heading.

The code I gave is modern format and sits under the template heading. Read what I wrote again, and see the link.