Help with template and sensor newbie

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…

No. The code for these sensors has not been updated to support this yet.

Also FYI, the mold_indicator sensor requires relative humidity sensors, not absolute humidity.

Ok, thanks Tom.
Yes I know that I must use the relative humidity in mold_indicator and I used it.
The two block of code are not related to each other. The first is to give an example on how I would like to format the second.
Is there any alternative way to calculate the mold risk?
Ciao

The equation used is in the source. I’ve been thinking of using this in a template sensor so I can feed it the critical temperature from an actual sensor rather than using the “calibration” of the outside temperature which really does not work well because of the thermal inertia of the object at the critical temp.

This is a fair way down on my two page to-do list though.

1 Like