Rule to turn OFF the charger relay the when the battery voltage reaches 395V

Hello OpenHab teachers,
I managed to monitor the voltage and temperature of a 400V battery
Can someone help me write two rules to be able to disconnect the auxiliary charger?

  1. Close the Charger Relay when the battery voltage is higher than 395V
  2. Close the Charger Relay when the battery temperature is higher than 40 degrees Celsius

my items:

Switch                     ChargerRelayJ20        "Charger Relay [%s]"
Number:ElectricPotential   Batt_Voltage           "Tesla Battery Voltage"       <battery>      {channel="mqtt:topic:ce2219a49a:6f071898da:Battery_Voltage", unit="V", stateDescription=""[pattern="%.1f V"]}
Number                     Cell_Temp_Max          "Cell Max Temp. [%d]"



Thank you

Create a rule

  • When Batt_Voltage changed
  • When Cell_Temp_Max changed

Pick your scripting - either Blockly, JS Scripting or JRuby (might need to install the corresponding automation addon)

Blockly Version

Here I created two variables to store intermediary results, just so the blocks don’t get stupidly long

JRuby Version - UI Version

ChargerRelayJ20.ensure << (Batt_Voltage.state > 395 | "V") || (Cell_Temp_Max.state > 40)

JRuby File based rule

rule "Activate Charger Relay" do
  changed Batt_Voltage, Cell_Temp_Max
  run do
    ChargerRelayJ20.ensure << (Batt_Voltage.state > 395 | "V") || (Cell_Temp_Max.state > 40)
  end
end

Or if you prefer to make it as succinct as possible (one line)

changed(Batt_Voltage, Cell_Temp_Max) { ChargerRelayJ20.ensure << (Batt_Voltage.state > 395 | "V") || (Cell_Temp_Max.state > 40) }

JavaScript version

Well, I’m not familiar with it, but many people are.

PS, I’d suggest making your Cell_Temp_Max item as Number:Temperature, but if it works as it is now, and you’re happy with it, maybe leave it be.

Hi, thanks for the help, but I couldn’t get it to work. I want to use these rules in case of emergency - as an additional safety system. I want to start the charger in other conditions.
Now it’s on and I want to turn it off if the battery voltage reaches 395.
I tested with a relay for the lights and reduced the maximum battery voltage threshold, but it doesn’t seem to work.

rule "Activate Charger Relay" do
  changed Batt_Voltage, Cell_Temp_Max
  run do
    LuminaJ20.ensure << (Batt_Voltage.state > 360 | "V") || (Cell_Temp_Max.state > 40)
  end
end

OK so the logic is not quite what I thought.

The code then:

    ChargerRelayJ20.ensure.off if (Batt_Voltage.state > 395 | "V") || (Cell_Temp_Max.state > 40)

This means if the condition is not met, it won’t turn on the relay, but if the condition is met, it will turn off the relay.

rule "TURN OFF charger relay" do
  changed Batt_Voltage
  run do
    LuminaJ20.ensure.off if (Batt_Voltage.state > 362 | "V")
  end
end

i tried like this to but is not working


I suspect the item’s state isn’t yet updated. Try this (with just one trigger item)

rule "TURN OFF charger relay" do
  changed Batt_Voltage
  run do |event|
    LuminaJ20.ensure.off if (event.state > 362 | "V")
  end
end

If that works, I’ll provide a rule that will work for two or more items to check

Not working :slight_smile:

I run the rule but the relay remaining ON

You can’t run the rule manually. The rule has to be triggered by a change in value of Batt_Voltage. So try changing Batt_Voltage state to 400 V, that should trigger the rule.

Assumption:

  • You’ve created this rule in a file located in <OPENHAB_CONF>/automation/ruby/ANY_FILENAME.rb

As you can see, my battery is charging now and has 364V
in the script I will set the relay to stop at 366 V
I think that’s the best way to test it, but it doesn’t work :slight_smile:

rule "TURN OFF charger relay" do
  changed Batt_Voltage
  run do |event|
    LuminaJ20.ensure.off if (event.state > 366 | "V")
  end
end

Ok, let’s see if it works

If I actuate the relay manually, it closes, but using the script it does not want to
Now the rule is active and i wait for 366V

You can manually change an item state. Unless you have other rules, it shouldn’t affect anything.

You can change an item state either using the Karaf console, or simply adding this line at the end of your rule and saving it again:

rule "TURN OFF charger relay" do
  changed Batt_Voltage
  run do |event|
    LuminaJ20.ensure.off if (event.state > 366 | "V")
  end
end

# The following lines are for testing only
sleep 1
Batt_Voltage.update 370 | "V"

Like this ?

Yes, assuming the current Batt_Voltage is NOT already 370 V, it will trigger a changed event

The battery has not reached 366V yet - it has 63KW and I charge with approximately 3KW.
Should I change like this?

rule "TURN OFF charger relay" do
  changed Batt_Voltage
  run do |event|
    LuminaJ20.ensure.off if (event.state > 366 | "V")
  end
end
sleep 1
Batt_Voltage.update 367 | "V"

You can change it like that to.

Changing the state of the Batt_Voltage item will not physically change anything in the real world outside openhab. So it won’t actually physically make your actual Battery to have that voltage.

You can set the Batt_Voltage state to 100000V and that won’t matter.
It just affects inside Openhab’s state and will trigger our rule

I set this as well, it’s not a problem, but I need to know how
Now the battery has reached 366V and the relay has not stopped

This is because the condition is > 366V. It has to be more than to trigger. == 366V doesn’t satisfy that condition

I understand - they need to reach 367V
I will wait for that

You don’t have to wait if you just added those two extra lines. I promise it won’t cause any harm.

Here’s a better rule. It should do what you want, plus log the event but only if the event actually caused the relay to be turned off. It won’t log anything if the relay was already off.

rule "Shutdown charger when threshold is reached" do
  changed Batt_Voltage, to: proc { |state| state > 377 | "V" }
  changed Cell_Temp_Max, to: proc { |state| state > 40 }
  run do |event|
    turned_off = ChargerRelayJ20.ensure.off
    logger.info "#{event.item} (#{event.state}) exceeded the threshold. Shutting down charger" if turned_off
  end
end

If you can have a threshold that’s inclusive, e.g. batt_voltage >= XX instead of batt_voltage > XX, then the to: condition above can use a simpler Ruby Range like this:

# This threshold is inclusive when using ruby range's lower bound
VOLTAGE_THRESHOLD = 377 | "V"

rule "Shutdown charger when threshold is reached" do
  changed Batt_Voltage, to: (VOLTAGE_THRESHOLD..)
  changed Cell_Temp_Max, to: (40..)
  run do |event|
    turned_off = ChargerRelayJ20.ensure.off
    logger.info "#{event.item} (#{event.state}) exceeded the threshold. Shutting down charger" if turned_off
  end
end
rule "Shutdown charger when threshold is reached" do
  changed Batt_Voltage, to: proc { |state| state > 377 | "V" }
  changed Cell_Temp_Max, to: proc { |state| state > 40 }
  run do |event|
    turned_off = ChargerRelayJ20.ensure.off
    logger.info "#{event.item.label} (#{event.state}) exceeded the threshold. Shutting down charger" if turned_off
  end
end

Will this close the relay if one or both conditions are met correctly?

It will turn off the switch item (is that what you mean by “Close”) if EITHER (or both) of the conditions were met.