otto001
(Otto)
December 11, 2023, 3:02pm
1
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
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
otto001
(Otto)
December 11, 2023, 3:10pm
2
and: what does the “>” or even “>-” mean after the key and before the code part??
dproffer
(David Proffer)
December 11, 2023, 4:36pm
3
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
otto001
(Otto)
December 12, 2023, 6:21am
4
THANKS A LOT!
especially the template editor helped me out
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
dproffer
(David Proffer)
December 12, 2023, 6:31am
5
I now declare you a jinja god … with all associate rights and perks!
Much success with your home automation fun in 2024!
otto001
(Otto)
December 12, 2023, 6:34am
6
haha… thanks a lot!
the same to you!
coming from 8 years with fhem this is a whole new world…
dproffer
(David Proffer)
December 12, 2023, 6:37am
7
dproffer:
float(0)
and from more of my painful lessons learned, don’t forget to handle the default/exceptions …
I have this in one of my templates which gives me division by zero errors in the log:
(pasting it on multiple lines for better reading)
{{
'%.2f'%(
(states('sensor.vehicle_bat_ced') | default(0) | float) * 100
/ (states('sensor.vehicle_odo') | default(1) | float)
)| float
}}
Three things I would like to know:
is default(0) a supported function? I can’t find any documentation on it.
what will states('sensor.vehicle_odo') return, if this sensor is not available? (After restarting HA…
otto001
(Otto)
December 12, 2023, 6:40am
8
yes, exceptions and constraints are always a source of joy in programming
dproffer
(David Proffer)
December 12, 2023, 7:31am
9
otto001:
fhem
8 years of Perl? I’m buying this round for you!
otto001
(Otto)
December 12, 2023, 8:12am
10
haha. well, it is not that bad
but if I can, I prefer python…
1 Like