Supporting spot energy pricing

If you can spot the error in the code you’re doing better than me. Sure your channels/items are linked correctly?

I’ll have a closer look this evening.

This is spot on, the constants are mixed up.

1 Like

Thanks Danny! @mstormi build updated. I’ll checkin once you confirm it’s working better (and also remove some of the logging).

Charging works now, Discharging does not, binding always sends -1 no matter the dischargeRate

2026-03-04 09:27:26.418 [DEBUG] [org.openhab.binding.sonnen.internal.SonnenHandler                ] - Executing batteryDischargingToGrid command
2026-03-04 09:27:26.419 [INFO ] [hab.binding.sonnen.internal.communication.SonnenJSONCommunication] - startStopBatteryDischarging: urlStr2 = http://192.168.170.45/api/v2/setpoint/discharge/-1
2026-03-04 09:27:26.423 [DEBUG] [hab.binding.sonnen.internal.communication.SonnenJSONCommunication] - ChargingOperationMode = {"EM_OperatingMode":"1"}
2026-03-04 09:27:26.423 [DEBUG] [org.openhab.binding.sonnen.internal.SonnenHandler                ] - Update channel batteryDischargingToGrid with state ON
2

Another typo, try again :crossed_fingers:

Looks fine to me now. Will do some more testing.

One more thing: for now, we always need to specify the rate and send the [dis]charge command.
I wonder if you can change the binding to omit the latter when [dis]charging is already going on and we only want to adapt the rate, it would need to send another POST .../[dis]charge/xxx (but not set EM_OperationMode)

Yeah I’d already been thinking about this. It will take some refactoring but it’s worth doing. I also think that setting charge/discharge to 0 should turn the mode back to Auto - thoughts?

Leave it with me - might be a weekend job

No, please. 0 is a ‘valid’ target value.
When you have a large consumer like a heat pump, you will want it to NOT run off battery.
In standard self consumption mode that would happen. So forcefully [dis]charging with 0 is the way to go in case your heat pump is active. Or your EV.

1 Like

WIP - but can you confirm nothing broke in https://smedley.id.au/tmp/org.openhab.binding.sonnen-5.2.0-SNAPSHOT.jar I refactored some code to make things simpler.

Edit 2: Changing the value of the (dis)charge rate should now update the battery if it’s already in manual mode (asusming I got the logic right)

I’m afraid I think you broke it:
I’ve forced discharging with 999 W so inside 1-10000 but got this:

2026-03-05 11:34:02.485 [DEBUG] [org.openhab.binding.sonnen.internal.SonnenHandler                ] - Executing batteryChargingFromGrid command
2026-03-05 11:34:02.515 [DEBUG] [hab.binding.sonnen.internal.communication.SonnenJSONCommunication] - Error processing charge request: Max battery charge power needs to be in the range of 0 - 10000.

Sorry I need to correct myself, it’s currently working fine. Maybe because of two bindings running at the same time, happens at times.
To change rate did not work though (only when I also issue the send cmd for the DischargingToGrid switch)

Excellent (that it’s still working). I may have to add some logging back in to understand why it only worked when you refreshed the switch.

Added some logging and fixed a typo, can you try again?

Charging keeps working, changing the rate still does not.

2026-03-06 09:13:31.334 [INFO ] [org.openhab.binding.sonnen.internal.SonnenHandler                ] - chargeRate changed, if battery in manual mode, update charging rate
2026-03-06 09:13:31.334 [INFO ] [hab.binding.sonnen.internal.communication.SonnenJSONCommunication] - putData = null
2026-03-06 09:13:31.334 [INFO ] [hab.binding.sonnen.internal.communication.SonnenJSONCommunication] - emgetOperationMode() = null
2026-03-06 09:13:34.424 [DEBUG] [hab.binding.sonnen.internal.communication.SonnenJSONCommunication] - BatteryData = {"Apparent_output":999,"BackupBuffer":"0","BatteryCharging":true,"BatteryDischarging":false,"Consumption_Avg":440,"Consumption_W":429,"Fac":49.992,"FlowConsumptionBattery":false,"FlowConsumptionGrid":false,"FlowConsumptionProduction":true,"FlowGridBattery":false,"FlowProductionBattery":true,"FlowProductionGrid":true,"GridFeedIn_W":1669.0,"IsSystemInstalled":1,"OperatingMode":"2","Pac_total_W":-999,"Production_W":3098,"RSOC":55,"RemainingCapacity_Wh":11557,"Sac1":999,"Sac2":null,"Sac3":null,"SystemStatus":"OnGrid","Timestamp":"2026-03-06 09:13:34","USOC":51,"Uac":228.0,"Ubat":213.0,"dischargeNotAllowed":false,"generator_autostart":false}
2026-03-06 09:13:34.424 [DEBUG] [org.openhab.binding.sonnen.internal.SonnenHandler                ] - Update channel batteryChargingState with state ON
2026-03-06 09:13:34.425 [DEBUG] [org.openhab.binding.sonnen.internal.SonnenHandler                ] - Update channel batteryDischargingState with state OFF
2026-03-06 09:13:34.425 [DEBUG] [org.openhab.binding.sonnen.internal.SonnenHandler                ] - Update channel batteryCharging with state 999 W

Also, the error I encountered earlier happened again. But the charging cmd worked.
I think it’s happening when as part of my running HEMS it resets the bat mode to default self consumption. So it might be cosmetic, but still worth fixing.

(I think in the last-but-one line below you should add to debug output which command it’s executing that results in the error in the last line)

6-03-06 09:04:38.126 [DEBUG] [org.openhab.binding.sonnen.internal.SonnenHandler                ] - Executing batteryChargingFromGrid command
2026-03-06 09:04:38.127 [INFO ] [hab.binding.sonnen.internal.communication.SonnenJSONCommunication] - putData = EM_OperatingMode=2
2026-03-06 09:04:38.633 [INFO ] [hab.binding.sonnen.internal.communication.SonnenJSONCommunication] - emgetOperationMode() = 2
2026-03-06 09:04:38.633 [DEBUG] [org.openhab.binding.sonnen.internal.SonnenHandler                ] - Update channel batteryChargingFromGrid with state OFF
2026-03-06 09:04:38.636 [DEBUG] [org.openhab.binding.sonnen.internal.SonnenHandler                ] - Executing batteryDischargingToGrid command
2026-03-06 09:04:38.636 [DEBUG] [hab.binding.sonnen.internal.communication.SonnenJSONCommunication] - Error processing discharge request: Max battery discharge power needs to be in the range of 0 - 10000.
2

Hi Stormi,

So when the rate is changed, the battery is reporting the operating mode is 2 - ie automatic.

I’ve tweaked the second last log line, and am now also reporting the requested rate in that last log line.

usuall url :slight_smile:

oh, okay … seems my HEMS intervenes and sends PVForceCharge OFF right after I send the initial Charge(999 W) and EM_OperationMode goes to Automatic and no longer accepts rate changes
Now that I stopped it from doing that, changing rate works.

Looks fine to me now. Will let you know if I spot anything.