What is incorrect with this value_template logic?

I have a sensor:

- platform: mqtt
  unique_id: "battery_time_to_go"
  name: "Time To Go"
  scan_interval: 5
  state_topic: "N/abc123/system/0/Dc/Battery/TimeToGo"
  value_template: >
    {% if value_json.value > 864000 %}
      10 Days
    {% elif value_json.value < 86400 %}
      {{ value_json.value / 3600 | float | round(0) }} Hours
    {% elif value_json.value > 86400 && value_json.value > 864000 %}
      {{ value.json.value / 86400 | float | round(1) }} Days
    {% else %}
      Error Calculating TimeToGo
    {% endif %}

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 ?).

I’m no expert, but I think you need to replace the && with just “and”

You need ‘and’ instead of && in the 5th line of the template.

thank you. what language is this using under the hood?

& 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.

    {% if value_json.value > 864000 %}
      10 Days
    {% elif value_json.value > 86400 and value_json.value > 864000 %}
      {{ (value.json.value / 86400) | round(1) }} Days
    {% elif value_json.value < 86400 %}
      {{ (value_json.value / 3600) | round(0) }} Hours
    {% else %}
      Error Calculating TimeToGo
    {% endif %}

lastly, you don’t even need the and. Just use simplified syntax

    {% if value_json.value > 864000 %}
      10 Days
    {% elif 86400 <= value_json.value <= 864000 %}
      {{ (value.json.value / 86400) | round(1) }} Days
    {% elif value_json.value < 86400 %}
      {{ (value_json.value / 3600) | round(0) }} Hours
    {% else %}
      Error Calculating TimeToGo
    {% endif %}

technically the else isn’t needed either unless you want to avoid negative results or something

    {% if value_json.value > 864000 %}
      10 Days
    {% elif 86400 <= value_json.value <= 864000 %}
      {{ (value.json.value / 86400) | round(1) }} Days
    {% else %}
      {{ (value_json.value / 3600) | round(0) }} Hours
    {% endif %}

With:

- platform: mqtt
  unique_id: "battery_time_to_go"
  name: "Time To Go"
  scan_interval: 5
  state_topic: "N/b827eb57c4de/system/0/Dc/Battery/TimeToGo"
  value_template: >
    {% if value_json.value > 864000 %}
      10 Days
    {% elif 86400 <= value_json.value <= 864000 %}
      {{ (value.json.value / 86400) | round(1) }} Days
    {% else %}
      {{ (value_json.value / 3600) | round(0) }} Hours
    {% endif %}

I’m getting back Unknown

When I check the value of that mqtt topic it’s returning a valid float:

Why is this failing?

I re-wrote this as follows:

- 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.