Rewrite template from openHAB

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

It shouldn’t be too hard but some questions first…

Why are there two ‘=’ signs in this line of code:

var Number newlimit = (M350_power.state as DecimalType + (P_EM3_residual.state as DecimalType - offset)) /3 // * 100 % / 300 W = 1/3

also are “%” and “W” just units and can be ignored? Otherwise I don’t know what they represent.

and what does “/3 // * 100 % / 300 W” do? I don’t know what " // * " is doing.

what are the entities that represent:

M350_power
P_EM3_residual
M350_limit_rel

what are these two functions doing:

postUpdate(diff_Power_to_Limit, diff_PtoLimit) 
postUpdate(limit_nonpersistent_relative, newlimit)

I don’t see the first variables in those functions defined in the rule anywhere.

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

this is how it should look like in the end :

Sorry but you didn’t answer any of my questions.

the question you tried to answer didn’t provide what I needed.

those entities I was asking for should be “sensor.something”

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.

Those are comments. // means comment in some languages. yaml and python use #.

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.

2 Likes

Duh! :man_facepalming:

I caught that in the first line but totally missed it in that line for some reason.

I’ll blame it on being up all night at work. Yeah, that’s it…

1 Like

@petro

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 ?

Thank you

nothing, that’s just the highlighter in the forums. The forums do not understand jinja, so it highlights random crap.

You’ll get 3 separate sensors. If you can’t find them, there’s most likely an error. I did not test any of this, it could have errors.

1 Like

I can see this

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.

1 Like

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.

Please note the main sections.

1 Like

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?

2 Likes

this is how my configuration looks like

template:
  - sensor:
      - name: "Strom-Wohnung-aktuell"
        unit_of_measurement: "W"
        device_class: "power"
        state_class: "measurement"
        state: >
            {{ state_attr('sensor.poweropti', 'Watt') }}
      - name: "Strom-Wohnung-Bezug"
        unit_of_measurement: "kWh"
        device_class: "energy"
        state_class: "total_increasing"
        state: >
            {{ state_attr('sensor.poweropti', 'A_Plus') }} 
      - name: "Strom-Wohnung-Netz-Lieferung"
        unit_of_measurement: "kWh"
        device_class: "energy"
        state_class: "total_increasing"
        state: >
            {{ state_attr('sensor.poweropti', 'A_Minus') }}  
  
      - name: "New-Limit"
    state: >
     {% set offset = 0 %}
      {% set power = states('sensor.hm_600_power') | float(0) %}
      {% set residual = states('sensor.strom_wohnung_aktuell') | 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.hm_600_power') | float(0) %}
         {% set limit_rel = states('sensor.number.limit_nonpersistent_relative') | float(0) %}
        {{ power - 3 * limit_rel }}
  - name: "Limit Nonpersistent Relative"
    state: >
      {% set new_limit = states('sensor.new_limit') | float(0) %}
      {% set limit_rel = states('sensor.number.limit_nonpersistent_relative') | float(0) %}
      {% set residual = states('sensor.strom_wohnung_aktuell') | 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 %}

and that shows up in my logs

Logger: homeassistant.config
Source: config.py:xxx
First occurred: 20:58:48 (27 occurrences)
Last logged: 21:13:53

    Invalid config for [template]: extra keys not allowed @ data['state']. Got "{% set offset = 0 %}\n {% set power = states('sensor.hm_600_power') | float(0) %}\n {% set residual = states('sensor.strom_wohnung_aktuell') | float(0) %}\n {% set newlimit = (power + residual - offset) / 3 %}\n {% if newlimit >= 100 %}\n 100\n {% elif newlimit <= 0 %}\n 0\n {% else %}\n {{ newlimit }}\n {% endif %}\n" required key not provided @ data['sensor'][3]['state']. Got None. (See /config/configuration.yaml, line 29).
    Invalid config for [template]: [name] is an invalid option for [template]. Check: template->name. (See /config/configuration.yaml, line 62).
    Invalid config for [template]: extra keys not allowed @ data['state']. Got "{% set offset = 0 %}\n {% set power = states('sensor.hm_600_power') | float(0) %}\n {% set residual = states('sensor.strom_wohnung_aktuell') | float(0) %}\n {% set newlimit = (power + residual - offset) / 3 %}\n {% if newlimit >= 100 %}\n 100\n {% elif newlimit <= 0 %}\n 0\n {% else %}\n {{ newlimit }}\n {% endif %}\n" required key not provided @ data['sensor'][3]['state']. Got None. (See /config/configuration.yaml, line 29).
    Invalid config for [template]: extra keys not allowed @ data['state']. Got "{% set offset = 0 %}\n {% set power = states('sensor.hm_600_power') | float(0) %}\n {% set residual = states('sensor.strom_wohnung_aktuell') | float(0) %}\n {% set newlimit = (power + residual - offset) / 3 %}\n {% if newlimit >= 100 %}\n 100\n {% elif newlimit <= 0 %}\n 0\n {% else %}\n {{ newlimit }}\n {% endif %}\n" required key not provided @ data['sensor'][3]['state']. Got None. (See /config/configuration.yaml, line 29).
    Invalid config for [template]: [name] is an invalid option for [template]. Check: template->name. (See /config/configuration.yaml, line 67).

There is a pretty obvious indentation issue here.

@nickrout that was the problem. I corrected it and now the individual sensors also appear . thanks you :slight_smile:

@petro Many thanks for your help. Because I’m new to Home Assistant, I’ve learned something new :slight_smile:

I have one last question :slight_smile: .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?