Hello
I’m trying to understand how yaml sensors and template work…
I am trying to write some code taken from examples to create some sensors to calculate mold risk in multiple rooms.
I get some code from a website here in Italy that calculates absolute humidity by using a macro (jinja)
{% macro umidita_assoluta(temp,hum) -%}
{% if states(hum)|float==0 or states(temp)=='unknown' -%}
'unknown'
{%- else %}
{{ (states(hum)|float * 6.112 * 2.1674 * e**((states(temp)|float*17.67)/(states(temp)|float+243.5)) / (states(temp)|float+273.15))|round(2) }}
{% endif %}
{%- endmacro %}
Then I put this code in configuratio.yaml
template: !include templates.yaml
and in templates.yaml I put this
- sensor:
- name: "umidita assoluta esterna"
unique_id: umi_ass_ext
state: >-
{% from 'tools.jinja' import umidita_assoluta %}
{{ umidita_assoluta('sensor.termoigrometro_esterno_temperature', 'sensor.termoigrometro_esterno_humidity') }}
unit_of_measurement: 'g/m³'
icon: mdi:water-outline
state_class: measurement
It works and I can use/move/assign it freely in the UI
Then I made some other sensors to calculate mold risk
I put this in configuratio.yaml
sensor: !include sensors.yaml
and in sensors.yaml I wrote
# mold risk first room
- platform: mold_indicator
name: mold_indicator_matrim
#unique_id: mold_risk_matr
indoor_temp_sensor: sensor.termoigrometro_matrimoniale_temperature
indoor_humidity_sensor: sensor.termoigrometro_matrimoniale_humidity
outdoor_temp_sensor: sensor.termoigrometro_esterno_temperature
calibration_factor: 2
# mold risk second room
- platform: mold_indicator
name: mold_indicator_dav
#unique_id: mold_risk_dav
indoor_temp_sensor: sensor.termometro_igrometro_davide_temperature
indoor_humidity_sensor: sensor.termometro_igrometro_davide_humidity
outdoor_temp_sensor: sensor.termoigrometro_esterno_temperature
calibration_factor: 2
# mold risk nthroom etc..
these sensors work but I can’t assign them a unique_id or a friendly_name so I can’t freely use them in UI.
If I try to assign them to a zone I get an error like
" This entity does not have a unique ID"
Is there some way I can assign them a unique ID?
Can these latter sensors be merged into templates so I can format like the others in templates.yaml?
Where can I learn some simple code to get confidence with this coding?
Thanks everybody
Pietro
P.S.
I can’t understand how to format code in this post…