Thanks for the suggestion. It looks promising. I can’t get the broker to publish an empty topic to test it, but I’ll see tomorrow when it again goes empty if my logfile explodes again or not
Copy-paste the following template into the Template Editor and experiment with it to see how it behaves with different values.
Test 1
{% set value_json = none -%}
{% if value_json.value is not defined or value_json.value is none -%}
∞
{% else -%}
{{ value_json.value }}
{% endif -%}
Test 2
{% set value_json = {'value': none} -%}
{% if value_json.value is not defined or value_json.value is none -%}
∞
{% else -%}
{{ value_json.value }}
{% endif -%}
Test 3
{% set value_json = {'value': 123} -%}
{% if value_json.value is not defined or value_json.value is none -%}
∞
{% else -%}
{{ value_json.value }}
{% endif -%}
I tried some similar variants earlier to see if I could get the errors.
Now also with your three. The only way I can get an error is if I completly empty the value field, like this:
{% set value_json = -%}
Receiving the
TemplateSyntaxError: Expected an expression, got ‘end of statement block’
If i put ’ ’ single marks with empty content, it goes as wanted with no error. Can it be that the topic is completly removed from the sender side when null, and that throws the error? Allthough, I see the topic in mqtt-explorer when it goes blank.
Exactly. It looks like there is not valid json published when there is no value for that topic.
It’s equal to these. This is not in use, but still published. Looks like mye seconds that is only published when there is no production. This is a off-grid solar from Victron.
It shows as null in mqtt-explorer. But I suspect that is a fix applied there?
The value null seen in that screenshot corresponds to none in a Jinja2 template.
The second test example handles none.
Test 2
{% set value_json = {'value': none} -%}
{% if value_json.value is not defined or value_json.value is none -%}
∞
{% else -%}
{{ value_json.value }}
{% endif -%}
time_since(datetime, precision) converts a datetime object into its human-readable time string. The time string can be in seconds, minutes, hours, days, months, and years. precision takes an integer (full number) and indicates the number of units returned. The last unit is rounded. For example: precision = 1 could return “2 years” while precision = 2 could return “1 year 11 months”. This function can also be used as a filter. If the datetime is in the future, returns 0 seconds. A precision of 0 returns all available units, default is 1.
Thanks for your help, this solved it. No error today! enjoy the coffee
Now I see that I have to exclude it from recorder, it sends very varying and frequent updates when the sun intensity change. Wish that the mqtt sensor could have the same as we have in esphome, update_interval.