Template for Custom Component "Switch Humidifier"

I’m trying to make a nice template for “Switch Humidifier” custom component.
I’m almost there, I have this:
switch_humidifier
Still, my sensor for the humidifier states as “Unavailable” every time it is on. When it is off I got the “IDLE” output, everything okay in that front.
What can I be missing in the code?

This is my platform code:

humidifier: 
  - platform: switch_humidifier
    name: Humidifier Inside Garden
    switch_id: switch.tasmota_2
    sensor_id: sensor.humid_inside_garden
    type: humidifier
    start_delta: 5
    stop_delta: 5

This is my template sensor code:

      humidifier_inside_garden_state:
        friendly_name: "Humidifier Inside Garden"
        icon_template: >-
          hass:air-humidifier
        value_template: >-
          {% if (is_state('switch.tasmota_2', 'on'))  %}
            {% if (state('sensor.tasmota_2_energy_power') | int >= 15) %}
              Running
            {% else %}
              Out of water
            {% endif %}
          {% else %}
            IDLE
          {% endif %}

Thank you for your help in advance.

The template looks fine, however I do notice that your image shows an entity with the name Humidifier State when your template is Humidifier Inside Garden. Are you sure you’re looking at the correct entity?

Thank you for your answer @petro . Yes, in the image you see with the name I’m giving to it, but the entity is Humidifier Inside Garden. So the issue persists.

Here is the code I’m using for the card:

type: entities
entities:
  - entity: humidifier.humidifier_inside_garden
  - entity: sensor.humid_inside_garden
    name: Humidity Inside Garden
  - entity: sensor.humidifier_inside_garden_state
    name: Humidifier State
  - entity: switch.tasmota_2
    name: Switch Humidificador
state_color: true

where are you putting this code

In my configuration.yaml file, just after my other template sensors.

can you post the yaml around this sensor?

Here it goes with a chunk of whats before, and a chunk of whats after:

# TEMPLATE SENSORS  
  - platform: template
    sensors:
      days_passed:
        friendly_name: "Days Passed"
        icon_template: mdi:calendar
        value_template: >
          {% set start = states('input_datetime.grow_start_date') | as_datetime | as_local %}
          {{ (now() - start).days }}
      planned_veg_start_date:
        friendly_name: "Veg Start Date (planned)"
        icon_template: mdi:calendar
        value_template: >
          {% set start = states('input_datetime.grow_start_date') | as_datetime | as_local %}
          {{ (start + timedelta(days = states('input_number.total_germination_time') | int)).date() }}
      planned_flower_start_date:
        friendly_name: Flower Start Date (planned)"
        icon_template: mdi:calendar
        value_template: >
          {% set start = states('sensor.planned_veg_start_date') | as_datetime | as_local %}
          {{ (start + timedelta(weeks = states('input_number.total_vegetative_time') | int) ).date() }}
      humidifier_inside_garden_state:
        friendly_name: "Humidifier Inside Garden"
        icon_template: >-
          hass:air-humidifier
        value_template: >-
          {% if (is_state('switch.tasmota_2', 'on'))  %}
            {% if (state('sensor.tasmota_2_energy_power') | int >= 15) %}
              Running
            {% else %}
              Out of water
            {% endif %}
          {% else %}
            IDLE
          {% endif %}
# HUMIDIFIER PLATFORM  
humidifier: 
  - platform: switch_humidifier
    name: Humidifier Inside Garden
    switch_id: switch.tasmota_2
    sensor_id: sensor.humid_inside_garden
    type: humidifier
    start_delta: 5
    stop_delta: 5

are you getting errors when you reload template sensors?

It’s possible that the int without a default is causing problems.

Im getting this error:

Template variable error: ‘state’ is undefined when rendering ‘{% if (is_state(‘switch.tasmota_2’, ‘on’)) %} {% if (state(‘sensor.tasmota_2_energy_power’) | int >= 15) %} Running {% else %} Out of water {% endif %} {% else %} IDLE {% endif %}’

states( not state(

Nice call, thanks.
I changed the code a bit (float instead of int) but I face the exact same issue - the status is “unavailable”, except for IDLE.

      humidifier_inside_garden_state:
        friendly_name: "Humidifier Inside Garden"
        icon_template: >-
          hass:air-humidifier
        value_template: >-
          {% if (is_state('switch.tasmota_2', 'on'))  %}
            {% if (states('sensor.tasmota_2_energy_power') | float <= 1) %}
              Out of water
            {% else %}
              Running
            {% endif %}
          {% else %}
            IDLE
          {% endif %}

@petro, somehow I managed to put it to work. Even if I slightly changed the code again, I think the solution haves to do with a deep reboot I made to the system, or anything with my Athom Tasmota Plugs. Anyhow, it is working except when the humidifier starts, it says “Out of Water” for 1 or two minutes, and only after it will say “running” as expected.
My slution for it was to create an automatization for notifying me when the humidifier gets out of water, only after 5 minutes with the “Out of Water” state. And thats good enough for me.
Thank you very much for your help, the case is solved :slight_smile:

Here’s my final version of the code:

      humidifier_inside_garden_state:
        friendly_name: "Humidifier Inside Garden"
        icon_template: >-
          hass:air-humidifier
        value_template: >-
          {% if (is_state('switch.tasmota_2', 'on'))  %}
            {% if (states('sensor.tasmota_2_energy_power') | float <= 5) %}
              Out of Water
            {% else %}
              Running
            {% endif %}
          {% else %}
            IDLE
          {% endif %}