Solar automation with Victron and Sonoff eWeLink/Cube and Matter

This might take a while, depending on your level of knowledge, but this is a successful way to setup once and configure as many times as needed.

I have a Victron (for the most part) solar system setup, that I wanted to automate to have a simple “plug in and and simply configure later” by adding Sonoff devices and other eWeLink compatible devices.

This will allow you to setup initial data transmission from your Victron system and later configure as many devices as you would like and perform automations from eWeLink directly.

I will not focus on how to configure devices, as they all have standard config, same you will find on all Sonoff/eWeLink/Victron documentation or internet tutorials. Only mandatory step is to have STATIC IP ADDRESS for all your devices (GX, iHost and NSPanel Pro)

Requirements:

  • Victron GX device with Node-RED (can be Cerbo on RaspberryPI) - might also work the same way with other devices, logic should be the same
  • iHost with Node-Red
  • (optional) NSPanel Pro (to act a matter hub for later automation with Virtual Devices)

Step 1 - Basic iHost Configuration

Upgrade to latest version, have static IP on device or setup dhcp binding on your router;

In Docker install Node RED and eWELink Smart Home (ignore homebridge in below screenshot, not part of this);

Configure your eWeLink with your account so that your devices are visible in eWeLink Cube;

In node-red, make sure to have these nodes installed (you can have others, these are needed for this setup) - node red, aedes, ewelink cube and victron:

NOTE: you can probably use any/other MQTT brokers, but this is what I use

Configure Aedes MQTT broker ON IHOST

image

Step 2 - Configure Victron Node Red and establish communication

Open Node-RED on GX device and if not available, install Victron node and Aedes MQTT (might work without Aedes to have mqtt-out function)

Select which data you would like to export to iHost -

I chose total power produced and battery SoC (note i am using a Fronius inverter instead of MPPTs, but it should work the same) and created a flow to expose them to mqtt

You can expose basically anything you want here, it’s quite easy and the process is repeatable - just add more devices and connect them to mqtt. Only thing i struggled here is to have a unified way (one mqtt out), but it didn’t work out great for me so I just went with this. Also keep in mind to configure TOPIC as you will need to receive this on the other end - here named “pw”

Step 3 - Configure iHost to take advantage of data

Initial setup - have event-state and get-devices from eWeLink Cube node

image

This will bring all your devices from eWelink to node-red to be later accessed and referenced to.

Next, setup “virtual devices” from node-red to ewelink cube (note - they will only be visible in ihost/ewelink cube, not in ewelink app! this will be done later with nspanel pro)

**Here is the challenging part! (**I have not found a better way to do this, if someone else has one, please let me know and I will update this)

Ewelink was not designed for this so does not know what solar production is and how to use it as trigger. You CAN use it directly from node-red with control-device function, but it will NOT allow you to manually change state from ewelink app later, so you are stuck with what ever you configure in node-red.

I am using temperature (as it will allow any scale/number) to represent power production (1w = 1 degree) and humidity for state of charge (0-100%), this in turn allows me to further create automations (scenes) directly from ewelink app!

Also, I have 0 coding skills, so I am jerry-rigging as much as I can without understanding much of the code. Although i read the instructions, they were not clear to me, for sure there are better way of doing this (probably JS scripting directly) but this is aimed to be truly NO-CODE setup apart form this initial configuration.

Even after reading documentation on the ewelink cube API, it’s still not clear what exactly is the relation between node-red integration of payload messages between function, put-device and register device.

Register device - basically creates a virtual device in ewelink cube, based on what you setup here, it will expect certain payload message (this is the biggest gray area for me), so I use as Temperature and humidity sensor (as this will later allow you create scene trigger from) and for Capabilities i use temperature for power and humidity for state of charge (i was not able to combine them, so I use 2 separate devices)

Device should appear in ewelink cube, but without data, you will need to add that timestamp trigger and deploy and also search on how to get token.

Next, we need to send data -

Here is the whole flow:

image

First, we are using MQTT in to receive the data from Victron, then I use a delay function to send messages 1/minute so I don’t overwhelm the devices/network (this is optional and you can add later).

Then a function is used to transform the message from MQTT from number into string of data that also says it is “temperature” so later functions know what the message represents :

Code to copy for stringify function:

================================

const jsonObject = {

    temperature: msg.payload

};

msg.payload = JSON.stringify(jsonObject);

return msg;

================================

Next step to transform the string into a JSON, which is the accepted format in which the data should be presented to a put-device function -

This functions does a few other thins also - it checks if the incoming message is a string, it round the numbers - not so important for temperature/power as they are all integers, but if you do with state of charge it will give a lot of decimal places)

And code to copy:

=========================================

let value;

// Parse input

if (typeof msg.payload === “string”) {

try {

    let data = JSON.parse(msg.payload);

    value = data.temperature ?? data;

} catch {

    value = Number(msg.payload);

}

} else if (typeof msg.payload === “object”) {

value = msg.payload.temperature;

} else {

value = Number(msg.payload);

}

// Ensure valid number

value = Math.round(Number(value));

// Correct iHost format

msg.payload = {

temperature: {

    temperature: value

}

};

return msg;

====================================

Some of the code comes from GPT, some from this forum.. this is what I found working. For sure there is a MUCH simpler way to do it…

End result:

For SoC - sate of charge to humidity, you should change the following:

  • when exposing data from GX node-red, use another mqtt out and set topic to “soc” for example, do the same on iHost node-red for MQTT it (both should have same topic name, does not matter what name is as long as they are identical)

  • in stringify and parsing function change in the code humidity instead of temperature

  • in register device, select device setup with Humidity and in Put-device state select Humidity in capabilities

  • Basically same as initial flow, however when you see temperature, use humidity.

Last step is to add your NSPanel to iHost via matter bridge. For this you must have you iHost, NS Panel and phone connected to same network. Have iHost connected to ewelink account (you should have already done this in the first steps), have NS Panel pro connected to your ewelink account, then just follow this tutorial - https://www.youtube.com/watch?v=CwgnMTcWVaE

YOU ARE DONE!

Now you can create Scenes directly from eWeLink app based on Power production and/or state of charge.

Here is a simple example where I tell my boiler to turn on when Solar power is enough and batteries are charged over 50%

From here on, no more coding required. Just simple logic built in Sonoff and eWeLink.

3 Likes

PS - thanks to the amazing team at ewelink. I found much of ths logic and information on this forum and they have always answered my questions and help with this! And also to the great community that is forming around this!

Great tutorial, thanks for the share.

1 Like

I’ve built a nice device that behaves like a watt router and I’m currently working on making it controllable with iHosts.

1 Like

I communicated with Sonoff regarding native support for photovoltaic power plants. I have the impression from the communication that they are not interested.

My idea is clear and that is reading data from the inverter and controlling consumption either by switching on the relay or actively via ssr. I bought optocouplers and when I edit the diagram I would publish it here.

This is my connection proposal:

So I present to you my work that you might be interested in. It all started with me, like everyone else, looking for a way to integrate an inverter (Huawei SUN2000, GoogWe and others) into an intelligent home control system from the company Sonoff. Since you don’t have anything like that, I was forced to build it myself. The diagram is quite simplified, but it shows the essence.

In the red box, I want to put optocouplers so that the devices are galvanically isolated from each other.

I built a simple device that reads data about current consumption and production from the inverter using RS485 (Modbus). Based on the read values, use the formula:
exported energy = production - consumption
If production is higher than consumption, it will turn on ssr1-4 according to priority.

Model example:
It exports 1kW to the grid and priority number 1 is set on SSR1. The ESP32 turns on the SSR1 output, but the output turns on and off in a 0.5-second loop. This causes the 2kW coil to run at half power.

Continuously monitors energy export and adjusts the switching on and off cycles on SSR1-4 accordingly.

If production is large and SSR1 is running at full capacity, it will turn on additional SSRs according to priority and regulate the output on all used SSRs so that the export to the grid is ±0.

The setup I used:


In green is my idea of how it could be connected with Sonoff. Connection with Sonoff gateways such as (iHost, zbb u/p, nspp) would take place via Zigbee. In that case, a nice white box without a display would be enough.

And with the appropriate outputs like:

  • A/B for Modbus.
  • SSR 1 - SSR 4 for load control.
  • Dry contact for switching.

In the application / gateway, it could be set:

  • Which load is connected to which SSR (due to regulation calculation).
  • Priority of switching SSR outputs + priority for dry contact.
  • The possibility to set scenes in combination with your other devices.
  • A detailed overview of utilized, produced, and exported energy.
  • Detailed overview of current voltage on each phase.

I am offering you my own intelligent system for regulating household consumption, which, as far as I know, the competition not have. In case of questions, do not hesitate to contact me.