How can I create a template that monitors my solar system and independently changes the values in order to achieve zero injection ? I have already received a Rule from openHAB from another group. is it possible to rewrite this?
setup : Hoymiles inverter
Smartmeter
openDTU
as an example the rule of openHAB :
rule "Adjust inverter limit"
when
Item P_EM3_residual received update
then
var offset = 0 // 0 Watt target to regulate on!
var Number newlimit = (M350_power.state as DecimalType + (P_EM3_residual.state as DecimalType - offset)) /3 // * 100 % / 300 W = 1/3
if (newlimit >= 100) newlimit = 100
if (newlimit <= 0) newlimit = 0
var Number diff_PtoLimit = M350_power.state as DecimalType - (M350_limit_rel.state as DecimalType * 3)
postUpdate(diff_Power_to_Limit, diff_PtoLimit)
if (((newlimit < 100) || (M350_limit_rel.state != 100)) && (P_EM3_residual.state < 10 || ((diff_PtoLimit <= 5) && (diff_PtoLimit >= -5)))) {
var Number diff_r = newlimit - M350_limit_rel.state as DecimalType
if ((diff_r >= 1.5) || (diff_r <= -1.5)) {
postUpdate(limit_nonpersistent_relative, newlimit)
}
}
end
M350_power ----> means the inverter
P_EM3_residual -----> means a shelly 3em
M350_limit_rel ----> means limit relative ( in % )
I’m trying to implement a zero injection for my case, which can read the value from my smart meter and sends this offsetting calculation to the inverter via MQTT and adjusts the value. OpenDTU is able to read the inverter and regulate the power.
OpenDTU and Smartmeter are integrated in HA I only need a suitable template for control
Everything after “//” is a comment and can be ignored. This is just to make the code easier to understand.
M350_Power is the power of the balcony power plant
P_EM3_residual is the power of adding the three phases of the Shelly EM3
M350_limit_rel is the relative limit of the TSUN M350 inverter what it outputs on MQTT.
Since only the variable is updated. Can be equated with an “=”.
First for debug purposes only
The second writes the new limit which is then published on MQTT.
based on what you have, these are a series of sensors that represent the data you’re calculating. It assumes you actually have these items as entities
template:
- sensor:
- name: New Limit
state: >
{% set offset = 0 %}
{% set power = states('sensor.m350_power') | float(0) %}
{% set residual = states('sensor.p_em3_residual) | float(0) %}
{% set newlimit = (power + residual - offset) / 3 %}
{% if newlimit >= 100 %}
100
{% elif newlimit <= 0 %}
0
{% else %}
{{ newlimit }}
{% endif %}
- name: Diff Power to Limit
state: >
{% set power = states('sensor.m350_power') | float(0) %}
{% set limit_rel = states('sensor.m350_limit_rel') | float(0) %}
{{ power - 3 * limit_rel }}
- name: Limit Nonpersistent Relative
state: >
{% set newlimit = states('sensor.new_limit') | float(0) %}
{% set limit_rel = states('sensor.m350_limit_rel') | float(0) %}
{% set residual = states('sensor.p_em3_residual) | float(0) %}
{% set diff = states('sensor.diff_power_to_limit') | float(0) %}
{% if (new_limit < 100 or limit_rel != 100) and (residual < 10 or -5 <= diff <= 5) and -1.5 <= new_limit - limit_rel <= 1.5 %}
{{ new_limit }}
{% else %}
{{ this.state if this.state is defined else 0.0 }}
{% endif %}
You can use an automation to set the non persistent limit as well, might be easier too and it will persist through restarts.
Either way. I’d also recommend trying to use HA the way it was intended instead of trying to replicate something from openhab as the translation is not 1 to 1.
Thank you very much, I still have two questions. what do the red values in the formula mean and why is this configuration not displayed as a sensor when I search for it in the developer tools ?
ok, that just shows you the templates work. If they aren’t showing up, you’re doing something else wrong when adding it to your configuration. And that would show up as an error in your logs.
Nope. You didn’t copy the entire section I posted. You’re missing the main sections. Also, don’t post pictures, I can’t see the entirety of what you posted. You might have done it correctly, but your image doesn’t show everything. Also, people can’t copy/paste from an image and are more likely to avoid helping you. Please take the time to post text.
what version of HA are you running? Also, to format your text, use 3 backticks, just like you did in the original post.
FYI this won’t work in the template editor. It will only work in your configuration. So if you’re getting that error in the template editor, that’s expected. I’m also not really convinced you know where errors will show up… do you know where to look?
I have one last question .Now I have deposited these 3 sensors. Is it correct to realize my plan that I have to create an automation with these sensors? if so what would be the best way to build it?