Problem to set a Variable to a defined Value for example "0"

Hello,

at the moment i have the actual problem that my SMA Inverter sets the actual Power value at night to

So in the diagramm i see no usable values :wink:
So i my idea is following : IF the Modbus Power Value ist lower than “0” then set this value permanent to “0” .

here is my actual Yaml Version which is not working :slight_smile:

I tried to generate a second variable but this also not works.


modbus: 
  - type: tcp
    host: 192.168.178.26 # SMA TriPower
    port: 502
    name: TCP_Wechselrichter
    close_comm_on_error: true
    delay: 5
    timeout: 5
    sensors:
      - name: PV_Gesamtertrag
        unit_of_measurement: kWh
        slave: 3
        device_class: 'energy'
        state_class: total_increasing
        address: 30531 #U32
        data_type: uint32
        count: 2
      - name: PV_Leistung
        unit_of_measurement: W
        slave: 3
        device_class: 'power'
        address: 30775 #S32
        data_type: int32
        count: 2
      - name: PV_Tagesertrag
        unit_of_measurement: Wh
        slave: 3
        device_class: 'energy'
        state_class: total_increasing
        address: 30517 #U64
        count: 4
        data_type: uint64

        
template:
 - sensor :
    name: PV_leistung_2
    state: >
     {% if states('sensor.Pv_Leistung') | float < 0 %}
     initial: {{ states('sensor.Pv_leistung_2') | float 0 }}
     {% else %}
     {{states('sensor.Pv_Leistung') | float }}
     {% endif %}
    unit_of_measurement: "W"
    device_class: 'power'

Can anyone help me ?

Thank alot :wink: Greets Ronny

Are you sure the sensor is not returning 0 at night and that the values are actually signed and not unsigned?

And in the template sensor you set the value to initial: {{ states(‘sensor.Pv_leistung_2’) | float 0 }}
What does the text “initial:” do there? The value should be just the number

And please format your yaml with the </> feature so it is displayed correctly so we can see indentation and not bullets

Hello Kenneth,

that was my first intention that the values not read out correctly vie modbus , i tried only to get a single Word via Modbus, changed the register high and Low Word… , because i only need a Variable up to 7000w , this didnt worked.

The Inverter sets , the value correct to “0” for some minutes at night, after that SMA sets by itself to this great Value over night. When he starts up the Value is shown correctly .

image

This displayed Code is only the result of trying to get it run , so please don´t see this as a nearly working version :rofl:

I you have some code ideas that would be nice :grinning:

 template:

* sensor :
name: PV_leistung_2
state: >
{% if states(‘sensor.Pv_Leistung’) | float < 0 %}
initial: {{ states(‘sensor.Pv_leistung_2’) | float 0 }}
{% else %}
{{states(‘sensor.Pv_Leistung’) | float }}
{% endif %}
unit_of_measurement: “W”
device_class: ‘power’

We cannot help you if you keep on posting yaml like bullets. PLEASE select the yaml, click the little </> button in the menu bar and save again. You can edit both your original post the what you just posted

Hi Kenneth,

i thought i did this , is it better now ?

Greets Ronald

You have

template:
 - sensor :
    name: PV_leistung_2
    state: >
     {% if states('sensor.Pv_Leistung') | float < 0 %}
     initial: {{ states('sensor.Pv_leistung_2') | float 0 }}
     {% else %}
     {{states('sensor.Pv_Leistung') | float }}
     {% endif %}
    unit_of_measurement: "W"
    device_class: 'power'

I do not understand where the “initial:” comes from. That is not as I can see anything in the syntax for a template sensor.

Does this work?

template:
 - sensor:
     name: PV_leistung_2
     unique_id: pv_leistung_2
     state: >
       {% if float(states('sensor.Pv_Leistung'),-1) < 0 %}
         {{ float(states('sensor.Pv_leistung_2'),0) }}
       {% else %}
         {{ float(states('sensor.Pv_Leistung'),0) }}
       {% endif %}
      unit_of_measurement: "W"
      device_class: 'power'

Let explain.

First I indent so things are easier to read. And in some cases this is also required to make it work

I use the format float(variable,0). It is safer than piping with | as many falls into traps with order of operands.

The if statement checks if the sensor Pv_Leistung is less than 0. We convert the sensor to float as they are always returned as strings. We set the default of the float function to -1 so when the sensor is undefined, you get same result as when it is the negative value you look for.

the else statement sets the sensor to its existing value which is hopefully more relevant to hold on to than setting it to 0. If it is undefined like when you start, it defaults to 0

Try this.

Hello Kenneth,

now itś running perfect .

Thanks you for youŕe help :+1:

Glad it works

I just added unique_id to the solution

Always put a unique_id which can be any string but it has to be unique and you should avoid spaces as I have seen occational bugs with that. The unique ID enables additional user interface features so you can alter the icon etc. Otherwise you get a warning message that it cannot be managed because of missing unique_id. All the mqtt and template integrations supports this now so it is worth adding then everywhere. It does not alter the way the item works. If you later change the unique ID you have to remove some ghost things so define it once and never alter them. That is why many use some random string generator to make them. I personally key a string based on the name.