Automation issue - sensor becomes unavailable for a moment

Hi I’ve some trouble with an automation, any suggestions are welcome.

I need to be informed when this sensor triggers to a new value but the issue is that it first becomes unavailable for a moment.
Beside this, I want to know its value BEFORE it becomes unavailable, but that’s where I’m struggling because I can’t use trigger.from_state then.

alias: test
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.mysensor
condition:
  - condition: template
    value_template: "{{ trigger.from_state.state not in ['unknown'] }}"
  - condition: template
    value_template: "{{ trigger.from_state.state != trigger.to_state.state}}"
  - condition: template
    value_template: "{{ states('sensor.mysensor') | is_number }}"
  - condition: template
    value_template: "{{ trigger.to_state.state not in ['unknown', 'unavailable'] }}"
action:
  - service: notify.myself
    data:
      message: >-
        {{ trigger.to_state.name }} goes from {{ trigger.from_state.state }}x to {{ trigger.to_state.state }}x.
      title: change update
mode: single

You might need to set up a template sensor to filter out these values. State value of:

{% if states('sensor.my_sensor') in ['unknown', 'unavailable'] %}
  {{ this.state }}
{% else %}
  {{ states('sensor.my_sensor') }}
{% endif %}

then use that template sensor in the automation.

1 Like

ah good suggestion. I was thinking about an input_text to save the current value and then a template to compare the value to the newer. and to use it for the notification, but that’s maybe to much work.
you mean something like this? the template sensor looks like this now:

template:
  - sensor:
    - name: "MySensor"
      unit_of_measurement: "x"
      state: >-
        {{ state_attr('sensor.mysensor', 'rows')[0]['impressions'] }}
      availability: >-
        {{ state_attr('sensor.mysensor', 'rows') is not none and state_attr('sensor.mysensor', 'rows').impressions is not none }}

change to:

template:
  - sensor:
    - name: "MySensor"
      unit_of_measurement: "x"
      state: >-
        {% if states('sensor.mysensor') in ['unknown', 'unavailable'] %}
          {{ this.state }}
        % else %}
          {{ state_attr('sensor.mysensor', 'rows')[0]['impressions'] }}
        {% endif %}
      availability: >-
        {{ state_attr('sensor.mysensor', 'rows') is not none and state_attr('sensor.mysensor', 'rows').impressions is not none }}

hm when I try that, it reports not available…

Ok the final sensor looks like

template:
  - sensor:
    - name: "mysensor"
      unique_id: mysensor
      state: >-
        {% if states('sensor.mysensor') in ['Active'] %}
          {{ state_attr('sensor.mysensor', 'rows')[0]['impressions'] }}
        {% else %}
          {{ this.state }}
        {% endif %}
      availability: >-
        {{ state_attr('sensor.mysensor', 'rows') is not none 
        and state_attr('sensor.mysensor', 'rows').impressions is not none }}

but I think that availability is not needed anymore right? because you filter the none (and also the unavailable and unknown) values out with the “state” line. Correct me if I’m wrong.

hmm I thought it was solved by the latest template sensor (message above this one)
but I stil get these unexpected notifications:

  domain: notify
  service: myself
  service_data:
    message: mysensor goes from unavailablex to 27x.
    title: change update

and another one, directly after that one:

  domain: notify
  service: myself
  service_data:
    message: mysensor goes from 30x to unavailablex.
    title: change update

Yes there was a change, the sensor went from 27 to 30x but I don’t understand why it triggers multiple times (actually 4 times) and why it still goes to unavailable? I don’t see the unavailable though when I look up the sensor.

so the automation executed 4 times in a row, older to new:

  1. mysensor goes from 30x to unavailablex.
  2. fails at value_template: ‘{{ trigger.from_state.state != trigger.to_state.state}}’
  3. fails at value_template: ‘{{ trigger.from_state.state != trigger.to_state.state}}’
  4. mysensor goes from unavailablex to 27x.

???

The entire purpose of the template sensor suggestion was to have an entity that didn’t go unavailable. So yes, you need to remove the unavailability template.

Additionally, if you would like to stop triggering on attribute changes, add to: and leave it empty. This would allow you to remove your to_state != from_state condition.

Yes I changed the trigger to this

platform: state
entity_id:
  - sensor.mysensor
for:
  hours: 0
  minutes: 0
  seconds: 0
to: null

But it still triggers multiple times to unavailable, I dont get it

Post the whole config for your sensor, and also look in the logbook to see what states sensor.mysensor is changing between when you are getting notified 4 times in a row.