[newbie]: mqtt and absolute value/splitting

Hi,

still pretty new to HA, I am having a hard time to understand all the concepts.
Now, I have the following situation:

I want to setup energy stuff in HA. Having a sungrow iverter which I am monitoring using modbus on fhem, I setup fhem to publish the relevant stuff using mqtt.
In my setup I have a value “impex” - this value is positive when I am selling energy and negative when I have to buy it.

Now, I would like to split up this to two entities using HA. I know, how this could be easily done using fhem, but I want to learn how things are done in HA.

I just created a file temlates.yaml and imported it in configuration.yaml:

template: !include templates.yaml

in this file, I have been playing around, but without big success and nothing (for me) helpful in the logs:

sensor:
  - name: PV_mom_einkauf
    unique_id: PV_mom_einkauf
    unit_of_measurement: "Wh"
    state: >
      {% set wert = ((states('sensor.pv_export_2') | float) %}
      {% if wert < 0 %}
        {{ wert * -1}}
      {% else %}
        0

Now, I am absolutely not sure what I am doing :wink:
besides that there is a cryptic error when reloading HA:

Invalid config for 'template' at templates.yaml, line 5: invalid template (TemplateSyntaxError: unexpected '}', expected ')') for dictionary value 'sensor->0->state', got "{% set wert = ((states('sensor.pv_export_2') | float) %} {% if wert < 0 %}\n {{ wert * -1}}\n{% else %}\n 0\n"

I read around a lot, but am absolutely not sure how this is intended to be done?
Best regards and thanks in advance,
Otto

and: what does the “>” or even “>-” mean after the key and before the code part??

Jinja2 is an ‘art’ more than a ‘science’ IMHO. I’m far from an expert with it and often stumble… There are some real jinja gurus here on this forum, do look them up. Good hunting!

Have a look here for some basic info:
https://jinja.palletsprojects.com/en/3.1.x/api/#basics

You might need

{{ 0 }}

rather than just

0

The template editor in Home Assistant dev tools can be very helpful:

    state: >
      {% set wert = ((states('sensor.pv_export_2') | float) %}
      {% if wert < 0 %}
        {{ wert * -1}}
      {% else %}
        0

Have a look at template sensor docs as well, link here. Examples I have shown below:

  - platform: template
    sensors:

# stove temperature minus ambient kitchen temperature
      stove_kitchen_temp_diff:
        friendly_name: "Stove To Kitchen Ambient Delta"
        value_template: >-
          {% set stove = states('sensor.kitchen_stove_govee_h5074_temperature') | float(0) %}
          {% set kitchen = states('sensor.kitchen_atc_mi_temperature') | float(0) %}
          {{ (stove - kitchen) | round(2) }}
        unit_of_measurement: '°F'
# lgtv hdmi1

  - name: "LG TV HDMI 1"
    unique_id: "C8:08:E9:7A:ED:D5-HDMI-1"
    command_topic: "lgtv/command"
    payload_on: "tv_hdmi_1"
    # payload_off: "OFF"
    state_topic: "lgtv/input"
    state_off: "OFF"
    state_on: "ON"
    value_template: "{{ 'ON' if value == 'tv_hdmi_1'  else 'OFF' }}"
    retain: true

THANKS A LOT!
especially the template editor helped me out :slight_smile:

just for sake of completeness:

sensor:
  - name: PV_mom_einkauf
    unique_id: PV_mom_einkauf
    unit_of_measurement: "Wh"
    state: >-
      {% set wert = (states('sensor.pv_export_2') | float) %}
      {% if wert < 0 %}
        {{ wert * -1 }}
      {% else %}
        {{ 0 }}
      {% endif %}
  - name: PV_mom_verkauf
    unique_id: PV_mom_verkauf
    unit_of_measurement: "Wh"
    state: >-
      {% set wert = (states('sensor.pv_export_2') | float) %}
      {% if wert < 0 %}
        {{ 0 }}
      {% else %}
        {{ wert }}
      {% endif %}
1 Like

I now declare you a jinja god … with all associate rights and perks! :coffee: :grin:

Much success with your home automation fun in 2024!

haha… thanks a lot!
the same to you!
coming from 8 years with fhem this is a whole new world…

and from more of my painful lessons learned, don’t forget to handle the default/exceptions …

yes, exceptions and constraints are always a source of joy in programming :wink:

8 years of Perl? I’m buying this round for you! :cocktail: :grinning:

haha. well, it is not that bad :slight_smile:
but if I can, I prefer python…

1 Like