Issue with rule to automate solar charging of electric car

  • Platform information:
    • Hardware: Raspberry Pi Model B Rev 2
    • OS: Raspbian GNU/Linux 10 (buster)
    • Java Runtime Environment: Zulu 8.46.0.225
    • openHAB version: 2.5.5
  • Issue of the topic: Issue creating rule
  • Please post configurations (if applicable):
    • Tesla Binding, Kostal Inverter Binding

Hello everyone,

I have the following situation I’m trying to automate. I have solar power on my roof which is connected to a Kostal Plenticore Plus 10 without a battery. Everything in the house draws power from that solar array, and I have an electric car (Tesla model S) with an old version of the wall charger (non-WiFi enabled version) to charge the car with.

The rule I’m trying to set up is a relatively straightforward one. I need a minimum of 4000W to start the car charging. If the car is connected to the charger, I want it to automatically start charging if I have more than 4kW, and to stop the charging process again once I go below 4kW.

I have set up the car as a thing and the inverter. I have various channels set up:

  • DCPower on the inverter which returns Number:Energy
  • Charge Cable on the car which is a string
  • Charge Enable Request which is a switch

I’m trying to create a rule that looks at the DCPower, but from what I can gather, I can’t take the number returned there as a starting point for the when section in my rule? I’m having a hard time trying to wrap my head around how I would create the logic in this rule, since what I’m trying to do seems relatively straightforward, but unfortunately I wasn’t able to find an example of something similar.

Any input or tips on what I could read to understand this better, or a pointer towards how to create the logic for this would be very much appreciated. :slight_smile:

Not sure exactly what you mean, but maybe “when power > 4000 run rule”?
That’s right, you can’t do that.

I think you’d want to trigger from any of your key controls changing, and sort out states in the body of the rule

when
   power changed or
   cable connected changed or
   charge request changed
then
   if (charger on AND power < 4000) then
       turn charger off
   else if ( cable connected AND charge requested AND power >4000)
       turn charger on, if it is not already on
etc. etc.

In real life you’d probably add some hysteresis and/or timing sophistication to stopit rattling on and off.

Not sure exactly what you mean, but maybe “when power > 4000 run rule”?
That’s right, you can’t do that.

That was what I meant. Power will constantly change as the inverter is polled by the binding every 30 seconds. I read that rule triggers could be an item update, and I was thinking that the new value for power would be a status update.

That gives me the question of what I should then use? I’ve noted down the return values of the channels I have which are not really a status change. I might be able to leverage the Astro binding, but I’m having a hard time wrapping my head around the rule that would make it start on for example astro:sun:home:rise and stop on astro:sun:home:set. Or is my understanding of the rules concept just wrong and I would need to constantly run an evaluation and I can’t really define the stopping and starting point (speaking of this example, start the rule at sunrise and stop it at sunset) for that rule?

appreciate the input by the way! :slight_smile:

Excellent. Use it as a rule trigger, as I outlined.
It takes some getting used to writing event-driven rules, but do not fear having a rule run every 30 secs .
You write your rule so that at each pass, it considers “Should I start charging? Stop charging? do nothing?” etc.

You don’t really care about Astro sun here, the meaningful part is your power production, use that.

1 Like

Ok, that makes sense. Thank you! :slight_smile:

Just posting here to track this for the future in case anyone else runs into a similar issue.

I can see in the log that I get an item state change event for my inverter, for example:
[vent.ItemStateChangedEvent] - kostalinverter_PLENTICOREPLUS100WITHOUTBATTERY_1b94e226_deviceLocalDCPower changed from 7805.8374023438 W to 7838.3364257812 W
where deviceLocalDCPower is the channel on my Kostal Plenticore thing.

I have created the following rule:

    when
        Item kostal_solar_power changed
    then
        logInfo("tesla_charge.rules","Tesla solar logging started")
        var Number solar_power = kostal_solar_power.state as DecimalType
        logInfo("tesla_charge.rules","The value of kostal is:{}", kostal_solar_power.state)
        logInfo("tesla_charge.rules","The value of solar_power is:{}", solar_power)
        if (solar_power > 4000)
        {
            "tesla:models:ebfe9e73:5YJSA7E22HF198215:chargeenablerequest".sendCommand(ON)
        }
        else if (solar_power <= 4000)
        {
            "tesla:models:ebfe9e73:5YJSA7E22HF198215:chargeenablerequest".sendCommand(OFF)
        }
    end

kostal_solar_power is the item that is linked to the deviceLocalDCPower channel. I just need to figure out how to convert the state which contains 7453.2109375 W to an integer number that I can use for the rest for the rule. The conversion to a DecimalType doesn’t seem to work because of the power unit that is attached.

Alternatively, leave it alone and change what you compare it to. (This will take care of unit conversions too)

if (kostal_solar_power.state > 4 | kW)

That did the trick. I was able to convert manually using:
var Number solar_power = (kostal_solar_power.state as DecimalType).intValue

But your solution is a lot more elegant and I hadn’t even seen that yet. Great info! The very rudimentary version is now:

when
    Item kostal_solar_power changed
then
    if (kostal_solar_power.state > 4 | kW)
    {
        test_start_charge.sendCommand(ON)
    }
    else if (kostal_solar_power.state <= 4 | kW)
    {
        test_start_charge.sendCommand(OFF)
    }
end

Where test_start_charge is an item that is linked to chargeenablerequest on the Tesla Model S thing.

I’ll expand this with some other checks and timings as you suggested, but its great to see that this works. Thanks again so much for your help! :slight_smile:

Interesting solution. How do you get around the instantaneous solar power dropping below 4kw when clouds come out for only minutes , when the production drops but then goes up again?

That constant on/off scenario?