Skip to content
View in the app

A better way to browse. Learn more.

Power Forum - Renewable Energy Discussion

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

ModBus Question

Featured Replies

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

ModBus.jpg

 

HA Entities.jpg

HA Entities.jpg

 

Day Hour.jpg

Day Hour.jpg

 

 

minute second.jpg

minute Second.jpg

 

year month.jpg

Year Month.jpg

Edited by Ivan Endicott

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.

  • 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 by Ivan Endicott

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.

  • 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.

  • 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 by Ivan_E

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 by p_i

  • 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 😳

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. 😉

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.

  • 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 by Ivan_E

@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 by slipx

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 by p_i

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);
            }

 

@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?

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

 

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 by slipx

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.

15 minutes ago, slipx said:

I'm referring to this. Its a setting within home assistant. 

image.png.a2513ad7ade9dd47b275e9831c4987de.png

Yeah, mine is set correctly. My automations would have issues if it wasn't. 

image.png.3699debfaea2689dd7790a10b2c75751.png

 

The ESPHome time component uses the timezone from the ESPHome server not Home Assistant.

image.png.54e4f34d8a52800bd559a4937d03da52.png

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.