I need help with formatting template sensor( I think)

I’m trying to create a sensor that will tell if my 3d printer is safe to shut down or not…

in the template editor I have

  {% if is_state('switch.3d_printer', 'off') %}
    Off
  {% elif is_state('sensor.octoprint_current_state', 'Printing') %}
    Not Safe 
  {% elif states('sensor.octoprint_actual_tool0_temp')|int <= 29 %}
    Safe
  {% else %} 
    Not Safe
  {% endif %}

and it works… how ever when I try and add it to a template sensor it returns “unavailable” as soon as the octoprint_curent_state changes from “printing” to anything else.

in a seperate templates.yaml file I have


sensor:
  - name: Ender Safe Shutdown
    state: >
    {% if is_state('switch.3d_printer', 'off') %}
      Off
    {% elif is_state('sensor.octoprint_current_state', 'Printing') %}
      Not Safe 
    {% elif states('sensor.octoprint_actual_tool0_temp')|int <= 29 %}
      Safe
    {% else %} 
      Not Safe
    {% endif %}

can anyone please help me with where I am going wrong?

thank you for your help and time

Robin

Indent the entire template by an additional two spaces then execute Check Configuration to confirm there are no syntax errors.

thank you taras,

this is what i have now ( i hope i understood you properly)

sensor:
  - name: Ender Safe Shutdown
    state: >
      {% if is_state('switch.3d_printer', 'off') %}
        Off
      {% elif is_state('sensor.octoprint_current_state', 'Printing') %}
        Not Safe 
      {% elif states('sensor.octoprint_actual_tool0_temp')|int <= 29 %}
        Safe
      {% else %} 
        Not Safe
      {% endif %}

I checked the configuration and it came up ‘Configuration good’ in green, however there is no change,
if switch.3d_printer is off the sensor shows ‘off’
if sensor.octoprint_current_state is ‘Printing’ it shows ‘Not Safe’
but as soon as sensor.octoprint_current_state changes the sensor shows ‘unavailable’

hope that helps.

Have you checked the Log for errors?

For example, if the int filter encountered a value it could not convert (such as unknown or unavailable) it would fail (and log an error ) because the int filter lacks a default value.

In the following example, the int filter contains a default value of 100 (but you should set it to whatever you think is best for your application):

{% elif states('sensor.octoprint_actual_tool0_temp') | int(100) <= 29 %}