Ignoring empty or null sensors

Hi guys,

I’m still slowly getting my head around home assistant and feel like i’m almost there, but as soon as i understand a little more - it gives me more ideas!

Anyway - i’m stuck on a notification automation at the moment.

automation:

- id: vacuum_complete_ios_notification
  alias: Vacuum Complete
  trigger:
    platform: state
    entity_id: sensor.vacuum_cleaning_count
  condition:
     - condition: template
       value_template: '{{ trigger.from_state.state != none }}'
  action:
    service: notify.ios_steves_iphone
    data:
      title: "Home Assistant"
      message: "Jeeves has finished cleaning."
      data:
        push:
          thread-id: "home-assistant-vacuum"

Occasionally the sensor.vacuum_cleaning_count value goes to nothing and triggers the automation.

I’ve tried a handful of different entries in the value_template: ‘{{ trigger.from_state.state != none }}’ line but none of them seem to help.

The log shows:

Line 15: 2019-01-23 14:32:04 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=sensor.vacuum_cleaning_count, old_state=<state sensor.vacuum_cleaning_count=; friendly_name=vacuum_cleaning_count @ 2019-01-23T14:29:18.645057+11:00>, new_state=<state sensor.vacuum_cleaning_count=57; friendly_name=vacuum_cleaning_count @ 2019-01-23T14:32:04.127175+11:00>>

So I’m just not sure how to ignore based on old_state=<state sensor.vacuum_cleaning_count=;

I went down the path of trying to only have it trigger on an integer increase but unfortunately got nowhere. Just wondering if anyone has any advice?

Thanks!

you can try something like this for the conditions:

condition:
  - condition: template
    value_template: '{{states.sensor.vacuum_cleaning_count.state | int > 0}}'
  - condition: template
    value_template: '{{ trigger.to_state.state|int > trigger.from_state.state|int }}'

But what might be better is to fix your sensor to not give you null values in the first place.

is you sensor a template sensor that you created? if so then post your sensor config.

and what is the state of the sensor when it’s “nothing”? is the state blank or something else?

1 Like

Thanks for the reply @finity!

Guilty as charged - I used a template platform sensor which just uses an attribute from the xiaomi_miio vacuum entry.

sensor:
  - platform: template
    sensors:
      vacuum_cleaning_count:            
        value_template: "{{ states.vacuum.xiaomi_vacuum_cleaner.attributes.cleaning_count }}"

This was before I worked out that I can actually use the attributes within a trigger but I’ve just left it for now as your code suggestions worked perfectly.

So thanks again :slight_smile: