I am looking at modifying an analog sensor, and want to state WET when value is < 40, DRY when value is (>41 and <1000), and to report ‘Check Sensor’ when the entity state is unknown, or null.
Any ideas? Had a look through the templating section but this is a bit outside the box.
I’m by no means an expert - but one problem is you’re comparing your state values with strings (remove the quotes around the numbers) .
If your sensor values are coming in as strings, you need to convert them to numbers for the comparison - apply the float filter first:
“states.sensor.plant_meter_sensor_31.state|float”
In python there is no “null”. You probably want to check for empty values using:
states.sensor.plant_meter_sensor_31.state is None
(or use Jinja’s “default” filter) before you convert to floats and do your other comparisons. The Automation Templating docs refer to the need for this - but none of the examples seem to include it.
Thanks @arch!
From your post I was able to work this out, much appreciated! See below for working code… When the unknown state is turned into a float, it is reflected as 0.00 so now I run conditions.
BTW, the analog sensor never goes above 950, but there is a catch all at the end anyhow.
Thanks again
"{% if (states.sensor.plant_meter_sensor_31.state|float == 0.00) %}Check Sensor{% elif ((states.sensor.plant_meter_sensor_31.state|float >= 1.00) and (states.sensor.plant_meter_sensor_31.state|float <= 40.99)) %}Wet{% elif ((states.sensor.plant_meter_sensor_31.state|float >= 41.00) and (states.sensor.plant_meter_sensor_31.state|float <= 1000.00)) %}Dry{% else %}Value Weirdness{% endif %}"
Only one thing noted in the log, which hasnt changed since the updated sensor template:
16-11-06 11:07:56 homeassistant.components.sensor.template: UndefinedError: 'None' has no attribute 'state'
doesnt appear to break anything, but I hate seeing it there at every reboot. Is this something I can change to fix this? Doesnt appear to be having any impact on HASS or the correct values being applied…