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