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
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 :
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()