Dropdown Helper State Machine

Hi all

I am looking for the best way to set a dropdown helper to a particular state based on a sensor’s value Multiple automations seems messy to me and it feels like there must be a better way.

Essentially, I have a helper called input_select.cabin_fireplace_state which I am going to use as a state machine to store the state of my fireplace (Off/On/Low Heat/High Heat). This will be based on the wattage recorded by another sensor (A smart plug).

>2w < 800w - On
>800w < 1800w - Low Heat
> 1800w - High Heat

I will then have templated switches to control the different modes and lock/unlock options depending on what is possible from a particular state. For example, the fireplace must transition from Off to On before either heat option is available.

Hmm, actually. Maybe a template sensor may be better than using a helper

Think im getting close. My syntax is slightly off though. Wondering if anyone could help:

  - platform: template
    sensors:
      cabin_fireplace_state:
        friendly_name: "Cabin Fireplace State"
        value_template: >-
        {% if states('sensor.cabin_fireplace_energy_power') < 2 %}
          {{ "Off" }}
        {% elif states('sensor.cabin_fireplace_energy_power') > 2 and states('sensor.cabin_fireplace_energy_power') < 800 %}
          {{ "On" }}
        {% elif states('sensor.cabin_fireplace_energy_power') > 800 and states('sensor.cabin_fireplace_energy_power') < 1800 %}
          {{ "Low Heat") }}
        {% elif states('sensor.cabin_fireplace_energy_power') > 1800 %}
          {{ "High Heat" }}
        {% endif %}

  - platform: template
    sensors:
      cabin_fireplace_state:
        friendly_name: "Cabin Fireplace State"
        value_template: >-
          {% set p = states('sensor.cabin_fireplace_energy_power') | float(0) %}
          {% if p < 2 %} Off
          {% elif 2 < p < 800 %} On
          {% elif 800 < p < 1800 %} Low Heat
          {% else %} High Heat
          {% endif %}

In your example, it overlooks to convert the sensor’s state value, which is a string, into a number. Therefore every test that compares a string to a number will fail.

Thanks very much, that was what I was missing. (Also thanks for tidying my template on to less lines.)

1 Like

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions. For more information, refer to guideline 21 in the FAQ.

1 Like