Automation issues with sensor.xiaomi_lywsdcgq and ESP32 ESPHome

Hi,

On the whole the sensor works fine for control but issues arise with it in Automation.

Because the sensor looses connectivity once in a while due to the ESP32 on ESPhome the sensor reading reports Unavailable.

This in turn triggers my automation to return back a notification as Unavailable even though I have a delay set for 20 seconds.

Is there something I could add to my template so if sensor is unavailable it does not trigger the automation?

- id: '1576494984622'
  alias: HOUSE TEMPERATURE OUTSIDE LIMITS NOTIFICATION
  description: ''
  trigger:
  - for: 00:00:20
    platform: template
    value_template: '{{states.sensor.xiaomi_lywsdcgq_temperature.state | float < 16.00
      or states.sensor.xiaomi_lywsdcgq_temperature.state | float > 22.50}}'
  condition: []
  action:
  - data_template:
      message: HEATING CONTROL FAILURE {{ states.sensor.xiaomi_lywsdcgq_temperature.state
        }} °C
    service: persistent_notification.create
  - data_template:
      message: HEATING CONTROL FAILURE {{ states.sensor.xiaomi_lywsdcgq_temperature.state
        }} °C
    service: notify.mobile_app_sm_g960f

A few things:

Avoid using states.sensor.temperature.state , instead use states('sensor.temperature') . It is strongly advised to use the states() , is_state() , state_attr() and is_state_attr() as much as possible, to avoid errors and error message when the entity isn’t ready yet (e.g., during Home Assistant startup).

This won’t fix the problem, but it will help the logs. It’s possible that states.sensor.temperature.state might not even exist at some point (if HA reboots while the ESP isn’t connected).

To fix the issue, take a look at what is evaluated. Open Dev Tools >> Templates and type the following into the box.

{{ "unavilable" | float < 16.00 }}

This evaluates to True. In fact, all {{ string | float < 16.00 }} is true. Why? Becuase the float filter returns a default value of 0 if it could not convert the value.

https://jinja.palletsprojects.com/en/2.11.x/templates/#float

So you have 2 options.

  1. set the default value of float to be something that wont trigger
  trigger:
  - for: 00:00:20
    platform: template
    value_template: '{{states.sensor.xiaomi_lywsdcgq_temperature.state | float(100.0) < 16.00
      or states.sensor.xiaomi_lywsdcgq_temperature.state | float(100.0) > 22.50}}'

This means any failed conversion will return 100.0. It’s not very flexible, but it will help in this case.

The better solution would be to just check that it exists first. states(‘sensor’) will return ‘unknown’ always if it doesn’t exist which is why it is recommended to use.

  trigger:
  - for: 00:00:20
    platform: template
    value_template: > 
      {% set t = states('sensor.xiaomi_lywsdcgq_temperature) %}
      {{ t != 'unknown' and t | float < 16.00 and t | float > 22.5 }}
         

Thanks for that detailed explanation.
From Example two, would Unknown not cause a trigger?

For now I have adjusted the -for: 00:00:60.

No unavailable notifications since yesterday.

Regards,

Unknown would cause a trigger, but states(‘fake_sensor’) will only ever return ‘unknown’.

You could do a python to_lower conversion if you wanted.