I would expect it to spit out something like 10 Days or 3 Days or 4 Hours but it doesn’t not like my config. Getting the error below, what am I doing wrong?
Invalid config for [sensor.mqtt]: invalid template (TemplateSyntaxError: unexpected char '&' at 168) for dictionary value @ data['value_template']. Got '{% if value_json.value > 864000 %}\n 10 Days\n{% elif value_json.value < 86400 %}\n {{ value_json.value / 3600|float |round(0) }} Hours\n{% elif value_json.value > 86400 && value_json.value > 864000 %}\n {{ value.json.value / 86400|float|round(1) }} Days\n{% else %}\n Error Calculating TimeToGo\n{% endif %}\n'. (See ?, line ?).
& is not valid syntax. This is jinja. The syntax is very close to python. && should be and || should be or. Pipes in jinja are filters and it appears as if you’re using them properly. Also, not sure why you’re even checking for 10 days, you can just use your calculation.
EDIT: Floats aren’t needed either and you want parenthesis around the division. Filters aren’t applied to the value directly in front of them.
- platform: mqtt
unique_id: "battery_remaining"
name: "Battery Remaining"
icon: mdi:timer-sand
scan_interval: 5
state_topic: "N/b827eb57c4de/system/0/Dc/Battery/TimeToGo"
value_template: >
{% set seconds = value_json.value | round(0) | int %}
{% set days = (seconds / 86400) | round(0, 'floor') | int %}
{% set hours = ((seconds - days * 86400) / 3600) | round(0, 'floor') | int %}
{% set minutes = ((seconds - hours * 3600) / 60) | round(0, 'floor') | int %}
{% if days > 0 %}
{{days}} Days
{% elif hours > 0 %}
{{hours}} Hours
{% elif minutes > 0 %}
{{minutes}} Minutes
{% else %}
∞
{% endif %}
I added the last else as infiniti because when the system is charging we get back a value of null from mqtt and we’re not necessarily pulling from the battery and cannot calculate time to go.