Template sensor: how to write to log (to debug specific&rare situations)

I have a complex template sensor and would like to write something to a log file (wherever is easiest) in specific situations from inside the state part of the template sensor. I can’t find anything in the documentation. Here’s the part of the code:

{% set sensors = [
  {'name': 'sensor.room1_aqara_light_sensor_illuminance_lux', 'max': 9000},
  {'name': 'sensor.room2_aqara_light_sensor_illuminance_lux', 'max': 2500},
  {'name': 'sensor.room3_aqara_light_sensor_illuminance_lux', 'max': 2200}
] %}
{% set solar_elevation = states('sensor.sun_solar_elevation') | float(0) %}
{% set sin_elevation = sin(solar_elevation * 3.141592653589793 / 180) %}
{% set ns = namespace(ratios=[]) %}
{% for sensor in sensors %}
  {% if states(sensor.name) not in ['unavailable', 'unknown', 'none'] %}
    {% set ratio = (states(sensor.name) | float(0)) / (sensor.max * sin_elevation) %}
    {# prevent a ratio from being larger than 1, which would indicate that this sensor has its maximum defined too low #}
    {% set ratio = [ratio, 1] | min %}
    {% set ns.ratios = ns.ratios + [ratio] %}
  {% endif %}
{% endfor %}
…

Basically what I do is to calculate the ratio of the current light level to the light level at clear sky at the current sun elevation. This helps me to calculate the solar irradiance. As you can see in the comment, I sometimes seem to have ratios > 1, which indicate that I need to increase the peak light level of that sensor.

Instead of just limiting the ratio with 1, I would want to alert myself somehow in such situations with the exact values for measured illuminiation and sun elevation.

Is there any way to output/alert/log/… something from a template sensor in specific situations? Thanks!

You can’t write to a log from a template. That’s why you can’t find anything in the docs about it. It’s not possible.


FYI pi is a variable in jinja. Your (0) for the float isn’t needed if you’re checking for unavailable. And about that, you can use is_number.

{% set sensors = [
  {'name': 'sensor.room1_aqara_light_sensor_illuminance_lux', 'max': 9000},
  {'name': 'sensor.room2_aqara_light_sensor_illuminance_lux', 'max': 2500},
  {'name': 'sensor.room3_aqara_light_sensor_illuminance_lux', 'max': 2200}
] %}
{% set solar_elevation = states('sensor.sun_solar_elevation') | float %}
{% set sin_elevation = sin(solar_elevation * pi / 180) %}
{% set ns = namespace(ratios=[]) %}
{% for sensor in sensors if states(sensor.name) | is_number %}
  {% set ratio = (states(sensor.name) | float) / (sensor.max * sin_elevation) %}
  {# prevent a ratio from being larger than 1, which would indicate that this sensor has its maximum defined too low #}
  {% set ratio = [ratio, 1] | min %}
  {% set ns.ratios = ns.ratios + [ratio] %}
{% endfor %}

and to make sure that the above doesn’t have a divide by zero, you should add an availablity template that checks 'sensor.sun_solar_elevation' for a value.

{{ 'sensor.sun_solar_elevation' | has_value }}

Petro, thanks for all the suggestions. But besides these, how would you suggest to debug this template sensor if I can’t write to a log? There must be a different solution? I want to catch rare occurrences, maybe a brief moment once a month, and then save two values so I can process them later.

EDIT: Could I write to a log or send a notification or anything similar from a script? Then I could copy the code from the template sensor to a script and run an automation that calls that script every 5 minutes.