March 4, 20242 yr In the UK but this forum as been a great help. Currently running a SunSynk 5.5kW inverter and with the outage of SunSynk in the UK over the weekend it messed up the Time Sync on the inverter. So I looked into it and believe there is a way to sync the time with an external source via an ESP32 / RS485 to TTL board. I have found out ModBus registers 022 - 024 see to be labelled as System Time. (ModBus.jpg) I exposed these via the ESP32 board but (HA Entities.jpg) But looking at the numbers in eahch screenshot they make no sense, (Day Hour.jpg \ minute second.jpg and year month.jpg) Anyone know how to convert them to a human readable format and also I assume as they are R/W these can be adjusted on the fly? Maybe someone has some experience with these? Thanks ModBus.jpg HA Entities.jpg Day Hour.jpg minute second.jpg year month.jpg Edited March 5, 20242 yr by Ivan Endicott
March 5, 20242 yr I've been looking at those too since the outage this past weekend. Those registers are 16bit (2 bytes) values where the high 8bit (byte) is the first value and the low 8bit (byte) is the second value. In simplified code this means that: month = register22%256; year = (register22-month)/256+2000; hour = register23%256; day = (register23-hour)/256; second = register24%256; minute = (register24-second)/256; % = modulus function. I'm not sure how you're going to do this in HA as I'm using node-red to do these calculations. I still need to test my time updating code but I need to do this close to midnight so I don't mess up my daily totals if I get it wrong.
March 5, 20242 yr Author I too am using Node-Red as HA automatiion is too limited. Let me know how you get on please as this will been good to implement. Edited March 5, 20242 yr by Ivan Endicott
March 5, 20242 yr If using HA how are you not able to read and write to or from your Sunsynk? There is someone here on the forum that has done quite a bit of work setting up their system using an esp32 and rs485 module. There are some interesting threads already covering reading and writing via modbus. Check out.
March 5, 20242 yr Author Hi Shaun, that is the idea to use HA with the help of ESP Home but unfortunately the registers I am looking at are a bit more complex and haven't been covered before as far as I know. I need to work out how to interupt the numbers I get from the register before I can write them back. This is all new to me so it's not exactly second nature.
March 5, 20242 yr Author 12 hours ago, p_i said: I've been looking at those too since the outage this past weekend. Those registers are 16bit (2 bytes) values where the high 8bit (byte) is the first value and the low 8bit (byte) is the second value. In simplified code this means that: month = register22%256; year = (register22-month)/256+2000; hour = register23%256; day = (register23-hour)/256; second = register24%256; minute = (register24-second)/256; % = modulus function. I'm not sure how you're going to do this in HA as I'm using node-red to do these calculations. I still need to test my time updating code but I need to do this close to midnight so I don't mess up my daily totals if I get it wrong. @p_i So I am trying to get my head around working out the numbers into a readable form, So for example how does 6147 work out to be March 2024? I can see your notes but how does this break down as I am learning as I go? month = register22%256; year = (register22-month)/256+2000; Edited March 5, 20242 yr by Ivan_E
March 5, 20242 yr 12 minutes ago, Ivan_E said: @p_iSo I am trying to get my head around working out the numbers into a readable form, So for example how does 6147 work out to be March 2024? month = 6147 % 256 = 3 year = (6147-month)/256 + 2000 = 6144/256 + 2000 = 24 + 2000 = 2024 Edit: I've hopefully attached my date & time fetching node-red flow. I have 2 inverters in parallel, hence the 2 nodes in my flow. Just nuke one of them if you only have 1 inverter. I'm using mbusd on a remote node hence the censored IP address, but it should be simple to edit the inverter configuration to whatever you're using, eg. rs485 usb dongles. flows.json Edited March 5, 20242 yr by p_i
March 5, 20242 yr Author 5 minutes ago, p_i said: month = 6147 % 256 = 3 year = (6147-month)/256 + 2000 = 6144/256 + 2000 = 24 + 2000 = 2024 @p_i I get the year but I am struggling with the 6147 % 256 = 3 sorry 😳
March 5, 20242 yr Just now, Ivan_E said: @p_i I get the year but I am struggling with the 6147 % 256 = 3 sorry 😳 x % y = the remainder when you divide x by y. In the example above, 6147 = 24*256 + 3. Just trust me, node-red knows what to do with that. 😉
March 5, 20242 yr I bought a nodemcu and lost it. Somewhere there's an STM32 as well. Never got the chance to use either. I believe it still is easier to program and configure the esp32. I'm not trying to convince you to change to one. I have seen many posts where people have struggled to get the node red to work well with inverters and HA. I'm interested to see what you come up with.
March 5, 20242 yr Author @p_i Hi Can I confirm you formula is correct please? second = register24%256 minute = (register24-second)/256 I seem to be calculating the minutes using this register24%256 for example Int(5165/256) = 20 However I don't seem to be able to calculate the seocnds correctly. Edited March 5, 20242 yr by Ivan_E
March 5, 20242 yr @Ivan_E I've updated my ESPHome config to include time synchronization with HA. You can download from the GitHub repo or add the code below (remember to use your own modbus id). For now its a simple interval based update. You can set the interval to whatever suits you. #Set the interval to sync your inverter time with HA or comment out to disable interval: - interval: 3600s then: - lambda: |- esphome::modbus_controller::ModbusController *controller = id(inverter); time_t now = ::time(nullptr); struct tm *time_info = ::localtime(&now); int seconds = time_info->tm_sec; int minutes = time_info->tm_min; int hour = time_info->tm_hour; int day = time_info->tm_mday; int month = time_info->tm_mon + 1; int year = time_info->tm_year; year = year + 1900; int year1 = year - 2000; uint16_t reg22_value = (year1 << 8) | month; uint16_t reg23_value = (day << 8) | hour; uint16_t reg24_value = (minutes << 8) | seconds; if (year != 1970) { std::vector<uint16_t> rtc_data = {reg22_value, reg23_value, reg24_value}; esphome::modbus_controller::ModbusCommandItem set_rtc_command = esphome::modbus_controller::ModbusCommandItem::create_write_multiple_command(controller, 22, rtc_data.size(), rtc_data); controller->queue_command(set_rtc_command); ESP_LOGI("Time Sync", "Seconds: %d, Minutes: %d, Hour: %d, Day: %d, Month: %d, Year: %d", seconds, minutes, hour, day, month, year); } Edited March 6, 20242 yr by slipx
March 5, 20242 yr 14 hours ago, Ivan_E said: I too am using Node-Red as HA automatiion is too limited. Let me know how you get on please as this will been good to implement. I tested my time setting code and even though the values I get via modbus from my 2 inverters differs by about 20 seconds, I went to check the screens and they're perfectly in sync. I think I need to debug a bit more when I'm awake in the morning. I'd hate to blow up my inverters now. 😊 Edited March 5, 20242 yr by p_i
March 5, 20242 yr You can also trigger a manual sync by creating an input_boolean sensor i.e input_boolean.sync_inverter_time in HA and then adding it as a binary sensor. Whenever the toggle is turned on the action will run binary_sensor: - platform: homeassistant entity_id: input_boolean.sync_inverter_time name: "Sync Time" id: sync_time on_press: then: - lambda: |- esphome::modbus_controller::ModbusController *controller = id(inverter); time_t now = ::time(nullptr); struct tm *time_info = ::localtime(&now); int seconds = time_info->tm_sec; int minutes = time_info->tm_min; int hour = time_info->tm_hour; int day = time_info->tm_mday; int month = time_info->tm_mon + 1; int year = time_info->tm_year; year = year + 1900; int year1 = year - 2000; uint16_t reg22_value = (year1 << 8) | month; uint16_t reg23_value = (day << 8) | hour; uint16_t reg24_value = (minutes << 8) | seconds; if (year != 1970) { std::vector<uint16_t> rtc_data = {reg22_value, reg23_value, reg24_value}; esphome::modbus_controller::ModbusCommandItem set_rtc_command = esphome::modbus_controller::ModbusCommandItem::create_write_multiple_command(controller, 22, rtc_data.size(), rtc_data); controller->queue_command(set_rtc_command); ESP_LOGI("Time Sync", "Seconds: %d, Minutes: %d, Hour: %d, Day: %d, Month: %d, Year: %d", seconds, minutes, hour, day, month, year); }
March 6, 20242 yr @slipx I'm using your ESPHome yaml for my Sunsynk 8Kw inverter. Thanks for these updates for syncing the time, it is much appreciated. This time sync appears to be working great with the exception that my inverter time is now exactly 2 hours behind local time. I guess it is syncing GMT instead of CAT, my HA server has the correct time zone set and the ESPHome logs on the ESP32 are show the correct time, not sure why GMT is being synced. Any ideas on how to get local time on the inverter?
March 6, 20242 yr 46 minutes ago, Gambit said: @slipx I'm using your ESPHome yaml for my Sunsynk 8Kw inverter. Thanks for these updates for syncing the time, it is much appreciated. This time sync appears to be working great with the exception that my inverter time is now exactly 2 hours behind local time. I guess it is syncing GMT instead of CAT, my HA server has the correct time zone set and the ESPHome logs on the ESP32 are show the correct time, not sure why GMT is being synced. Any ideas on how to get local time on the inverter? I found I can add the time zone to the time component, giving that a try. time: - platform: homeassistant id: homeassistant_time timezone: Africa/Johannesburg
March 6, 20242 yr That's interesting. What do your ESPHome logs show you. When it triggers a time sync it will output the Hour and minutes. Mine pulls the correct time from HA Have you set your time zone correctly from within HA settings? Edited March 6, 20242 yr by slipx
March 6, 20242 yr 1 hour ago, slipx said: That's interesting. What do your ESPHome logs show you. When it triggers a time sync it will output the Hour and minutes. Mine pulls the correct time from HA Have you set your time zone correctly from within HA settings? My Home Assistant server is set to the correct time zone, I did assume it would use that however according to the ESPHome Time Component documentation it will use the ESPHome server timezone. I guess it uses this at the firmware compile time to set the device timezone if it hasn't been specified in the YAML. My ESPHome is running in docker and I haven't modified the TZ environment variable so it is just defaulting to UTC. I guess the ESPHome log web interface gets the correct time offset from the browser when it displays the logs.
March 6, 20242 yr 15 minutes ago, slipx said: I'm referring to this. Its a setting within home assistant. Yeah, mine is set correctly. My automations would have issues if it wasn't. The ESPHome time component uses the timezone from the ESPHome server not Home Assistant.
March 6, 20242 yr If you are running ESPHome in a docker container have you tried explicitly setting the time zone. There are a few ways to do this
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.