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.
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 }}
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:
mysensor goes from 30x to unavailablex.
fails at value_template: ‘{{ trigger.from_state.state != trigger.to_state.state}}’
fails at value_template: ‘{{ trigger.from_state.state != trigger.to_state.state}}’
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.
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.