Capture entity value at two events and create new entity

Hello,

First post here. I’m hoping someone can help nudge me over the hill of a problem I’ve been trying to tackle.

What I’m trying to achieve
I have a Tesla Wall Connector hooked into Home Assistant. This shows the overall total energy usage of the charger over it’s lifetime. I want to break this down some and give me stats such as “last charge” and “last day”.

How I’m approaching the problem so far
Right now I am starting off by tackling what should be the easier of the two, just recording how many watts were used in the last charge. My approach to this is such:

  1. When the charging contacts close, record the current Wh of the charger
  2. When the charging contacts open, record the difference from the current Wh and the previous initial Wh

To do this I setup two triggers based on the state of the contacts. It all made sense to me, but it isn’t working. The “initial” value does get recorded in my new entity, but not just when the charging starts. It keeps updating throughout the charge.

I feel there is probably a HA fundamental I am misunderstanding here and would appreciate any help or nudging in the proper direction.

This is what I am currently using, though as I said I’m still hung up in the first part and haven’t even tested the second yet

template:
  - trigger:
      - platform: state
        entity_id: binary_sensor.tesla_wall_connector_contactor_closed
        from: "off"
        to: "on"
  - sensor:
      - name: "Tesla Wall Connector Initial Wh"
        unique_id: "sensor.tesla_wall_connector_initial"
        state: "{{ states('sensor.tesla_wall_connector_energy') }}"
        unit_of_measurement: "Wh"

  - trigger:
      - platform: state
        entity_id: binary_sensor.tesla_wall_connector_contactor_closed
        from: "on"
        to: "off"
  - sensor:
      - name: "Tesla Wall Connector Last Charge Wh"
        unique_id: "sensor.tesla_wall_connector_last_charge"
        state: "{{ states('sensor.tesla_wall_connector_energy') - states('sensor.tesla_wall_connector_initial_wh') }}"
        unit_of_measurement: "Wh"

Attempting a different approach to capturing that initial energy level, I instead tried doing an automation to save it into an input_number but that doesn’t seem to work either. In fact the input_number doesn’t even seem to get set at all.

- id: "1656113275035"
  alias: Store charger Wh at start of charge cycle
  description: ""
  trigger:
    - platform: state
      entity_id:
        - binary_sensor.tesla_wall_connector_contactor_closed
      from: "off"
      to: "on"
  condition: []
  action:
    - service: input_number.set_value
      data:
        value: "{{ states('sensor.tesla_wall_connector_energy') }}"
      target:
        entity_id: input_number.tesla_wall_charger_precharge_wh
  mode: single

I got the automation working. The problem was I needed to increase the max value on the helper since the value I was trying to set was quite high, and the default was 100.

Still not sure why the template_sensor method doesn’t work like I expect but I am able to capture the initial value properly now with the automation and helper, so let’s see if I can manage the second half of this :slight_smile:

I managed to get the “Last Charge Session” working using Automations and Helpers. I would still love to understand why my Template Sensors approach doesn’t work though.

One thing I will note that I learned here, on the second half with the math is that apparently the sensors call returns a string even if the entity is a number? It wasn’t working until I tested it out in the template editor where I found I had to convert the results to float then it worked.

Here is what I ended up with (Automations so these are in the automations.yaml)

- id: "1656113275035"
  alias: Store charger Wh at start of charge cycle
  description: ""
  trigger:
    - platform: state
      entity_id:
        - binary_sensor.tesla_wall_connector_contactor_closed
      from: "off"
      to: "on"
  condition: []
  action:
    - service: input_number.set_value
      data:
        value: "{{ states('sensor.tesla_wall_connector_energy') }}"
      target:
        entity_id: input_number.tesla_wall_charger_precharge_wh
  mode: single

- id: "1656113845035"
  alias: Tesla Last Charge
  description: ""
  trigger:
    - platform: state
      entity_id:
        - binary_sensor.tesla_wall_connector_contactor_closed
      from: "on"
      to: "off"
  condition: []
  action:
    - service: input_number.set_value
      data:
        value: "{{ states('sensor.tesla_wall_connector_energy')|float(0) - states('input_number.tesla_wall_charger_precharge_wh')|float(0) }}"
      target:
        entity_id: input_number.tesla_last_charge
  mode: single

An easier way to do this might be by using the Utility Meter integration with a couple of tariffs.

Feed this with your sensor.tesla_wall_connector_energy sensor.

Use an automation triggered by your binary sensor binary_sensor.tesla_wall_connector_contactor_closed to reset the utility meter and switch tariffs (e.g. between charge and idle).

The charge tariff sensor will hold the last charge value until the next charge.

utility meter:
  - tesla_last_charge:
      source: sensor.tesla_wall_connector_energy
      cycle: yearly # we will reset it manually more often than this
      tariffs:
        - idle
        - charge
  - tesla: # no need for tariffs on this one, this is all you need to track daily use
      source: sensor.tesla_wall_connector_energy
      cycle: daily

automation:

trigger:
  - platform: state
    entity_id: binary_sensor.tesla_wall_connector_contactor_closed
    to:  # null "to" triggers on all state changes
action: 
  - if:
      - "{{ trigger.to_state.state == 'on' }}"
    then:
      - service: utility_meter.calibrate # reset the last charge sensor
        data:
          entity_id: sensor.tesla_last_charge_charge
          value: 0
      - service: select.select_option # change to recording the charge
        target:
          entity_id: select.tesla_last_charge
        data:
          option: "charge"
  - if:
      - "{{ trigger.to_state.state == 'off' }}"
    then:
      - service: select.select_option # change to recording idle energy
        target:
          entity_id: select.tesla_last_charge
        data:
          option: "idle"

This will create three sensors:

sensor.tesla_daily # daily energy
sensor.tesla_last_charge_charge # your last charge energy
sensor.tesla_last_charge_idle # energy use when not charging, reset yearly

That last one is a bi-product of this method and not really any use to you.

Huh that sounds a LOT easier than what I went through. Figures :slight_smile:
Though I learned a lot going through it. I’ll take a look at this as I think I still have some bugs in my daily tracking.