So now I am at the same situation as 24hours ago.
Works without unit_of_measurement. But not with.
But then why does it just stop working the next day.
The serial data is more or less the same.
And waterdepth I have kept disconnected.
That is what I thought yesterday.
But what I cannot explain is why does 24 hours later change its behavior.
I even restarted the hardware, Rpi 4. But no difference.
I will put the null condition check again and see if that improves it.
Script I used for testing
alias: zTest Repeat For testing
sequence:
- repeat:
for_each:
- PRESS,,f,61,M,,F*3F
- WEGT,11,1,F,5,4F
- PRESS,,f,62,M,,F*3F
- WEGT,12,1,F,5,4F
- WEGT,13,1,F,5,4F
- PRESS,,f,,M,,F*3F
- WEGT,14,1,F,5,4F
- PRESS,,f,63,M,,F*3F
- WEGT,15,1,F,5,4F
- PRESS,,f,64,M,,F*3F
- WEGT,16,1,F,5,4F
- WEGT,17,1,F,5,4F
- WEGT,18,1,F,5,4F
- PRESS,,f,65,M,,F*3F
- WEGT,19,1,F,5,4F
- PRESS,,f,66,M,,F*3F
- PRESS,,f,67,M,,F*3F
- WEGT,20,1,F,5,4F
- WEGT,21,1,F,5,4F
- PRESS,,f,68,M,,F*3F
- WEGT,22,1,F,5,4F
- PRESS,,f,69,M,,F*3F
- WEGT,23,1,F,5,4F
- WEGT,24,1,F,5,4F
- PRESS,,f,,M,,F*3F
- WEGT,25,1,F,5,4F
- PRESS,,f,70,M,,F*3F
- WEGT,26,1,F,5,4F
- PRESS,,f,71,M,,F*3F
- WEGT,27,1,F,5,4F
- WEGT,28,1,F,5,4F
- WEGT,29,1,F,5,4F
- PRESS,,f,72,M,,F*3F
- WEGT,30,1,F,5,4F
- PRESS,,f,73,M,,F*3F
- PRESS,,f,74,M,,F*3F
- WEGT,31,1,F,5,4F
- WEGT,32,1,F,5,4F
sequence:
- service: input_text.set_value
data:
value: "{{ repeat.item }}"
target:
entity_id:
- input_text.ztest_text_test
- delay: 30
mode: single
Is that a typo (I think the forum software will auto-corrects double commas)? Previously you posted it as follows: "PRESS,f,,M,F*3F”
Yes must be some cut past error or typo I made. 65 is replaced with null.
PRESS,f,65,M,F3F
PRESS,f,M,F3F
The 3rd element on the 2nd line is null.
I added quotes just to show it is a string. The actual serial data does not include quotes.
I have figured out what the problem is.
If I define a unit_of_measurement: “m” then the template sensor MUST return a numerical value.
If it tries to return none or “” or ‘’ it will error out and not return anything.
So what can I set it If want to return a null value and I have a unit_of_measurement: “m” defined??
It works if I return a 0 but that is not correct in my case.
The short answer is you can’t…
What is the purpose of returning a null value?
If the goal is to determine that the sensor isn’t updating, you can and should set up an availability
. You will need to figure out how you want to differentiate between when the template needs to report null/unavailable and when it should use the previous state. You will likely need to define attributes or other standalone sensors.
I figured it out! I used availability. And also got the timestamp working that we discussed a while back.
here is the code.
- name: waterdepth
unique_id: water_depth_id
unit_of_measurement: "m"
state: >-
{% set new_data_condition = (d | count > 3 and d[0]|length >= 6 and d[0][0] == 'X' and d[0][3:6] == 'WBT') %}
{{ d[3].split('*')[0] if new_data_condition else this.state }}
availability: >-
{% set new_data_condition = (d | count > 3 and d[0]|length >= 6 and d[0][0] == 'X' and d[0][3:6] == 'WBT') %}
{% set time_difference_seconds = as_timestamp(now()) - as_timestamp(this.attributes.updated_timestamp | default(now())) %}
{{ not (new_data_condition and d[3] == '') and (time_difference_seconds <= 60) }}
attributes:
updated_timestamp: >-
{% set new_data_condition = (d | count > 3 and d[0]|length >= 6 and d[0][0] == 'X' and d[0][3:6] == 'WBT') %}
{{ now() if new_data_condition else this.attributes.updated_timestamp | default(now()) }}
It would be nice not to have to repeat the new_data_condition logic 3 times. But I could not see how to set it to have scope for the whole sensor. Seems to only have scope per section.
Any ideas?
Since I now have 500 sensors I cant really declare a 500 different variables at the trigger level.
There has been a Feature Request thread for variables in template sensors since before there were this
or trigger
variables, 2 variants of trigger variables, or custom template macros…
You may be able to reduce the size by using more specific triggers instead of relying on a single, general trigger that you then have to parse over and over.