Why is that? It has been working but I would like to know your reasoning.
I put the time pattern in because if a sensor goes offline, ie. No data is received, then it will keep its last value. In this case I want it to report unavailable. And the time pattern I presume forces it to evaluate and the result will be unavailable.
Index of the trigger. (The first trigger idx is 0.)
trigger.platform
Hardcoded: time_pattern
trigger.now
DateTime object that triggered the time_pattern trigger.
If you check your logs, you will likely find an āUndefined Errorā every 10 minutes because trigger.to_state is undefined.
Every firing of the time pattern trigger will set the sensorās value to āunavailableā. In the following I used an analog to the sensor as you have it configured except I switched it to a 3 minute interval so the pattern is more apparent:
The longer gaps are when I followed a time pattern trigger with a input where d[3] was blankā¦ If your serial device is streaming data in regularly enough you may not notice the little blips of āunavailableā, but theyāre likely there.
You could guard against that with a more complex template, but itās not necessary.
With a properly configured State trigger, when no data is received from the serial gauge, the trigger will not fire, and the sensor keeps itās last value anyway. One thing you could do to make sure your state trigger doesnāt cause issues is to reject āunavailableā and āunknownā as follows:
You are absolutely correct! Thank you. So basically I am setting all of my sensors, I have 15, to be unavailable every 10 minutes just to catch out the sensor that does not report data anymore. However to keep reporting the last known value is not acceptable for me. How do you suggest I can detect if a sensor stops reporting data?
I am not sure how this will work as it will only stop firing the trigger if sensor.serial_gauge is Unknown or Unavailable. In my case there will always be data, but only some fields my be null.
I must have misunderstood your goal. For clarity, what was the purpose of the this.state in your original if not to keep reporting last known value as current value?
If the state of the gauge sensor being āunavailableā or āunknownā is not sufficient, what other criteria need to be considered to define āsensor stops reporting dataā? For example, if d[3] is null for 1 minute ( or 5, 10, 20ā¦), should that be considered reporting or not?
How do you want to be shown that the gauge sensor is not reporting? You can use further templating to set the value of attributes. Or you could set an availability template for sensor.pressureā¦ we need to know what the goal is.
The entity_id: sensor.serial_gauge receives information and I create 15 different sensors based on this information.So in my original code, under this 1 trigger I have 15 sensors defined. Each new line is information about an individual sensor based on the text value of d[0]. In the example 1 sensor is āPRESSā. But I have 14 others. So each of these 15 sensors get triggered each time and I uses the this.state to keep the same value in the case when d[0] does not match. Otherwise it will lose its value. I hope this explanation makes sense. Please tell me if there is another way to achieve this.
Unavailable and Unknown is very sufficient. This is there reason I used a time pattern. But If a sensor, for example the āPRESSā one stops reporting the trigger will never have a chance to to set it to Unavailable or Unknown because d[0] will never match āPRESSā again as it is not being reported.
say my serial data is
"PRESS,,f,65,M,,F*3F"
"WEGT",12,1,F,5,4F
"PRESS,,f,60,M,,F*3F"
"WEGT",14,1,F,5,4F
"WEGT",11,1,F,5,4F
"WEGT",12,1,F,5,4F
Here "PRESS" has stopped reporting but this.state will stay as the last reported value "60"
But here I want to report PRESS as unavailable.
Much appreciated your help.
So how do we detect when something doesnāt happenā¦?
Do the ātopicsā always come in the same order i.e. does PRESS always follow WEGT in normal operation? If so, you could do something like the following:
- trigger:
- platform: state
entity_id: sensor.serial_gauge
sensor:
- name: pressure_2
unique_id: pressure_2_id
state: >-
{% set current = this.state | default(0) %}
{% set d = trigger.to_state.state.split(',') %}
{% if trigger.to_state is search('WEGT') and not trigger.from_state.state is search('PRESS') %}
ERROR
{% else %}
{{ d[3] if (d | count > 3 and d[0] == 'PRESS' and d[3] != '') else current }}
{% endif %}
Unfortunately no. The serial device is a combination of other devices on a communication bus. So there is no control of the order. The only thing certain is that each device reports at least every 30 seconds.
I am really stumped to find a way to solve this. At worst I will need to live with a device offline still showing its last known value. But this is far from ideal.
Is there a way I can use the time pattern that I had but stop the sensor failing momentarily?
Maybe I can set a timestamp attribute for each sensor and if it is more than 60 Secs old then the gauge would show Unavailable? Is this actually possible in YAML?
I spent 20 years of my life coding but trying to solve problems with YAML drives me a bit crazy!
Itās possible to pull the current state of the serial sensor when the time pattern trigger fires, but the issue becomes that, as long as itās one of the normal strings, thereās no way to know whether the state that is captured at that moment indicates a failure of a specific sensor.
I think youāre going to have to go with a timestamping approach and maybe a binary sensor that indicates errors either for each individual sensor or the groupā¦ hereās a rough draft I came up with (just using the value sequence you provided earlier).
FWIW, you can get a line graph and statistical output by adding unit_of_measurement: and state_class: measurement, respectively, to the numeric sensors. I left it off to make it easier to see all three together in the History tool.
Brilliant. Thanks. I will try this out in the morning.
And also the state_class tip.
Thanks also for showing the history logbook. I never released I could trace the yaml execution like this. I have been running blind.
You have a high level of understanding of yaml. How did you learn it and what resources are there available? I am ok but am using trial and error alot because I cannot find any good resources.
Templating: Home Assistant Templating : This is the the most comprehensive and up-to-date source for HA-specific functions.
Jinja Docs: These docs are not geared toward new/casual users, but they contain a lot of important information.
Jinja for Ninjas: @skalavala has put together a well organized tutorial and group of examples. Itās been a while since this was updated, so there are better/easier ways to do some of the things shown, but itās still a good resource.
Since Jinja is built on Python, many Python methods also work in templates. Iāve never seen a complete list of which methods work and which donātā¦ But the Official Python docs have come in handy for me multiple times to figure out whatās going on in templates posted by some of the more advanced users on this forum.
I have just spent a few hours going around in circles. But I have just proved something strange.
Basically if my template sensor specifies a unit_of_measurement: āBarā then the trigger will fail if there is an unexpected null value. If I then put in the "and d[3] != " it will also fail but the sensors afterward will not be Unknown, they will just keep that same value forever.
BUT if remove the unit_of_measurement from the sensor that has a null value everything works well. I do not even need to perform the "and d[3] != " comparison. The trigger will not fail at that sensor and will continue evaluating the other sensors.
I repeated this 3 times. Put in unit_of_measurement and the following sensors are stuck with the same value. Remove the unit_of_measurement and it all works.
Is this a bug?
It is as if a null value is always an error if unit_of_measurement is specified. I have also tried different units like m and mph. Same result.
Working with the, admittedly limited, testing method I used previously I cannot reproduce what you have described. Post a screen shot of one of the sensor configurations that being problematic as well as any history or other info you think is pertinent.
It may not be readily apparent in the image, but I edited the sensor configuration and reloaded the integration throughout the test to include unit_of_measurement and state_class both individually and together. All three configurations showed up exactly the same.
It is incredible.
I go to sleep and now the next day I cannot reproduce this either now. It now failing again on the null 3rd element. Yesterday it was working all fine.
I will try again. There seems to be something strange happening when there is a null value.
In your test did you put in a null value to replicate what I have?
eg.
āPRESS,f,65,M,F3F"
"PRESS,f,M,F3Fā
So now it is working again. All I did was re arrange the order of the sensors in the trigger. I put the one, waterdepth, that has a null value last so that the following 2 sensors work. And then I put the original code back with waterdepth first and it keeps working.
I restart HA each time via developer tools.
Is it possible restart does not clear all of the state values?
Here is the original code and which is working again even though a value is null.
And now I put in a unit_of_measurement and it stops working. The following sensors are not reevaluated and are keeping their previous values.
Actually I think it has nothing to do with unit_of_measurement. It is probably some other state that is being disturbed by changing the code.
And now I take out the unit_of_measurement and the following 2 sensors are reporting again. Well watertemperature is quite stable but the pressure is changing.
It might be the combinationā¦ If the state value is non-numeric (except āunavailableā/āunknownā IIRC) it will cause an error with a sensor with a unit_of_measurement.