Hi. I am using a power measure for my water tank lighting. The controller is consuming 1w is it possible to make a template for this so it show power usage as 0w when the value is below 2?
template:
- sensor:
- name: Water Tank Light Power
unit_of_measurement: W
device_class: power
state_class: measurement
state: >
{% set power = states('sensor.your_power_sensor_here')|float(0) %}
{{ 0 if power < 2 else power }}
availability: "{{ has_value('sensor.your_power_sensor_here') }}"
while weāre at itā¦
does a command like ā{{ has_value(āsensor.your_power_sensor_hereā) }}ā exist for sensorās attribute, too? Iād need oneā¦
has_value()
will report true
as long as the entity exists and its state is not unavailable
or unknown
.
If you want to check whether an attribute exists you can use
{{ state_attr('sensor.my_sensor', 'my_attribute') != None }}
There are also functions like is_number()
that can be used to verify a value is a number (or is a string that represents a number), so if you are expecting the attribute to be a number you could use:
{{ is_number(state_attr('sensor.my_sensor', 'my_attribute')) }}
Iām not sure when youād have a sensor whose attributes can go to unavailable or unknown while the sensor state itself is valid, but if you want to replicate the functionality of the has_value()
function for an attribute you could do:
{{ not state_attr('sensor.my_sensor', 'my_attribute') in [None, 'unavailable', 'unknown'] }}
YES!! Third one is āthe oneā! Thanks a lot!
It turned out that two attributes: ācurrent_temperatureā and ātemperatureā (thatās set temp for climate) can be eihter missing or āNoneā so thatās an option for all cases.
I owe you an brief explanation why i need this:
i connected my Mitsubishi climate to wifi via ESP module and esphome. My problem is that when i restart HA it often happens (but not always) that esp module is connected, itās working ( i get IP, MAC, uptimeā¦), only communication with climate doesnāt work (i donāt get temps and status). Reboot of esp module helps in this case. I have yet to discover if this is because i donāt have level converter (esp has 3.3V, while climate has 5V) or is just my climateās faultā¦ but in the meantime iāll solve this with automation which will reboot module if needed.
Thanks again!
That or:
{{ state_attr('sensor.my_sensor', 'my_attribute')|is_number }}
Are my preferred methods.
I agree, using it as a filter instead of a function is easier to read and you donāt have to count parentheses.