My go at a Solar PV rule to charge the car when Solar is generated

Hi All

The goal is to only turn the Tesla charger on, when the Solar PV array is outputting more than 6kW.

I want the trigger to be when the solar PV changes (which is every 10minutes) or just run manually every 5minutes.

Once its been turned on, due to generation being above 6, run a timer for 30minutes.

Thinking about this a little more , what if the car is flat and they want it charged irrespective of the power status - because they need to use the car! I suspect that if they manually charge it, the system will actually go and turn it off.

If in that 30minutes the solar drops below 6, then we want to cancel the timer and turn the charger off.

Does this seem correct?

var Timer Solar_Timer = null
val Integer Solar_TimeOut = 30


rule "Charge Tesla Model S, when Solar Production is above 6kWh"
when
    Item SE_Live_Production changed or
    Time cron "0 0/5 * * * ?""
then
    if (SE_Live_Production.state > 6 | kW && TeslaCharge.state == "OFF")
        if (Solar_Timer !== null) {
           TeslaCharge.sendCommand("ON")
           Solar.reschedule(now.plusMinutes(Solar_TimeOut))
 } else {
           TeslaCharge.sendCommand("ON")
                       Solar_Timer = createTimer(now.plusMinutes(Solar_TimeOut))
                        [ |
                                if (SE_Live_Production.state > 6 | kW) {
                                        Solar_Timer.reschedule(now.plusMinutes(Solar_TimeOut))
                                } else {
                                        TeslaCharge.sendCommand("OFF")
                                        Solar_Timer = null
                                }
                        ]

                }
end

Thanks!

I regard cron triggered rules as a bit of bodge in most cases, and look for ways to have it all events-driven. That’s not always possible, but worth exploring.

We should take care here … what if PV doesn’t change, power maxed out or something?
Maybe the Item is still updated (to same). Would that be a better choice of rule trigger?

Perhaps it doesn’t update at all sometimes - at night say - and you should indeed keep the cron.

Let’s guess you have defined somewhere else variables Solar_Timer and Solar_Timeout. I think just Solar here is a typo though.

Yes. Is this a problem to be tackled? Now’s the time. Might have to come up with a way to detect “manually” and modify your automation. (It’s useful to remember manual = not-auto, which can be easier to work out.) Or perhaps a way to detect “flat” and modify.

Sort of related … maybe you’ve just left it out for clarity at this stage, but should you check the car is plugged in or something before turning charger on? If that’s detectable, it is also perhaps useful as a trigger.

Make a manual switch to override the rule.

Why would he need that? If the car is not pluged in, there is no harm done in turning on the power to the charger… It doesnt use the power anyway :slight_smile:

Because it’s going to turn it off again in a half hour. That might be thirty seconds after you really have plugged it in.

I’m sure the charger will be idiot proof, but we don’t have to behave like idiots to find out. Skimping on the logic leaves room for weird behaviour to creep in.

A better option would be, as you also suggest, to trigger if there is a car plug´d in at all. If not, then dont switch on the charger.

Hi Rossko57, yes it was a typo.

Yes, I will add a check to see if the charge port is open before charging. That’s all the binding can do I believe.

Kim, yeah I’m thinking a switch which ‘ARMS’ the rule is the go. Another way would be to use the Weather binding to check the weather, only run this rule if the weather for the day is not rainy or cloudy or something along those lines.

Does the rule seems logic correct?

var Timer Solar_Timer = null
val Integer Solar_TimeOut = 30


rule "Charge Tesla Model S, when Solar Production is above 6kWh"
when
    Item SE_Live_Production changed or
    Time cron "0 0/5 * * * ?""
then
    if (SE_Live_Production.state > 6 | kW && TeslaCharge.state == "OFF")
        if (Solar_Timer !== null) {
           TeslaCharge.sendCommand("ON")
           Solar_Timer.reschedule(now.plusMinutes(Solar_TimeOut))
 } else {
           TeslaCharge.sendCommand("ON")
                       Solar_Timer = createTimer(now.plusMinutes(Solar_TimeOut))
                        [ |
                                if (SE_Live_Production.state > 6 | kW) {
                                        Solar_Timer.reschedule(now.plusMinutes(Solar_TimeOut))
                                } else {
                                        TeslaCharge.sendCommand("OFF")
                                        Solar_Timer = null
                                }
                        ]

                }
end

Apart from the quotemark typo …

Do you have access to a state of charge measurement? It’s not so difficult to modify behaviour to deal with flats, say not starting the off-timer until charge > 15%, and/or giving charge even if available solar below threshold.

Did you review rule trigger conditions?

1 Like

I know the battery condition in the car, yes so I can modify to check the battery. That’s a fairly straight forward one.

‘review the rule trigger conditions’ - i think time (cron) and the output to the grid are good checks. What did you have in mind?

I said my piece already about the difference between change and received update, and if cron was needed at all.

OK, updated to:

rule "Charge Tesla Model S, when Solar Production is above 6kWh"
when
Item SE_Live_Export received update or

Seems its not correct, doesnt like the checks with respect to 6kw I suspect

10:44:14.702 [WARN ] [del.core.internal.ModelRepositoryImpl] - Configuration model 'tesla.rules' has errors, therefore ignoring it: [8,30]: missing 'then' at '"\nthen\n    if (SE_Live_Export.state > 6 | kW && TeslaCharge.state == "'
[21,68]: String literal is not properly closed

10:44:15.729 [WARN ] [del.core.internal.ModelRepositoryImpl] - Configuration model 'tesla.rules' has errors, therefore ignoring it: [8,30]: missing 'then' at '"\nthen\n    if (SE_Live_Export.state > 6 | kW && TeslaCharge.state == "'
[21,68]: String literal is not properly closed

Updated and its syntax is OK now

var Timer Solar_Timer = null
val Integer Solar_TimeOut = 30


rule "Charge Tesla Model S, when Solar Production is above 6kWh"
when
    Item SE_Live_Export received update or
    Time cron "0 0/5 * * * ?"
then
    var Number solar_power = SE_Live_Export.state as DecimalType
    if (solar_power > 6.0 && TeslaCharge.state == OFF){
        if (Solar_Timer !== null) {
           TeslaCharge.sendCommand("ON")
           Solar_Timer.reschedule(now.plusMinutes(Solar_TimeOut))
 } else {
           TeslaCharge.sendCommand("ON")
                       Solar_Timer = createTimer(now.plusMinutes(Solar_TimeOut))
                        [ |
                                if (SE_Live_Export.state > 6 | kW) {
                                        Solar_Timer.reschedule(now.plusMinutes(Solar_TimeOut))
                                } else {
                                        TeslaCharge.sendCommand("ON")
                                        Solar_Timer = null
                                }
                        ]

                }
}
end

If you still have your cron trigger the previous post in funny red colour gives you a clue

quotemarks typo.

Using VSCode with openHAB extension as your editor will pick up most syntax errors.

2 Likes