I hope somebody can help me with the following issue. I control my lights based on outside light levels using a NodeMCU with ESPhome on it. A simple setup using a LDR. Before this setup, i done it with the same LDR and MQTT (non ESPHome). The LDR gives a value and with the template sensor i do some math(own mapping calculation) stuff to set the light brightness level.
With my previous setup (MQTT) i didn’t had any problems. With my new setup (ESPHome) Hassio does detect when the sensor isn’t available. That results in a sensor value ‘unavailable’. For now i made a workaround to detect that value and set a sensor to 0 or 1. In the automation script i only set new brightness levels if the sensor is not 0 = ‘unavailable’ using a condition.
I can’t figure out how to handle this in the sensor template itself. If i using INT or FLOAT and the sensor value isn’t numeric it has an outcome. I suspected something else…
Is there a trick to not update a template sensor when the ESPHOME sensor is ‘unavailable’?
My brightness math:
value_template: '{{((((states.sensor.living_room_brightness.state | int - 1) * (1 - (states.input_number.maxbright.state) | int) / (states.input_number.maxldr.state | int -1 ) + (states.input_number.maxbright.state) | int ) | int -1) | float * (states.sensor.brightfactor.state) | float) | int}}
If the room_brightness is a string (no numeric) it will always result in the maxbright state.
Hope you understand my problem.
If any info is needed, let me know.
Can you please read the sticky at the top of the forum.
Trying to read code as you have posted it is near impossible, it should be (as per the sticky) quoted in ‘preformatted text’ (using the </> button above the text box).
Given your problem and a quick scan, it seems because you are using initialisation issues given your use of
states.sensor.living_room_brightness.state | int - 1
Rather than one of the bracketed evaluations, (from memory) something like
not numeric means unavailable or generally not usable as numeric?
in the first case you can limit yourself by comparing the state to unavailable (and maybe unknown?)
but as you’ll need to convert it to a float anyway, maybe it’s possible to use float’s default set to some “impossible” value like this:
{% set invalid_value = -48 %}
{% set resut = states('sensor.brightfactor') | float(invalid_value) %}
{% if result != invalid_value %}
# do your maths and return a new value
{% else %}
{{ states("sensor.this_sensor's_name") }} # i.e return its current value
{% endif %}