Help with a cool sensor calculation?

I need some help with a flashy bit of yaml to calculate my Tesla power usage.

For background, the car is set to charge between midnight and 6am when it is a quarter of the price.

For further background, I have a sensor measuring whole house power (clamp_power), and another sensor that is a group sum of all my power monitors I call ‘calculated_power’. In normal operation, the calculated is always about 10-30 below the clamped power, likely due to accuracy etc.

I already use these to create a sensor called ‘kitchen_bench’ where the high powered devices are connected when used that don’t have a monitor on them (kitchen_bench = clamp_power - calculated_power.

I would like to use a similar thing to monitor the Tesla power (tesla_power), rather than a plug in monitor as the load would be near 2.4kw constantly for 6 hours sometimes.

The yaml needs nested If’s like so :

If time is between 0000-0600
And
clamp_power - calculated_power is > 200
Then
tesla_power = clamp_power - calculated_power

I guess I could have some seperate yaml to make a ‘tesla_window’ on first, then have one that does the calculation if the window is on, but I was wondering if there is a flashy yaml that can do it all in one sensor?

Thanks in advance.

I know There is a ‘charging’ from the Tesla integration but I have seen it can be a bit flaky so I don’t want to rely on that.

Yes - Zero - or to be more exact, it uses 1 watt on standby… no maybe 1. But I am not too worried about the 1 - as it seems to use that regardless, 24/7 - maybe for the lights on it that spell ‘TESLA’.

By Flashy - I mean flashy to me, as I have asked for stuff in the past and the forum gods have provided the yaml code I then use.

Im my mind, I am pretty sure I can copy bits of my config to set two sensors, that I can then use to do this calculation i.e

Senor called ‘Charging_Period’ - which would be On/Off
Sensor called ‘Power_Difference’ - which would be a subtraction as above.
And Then I think I have the skills to set the Tesla_Power sensor with if’s on the above.
I have the code I can copy to make all that happen, but I dont know how to do it all in one sensor.

Further to this, I already have a thing in there that calculates peak and offpeak - so I can use the offpeak as the first test… sensor.current_electricity_tariff is set to offpeak when it is 00000 - 0600.

My first go at AI - and its very successful. I asked it this -

can you write the jinja2 code for this : if ‘sensor.current_electricity_tariff’ equals ‘offpeak’, and if ‘sensor.total_power’ - ‘sensor.consuming_power’ is less than 250, then set the value of a sensor called ‘Tesla Calculated Power’ to the value of ‘sensor.total_power’ - ‘sensor.consuming_power’, but if these conditions are not met, then set the value to 1

And it came back with this -

{% if states(‘sensor.current_electricity_tariff’) == ‘offpeak’ and
(states(‘sensor.total_power’) | float - states(‘sensor.consuming_power’) | float) < 250 %}
{{ states(‘sensor.total_power’) | float - states(‘sensor.consuming_power’) | float }}
{% else %}
1
{% endif %}

So I suspect my sensor in my configuration.yaml will be this -

  - name : "Tesla Calculated Power"
    unique_id: "Tesla Calculated Power"
    unit_of_measurement: 'W'
    device_class: power
    state_class: measurement
    state: >
      {% if states('sensor.current_electricity_tariff') == 'offpeak' and 
  (states('sensor.total_power') | float - states('sensor.consuming_power') | float) > 250 %}
      {{ states('sensor.total_power') | float - states('sensor.consuming_power') | float }}
      {% else %}
        1
      {% endif %}

got the code in the config, and formatted so it does not throw an error -

  - name : "Tesla Calculated Power"
    unique_id: "Tesla Calculated Power"
    unit_of_measurement: 'W'
    state: >
      {% if states('sensor.current_electricity_tariff') == 'offpeak' and (states('sensor.total_power') | float - states('sensor.consuming_power') | float) > 250 %}
        {{ states('sensor.total_power') | float - states('sensor.consuming_power') | float }}
      {% else %}
        1
      {% endif %}
    device_class: power
    state_class: measurement

Fingers crossed for overnight charging - see what that brings. I added and energy sensor - Left Riemann sum - to match also in helper

Closing this off now as it works as intended…