Looking to do some scripting, any help?

Post Reply
Steve1978
Posts: 9
Joined: Tue Feb 07, 2023 9:00 pm

Hi..

ok my goal:
I am on Octopus agile. I would like to create a script that runs hourly and picks out the 3 cheapest 30 minute intervals in the next 24 hour period. then force charge the batterys at these times.

I have run a script which i will share, this will get you the rates and pick out the 3 cheapest:

Code: Select all

$apiUrl = "https://api.octopus.energy/v1/products/AGILE-FLEX-22-11-25/electricity-tariffs/E-1R-AGILE-FLEX-22-11-25-D/standard-unit-rates/"
$apiKey = "sk_live_**********************"  # Replace with your actual API key

# Setup headers with basic authentication
$headers = @{
    Authorization = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$($apiKey):"))
}

# Make the API request
$response = Invoke-RestMethod -Uri $apiUrl -Method Get -Headers $headers

# Sort the prices and display the cheapest slots
$prices = $response.results | Sort-Object unit_rate
$cheapestSlots = $prices | Select-Object -First 4
$cheapestSlots
And the response is this

Code: Select all

value_exc_vat  : 17.2
value_inc_vat  : 18.06
valid_from     : 2024-09-15T21:30:00Z
valid_to       : 2024-09-15T22:00:00Z
payment_method : 

value_exc_vat  : 7.92
value_inc_vat  : 8.316
valid_from     : 2024-09-14T09:30:00Z
valid_to       : 2024-09-14T10:00:00Z
payment_method : 

value_exc_vat  : 2.2
value_inc_vat  : 2.31
valid_from     : 2024-09-14T10:00:00Z
valid_to       : 2024-09-14T10:30:00Z
payment_method : 

value_exc_vat  : 0.88
value_inc_vat  : 0.924
valid_from     : 2024-09-14T10:30:00Z
valid_to       : 2024-09-14T11:00:00Z
payment_method : 
You can just run this in powershell.

Next I am trying to communicate with my Modbus, and this is where i am stuck. I know a lot of people use HACS for doing stuff but I don't believe HA can do what i am trying to do. So I have my modbus in it is an EW11 wifi one and when using HACS I can read and see data, i can set charge times etc.

But what i am trying to do is access the info in command line or via a script. closest I am getting is with this script below, it should bring me back the Firmware version but i am getting errors. I think it might be my NModbus.dll but i am not sure.

Code: Select all

Add-Type -Path "C:\Script\NModbus\bin\Debug\netstandard2.0\NModbus.dll"

# Setup Modbus TCP connection
$ipAddress = "192.168.8.113"  # Elfin EW11 IP address
$port = 502  # Default Modbus TCP port

# Create TCP connection
$tcpClient = New-Object System.Net.Sockets.TcpClient($ipAddress, $port)

# Use ModbusIpMaster for TCP connections
$modbusMaster = [NModbus.Device.ModbusIpMaster]::CreateIp($tcpClient)

$modbusSlaveId = 1  # Unit ID of your inverter

# Reading Model (String data across 16 registers)
$registerAddressModel = 30000
$modelRegisters = $modbusMaster.ReadHoldingRegisters($modbusSlaveId, $registerAddressModel, 16)

# Convert registers to string (each register contains 2 characters)
$modelString = [System.Text.Encoding]::ASCII.GetString([BitConverter]::GetBytes([Convert]::ToInt32($modelRegisters, 16)))
Write-Output "Model: $modelString"

# Reading Firmware Master (U16)
$registerAddressFirmware = 30016
$firmwareRegister = $modbusMaster.ReadHoldingRegisters($modbusSlaveId, $registerAddressFirmware, 1)
Write-Output "Firmware Master Version: $($firmwareRegister[0])"

# Close connection
$tcpClient.Close()
Dave Foster
Posts: 1297
Joined: Thu Oct 13, 2022 7:21 pm

To be honest, what you are trying to do would be perfect for home assistant - there are already pre-written integrations for the modbus which would provide you with real time data and local control options.

There are octopus integrations, but you can also get the api data using rest sensors.

You could then create automations that do things depending on the state of the batteries, the agile prices and the latest solar forecast etc..

Anyway back to your script - what error are you getting ?

i’ve never used scripting myself but often use modpoll.exe https://www.modbusdriver.com/modpoll.html to read or write registers - probably easier to test with this and once you have confirmed the Elfin / inverter are replying ok you can then move on to your script.
Steve1978
Posts: 9
Joined: Tue Feb 07, 2023 9:00 pm

Thanks for your help.

I am now looking for a full list of the Inverter Registers. any idea where i can get that? i thought i saw it on this forum somewere but for the life of me i cant find it.

Here are some commands but i am struggling to translate what the output says, this should be telling me if the battery charge is enabled or not

Code: Select all

C:\win>modpoll.exe -m tcp -p 502 -a 247 -t 3 -0 -1 -c 3 -r 41001 192.168.8.113
modpoll 3.13 - FieldTalk(tm) Modbus(R) Master Simulator
Copyright (c) 2002-2024 proconX Pty Ltd
Visit https://www.modbusdriver.com for Modbus libraries and tools.

Protocol configuration: MODBUS/TCP, FC4
Slave configuration...: address = 247, start reference = 41001 (PDU), count = 3
Communication.........: 192.168.8.113, port 502, t/o 1.00 s, poll rate 1000 ms
Data type.............: 16-bit register, input register table

-- Polling slave...
[41001]: 0
[41002]: 768
[41003]: 1024
when i enable the charge for 0300 to 0400 i get this

Code: Select all

C:\win>modpoll.exe -m tcp -p 502 -a 247 -t 3 -0 -1 -c 3 -r 41001 192.168.8.113
modpoll 3.13 - FieldTalk(tm) Modbus(R) Master Simulator
Copyright (c) 2002-2024 proconX Pty Ltd
Visit https://www.modbusdriver.com for Modbus libraries and tools.

Protocol configuration: MODBUS/TCP, FC4
Slave configuration...: address = 247, start reference = 41001 (PDU), count = 3
Communication.........: 192.168.8.113, port 502, t/o 1.00 s, poll rate 1000 ms
Data type.............: 16-bit register, input register table

-- Polling slave...
[41001]: 1
[41002]: 768
[41003]: 1024
Dave Foster
Posts: 1297
Joined: Thu Oct 13, 2022 7:21 pm

Those are the time registers, they have to be read and written as a block of 6 registers together - i.e. you cannot set time period 1 without setting time period 2 (even if you leave it unchanged).

41001 is the grid charge enabled 1 = set (charge from grid), 0 = not set (don't charge from grid)
41002 is start time hour is div 256, minute is remainder i.e. 768 = 03:00
41003 is end time hour is div 256, minute is remainder i.e. 1024 = 04:00 (1025 would be 04:01)

etc..

This is probably the best starting point for register addresses, but be aware that Fox change, move and sometimes don't provide all registers on different inverters. https://github.com/nathanmarlor/foxess_ ... XESS-1.pdf
Post Reply