Automation: Failing on input_select.select_option template

Hi.

One of my automation is no longer working.
I don’t understand why, and I hope some of you can help me?

I’m currently running version 0.116.4 and I’m not sure when my automation stopped working.

So what is my automation:

To control my lights I use a input_select component.
When the input changes, the light changes brightness and temperature according to a light_profiles.csv.
The input_select component will change based on an automation, or I can change it manually.

From my Lovelace:

I’ve defined the input_select component like this:

input_select:
  current_light_profile:
    name: Current light Profile
    options:
      - bright
      - reading
      - concentrate
      - energize
      - relax
      - dimmed
      - movie
      - nightlight

I’ve updated my automation trigger accordingly to ver 0.115

It triggers at “two different settings”.
One: At some specified input_time
Two: When changing these input_time

      - platform: time
        at:
          - input_datetime.bright_time_weekend
          - input_datetime.reading_time_weekend
          - input_datetime.relax_time_weekend
          - input_datetime.dimmed_time_weekend
          - input_datetime.bright_time
          - input_datetime.reading_time
          - input_datetime.relax_time
          - input_datetime.dimmed_time

      - platform: state
        entity_id: 
          - input_datetime.bright_time_weekend
          - input_datetime.reading_time_weekend
          - input_datetime.relax_time_weekend
          - input_datetime.dimmed_time_weekend
          - input_datetime.bright_time
          - input_datetime.reading_time
          - input_datetime.relax_time
          - input_datetime.dimmed_time

So to the action part of my automation:

I’ve played with the template part here. But I’m not comfortable with this!
First I determine if it is a normal day, or weekend/holiday.
Then I find out what “timeslot” I’m in and return the correct input_select value to set (or option).

    action:
        
      - service: input_select.select_option
        data:
          entity_id: input_select.current_light_profile
          option: >
            {% set n = now() %}
            {% set n = (n.hour*60+n.minute)*60+n.second %}

            {% if is_state('binary_sensor.workday_light', 'off') %}

              {% set time_bright = state_attr('input_datetime.bright_time_weekend', 'timestamp') %}
              {% set time_reading = state_attr('input_datetime.reading_time_weekend', 'timestamp') %}
              {% set time_relax = state_attr('input_datetime.relax_time_weekend', 'timestamp') %}
              {% set time_dimmed = state_attr('input_datetime.dimmed_time_weekend', 'timestamp') %}

            {% else %}

              {% set time_bright = state_attr('input_datetime.bright_time', 'timestamp') %}
              {% set time_reading = state_attr('input_datetime.reading_time', 'timestamp') %}
              {% set time_relax = state_attr('input_datetime.relax_time', 'timestamp') %}
              {% set time_dimmed = state_attr('input_datetime.dimmed_time', 'timestamp') %}

            {% endif %}

            {% if time_bright < time_reading < time_relax < time_dimmed %}

              {% if time_bright < n and n < time_reading %}
              bright
              {% elif time_reading < n and n < time_relax %}
              reading
              {% elif time_relax < n and n < time_dimmed %}
              relax
              {% elif time_dimmed < n or n < time_bright %}
              dimmed
              {% endif %}

            {% elif time_dimmed < time_bright < time_reading < time_relax %} 

              {% if time_dimmed < n and n < time_bright %}
              dimmed   
              {% elif time_bright < n and n < time_reading %}
              bright
              {% elif time_reading < n and n < time_relax %}
              reading
              {% elif time_relax < n or n < time_dimmed %}
              relax
              {% endif %}

            {% else %}

            relax

            {% endif %}

When I change the time in Lovelace and thereby trigger the automation (platform: state), everything works fine. But if the automation is triggered by the time part (platform: time), nothing happens. And I get this in the log:

Logger: homeassistant.components.input_select
Source: components/input_select/__init__.py:256
Integration: Input select ([documentation](https://www.home-assistant.io/integrations/input_select), [issues](https://github.com/home-assistant/home-assistant/issues?q=is%3Aissue+is%3Aopen+label%3A%22integration%3A+input_select%22))
First occurred: 12:12:00 PM (3 occurrences)
Last logged: 2:50:00 PM

Invalid option: (possible options: bright, reading, concentrate, energize, relax, dimmed, movie, nightlight)

So I guess my template is no longer 100% valid? But it is strange that what type of trigger sets of the automation should affect the action part of the automation?

I’m not to bright with coding, so when I read this: breaking change in the template integration, I don’t really understand anything. So could this be the problem?

My complete code if it helps:

binary_sensor:
  - platform: template
    sensors:
      workday_light:
        friendly_name: "Worday tomorrow ( Updates after 12:00 )"
        value_template: >-
          {{ (states.sensor.time.state > '12:00' | timestamp_custom('%H:%M', False)
          and is_state('binary_sensor.tomorrow', 'on'))
          or 
          (states.sensor.time.state < '12:00' | timestamp_custom('%H:%M', False)
          and is_state('binary_sensor.today', 'on')) }}
        icon_template: mdi:briefcase

input_datetime:
# Work days
  bright_time:
    name: Bright
    has_time: true
    has_date: false
    icon: mdi:weather-sunny
  reading_time:
    name: Reading
    has_time: true
    has_date: false
    icon: mdi:brightness-5
  relax_time:
    name: Relax
    has_time: true
    has_date: false
    icon: mdi:brightness-6
  dimmed_time:
    name: Dimmed
    has_time: true
    has_date: false
    icon: mdi:brightness-4
# Hollidays days
  bright_time_weekend:
    name: Bright
    has_time: true
    has_date: false
    icon: mdi:weather-sunny
  reading_time_weekend:
    name: Reading
    has_time: true
    has_date: false
    icon: mdi:brightness-5
  relax_time_weekend:
    name: Relaxed
    has_time: true
    has_date: false
    icon: mdi:brightness-6
  dimmed_time_weekend:
    name: Dimmed
    has_time: true
    has_date: false
    icon: mdi:brightness-4

input_select:
  current_light_profile:
    name: Current light Profile
    options:
      - bright
      - reading
      - concentrate
      - energize
      - relax
      - dimmed
      - movie
      - nightlight

automation:

  - alias: Light presets
    id: light_presets
    trigger:
      - platform: state
        entity_id: input_boolean.sleep_mode_active
        to: 'off'

      - platform: state
        entity_id: input_boolean.light_presets
        to: 'on'

      - platform: time
        at:
          - input_datetime.bright_time_weekend
          - input_datetime.reading_time_weekend
          - input_datetime.relax_time_weekend
          - input_datetime.dimmed_time_weekend
          - input_datetime.bright_time
          - input_datetime.reading_time
          - input_datetime.relax_time
          - input_datetime.dimmed_time

      - platform: state
        entity_id: 
          - input_datetime.bright_time_weekend
          - input_datetime.reading_time_weekend
          - input_datetime.relax_time_weekend
          - input_datetime.dimmed_time_weekend
          - input_datetime.bright_time
          - input_datetime.reading_time
          - input_datetime.relax_time
          - input_datetime.dimmed_time



    condition:

      # Override if sleep mode is active
      - condition: state
        entity_id: input_boolean.sleep_mode_active
        state: 'off'

      #  Enable This Automations
      - condition: state
        entity_id: input_boolean.light_presets
        state: 'on'

    action:
      - service: input_select.select_option
        data:
          entity_id: input_select.current_light_profile
          option: >
            {% set n = now() %}
            {% set n = (n.hour*60+n.minute)*60+n.second %}
            {% if is_state('binary_sensor.workday_light', 'off') %}
              {% set time_bright = state_attr('input_datetime.bright_time_weekend', 'timestamp') %}
              {% set time_reading = state_attr('input_datetime.reading_time_weekend', 'timestamp') %}
              {% set time_relax = state_attr('input_datetime.relax_time_weekend', 'timestamp') %}
              {% set time_dimmed = state_attr('input_datetime.dimmed_time_weekend', 'timestamp') %}
            {% else %}
              {% set time_bright = state_attr('input_datetime.bright_time', 'timestamp') %}
              {% set time_reading = state_attr('input_datetime.reading_time', 'timestamp') %}
              {% set time_relax = state_attr('input_datetime.relax_time', 'timestamp') %}
              {% set time_dimmed = state_attr('input_datetime.dimmed_time', 'timestamp') %}
            {% endif %}
            {% if time_bright < time_reading < time_relax < time_dimmed %}
              {% if time_bright < n and n < time_reading %}
              bright
              {% elif time_reading < n and n < time_relax %}
              reading
              {% elif time_relax < n and n < time_dimmed %}
              relax
              {% elif time_dimmed < n or n < time_bright %}
              dimmed
              {% endif %}
            {% elif time_dimmed < time_bright < time_reading < time_relax %} 
              {% if time_dimmed < n and n < time_bright %}
              dimmed   
              {% elif time_bright < n and n < time_reading %}
              bright
              {% elif time_reading < n and n < time_relax %}
              reading
              {% elif time_relax < n or n < time_dimmed %}
              relax
              {% endif %}
            {% else %}
            relax
            {% endif %}

A small update:

I’ve moved my action part of the automation over to a script. The script runs without any problem (no error in the log) when I start it manually.
So the action part of my automation now is only one service to start the script.

But I still get the same error as before. If the automation is triggered by a state change, script is triggered and everything is fine:

  - alias: Light presets
    id: light_presets
    trigger:

      - platform: state
        entity_id: 
          - input_datetime.bright_time_weekend
          - input_datetime.reading_time_weekend
          - input_datetime.relax_time_weekend
          - input_datetime.dimmed_time_weekend
          - input_datetime.bright_time
          - input_datetime.reading_time
          - input_datetime.relax_time
          - input_datetime.dimmed_time

But when automation is triggered by the time in “input date time”:


  - alias: Light presets
    id: light_presets
    trigger:

# Use input_datetime helpers in automation triggers v0.115
      - platform: time
        at:
          - input_datetime.bright_time_weekend
          - input_datetime.reading_time_weekend
          - input_datetime.relax_time_weekend
          - input_datetime.dimmed_time_weekend
          - input_datetime.bright_time
          - input_datetime.reading_time
          - input_datetime.relax_time
          - input_datetime.dimmed_time

I get the same error as before:

WARNING (MainThread) [homeassistant.components.input_select] Invalid option:  (possible options: bright, reading, concentrate, energize, relax, dimmed, movie, nightlight)

Sorry to bump my own thread, but I’m struggling a bit here :slight_smile:

Look at this if-elif chain:

              {% if time_bright < n and n < time_reading %}
              bright
              {% elif time_reading < n and n < time_relax %}
              reading
              {% elif time_relax < n and n < time_dimmed %}
              relax
              {% elif time_dimmed < n or n < time_bright %}
              dimmed
              {% endif %}

What happens if the value of n doesn’t match any of the 4 tests?

If it fails to match the fourth test, it simply exits the if-elif chain without producing a result. In other words, the action calls select_option without specifying a valid option (and you get an error message telling you “Invalid option”).

You can confirm this theory by ending the chain like this:

              dimmed
              {% else %}
              concentrate
              {% endif %}

That will put your lights in daylight mode whenever n fails to match any of the four tests. Then you can double check those time boundaries and determine why there’s no match for any of them (when triggered by the Time Trigger).

1 Like

Yes this is the problem

If it fails to match the fourth test, it simply exits the if-elif chain without producing a result

It seems that when the automation is executed by the time trigger, my " n = now() " is now equal to the input value e.g. “time_bright”
So this is the old code:

if time_bright < n and n < time_reading

I’ve change the code to this (“lower or equal” added to all if chains ):

if time_bright <= n and n < time_reading

And now it’s working again.
Thanks for the help. I was really stuck on this, and I don’t think I would have gotten to the solution all by myself. :+1:

You’re welcome.

Given that a week has passed since you first posted your question, and no one else commented, it was clear to me that you required assistance to solve it. After you were provided with a solution, you confirmed it was the source of the error message and then, surprisingly, marked your own post as the “Solution”.

You should know that it is the custom of this community forum to assign “Solution” to the post that offers, or leads to, the solution, not to a post that simply confirms, or duplicates, the ideas presented in an earlier post.

Didn’t know that :flushed:. I only wanted the thread marked as solved and show the solution. Off course you should be credited for showing me the way. I’ll fix this.

1 Like

No problem. Yes, there’s the issue of giving credit where credit is due but it’s also for the benefit of other users. They may have a similar problem and jump to the Solution post where they may only find an abbreviated version of the actual solution (or simply a copy of someone else’s post).

Anyway, don’t hesitate to post questions, I’ll help you whenever I can.

I am interested in the same automation as you did for your lighting. After going through the above codes, I still don't see how you use your light profiles stored in light_profiles.csv. Can you enlighten me with that TIA.

I’ll happily show you more of my code and how I use the light profile.csv.

Unfortunately I’m currently not close to my home assistant system, so it will have to wait a week or two.

So here is my code.

I have basically three scripts for every room.
That’s light on, light update and light off.

It looks like this:

########################################################
######             Light Hallway On               ######
########################################################
light_hallway_on:
 
  alias: Hallway lights On
  icon: mdi:lightbulb-on
  mode: single
  sequence:

    # Hallway ligths on
    - service: light.turn_on
      entity_id: light.hallway
      data_template:
          profile: '{{ states.input_select.current_light_profile.state}}'
          transition: '{{ transition }}'

########################################################
######           Light Hallway Update             ######
########################################################
light_hallway_update:

  alias: Hallway lights Update
  icon: mdi:lightbulb
  mode: single
  sequence:

    # Activate when light is on
    - condition: state
      entity_id: light.hallway
      state: 'on'

    # Hallway ligths on
    - service: light.turn_on
      entity_id: light.hallway
      data_template:
          profile: '{{ states.input_select.current_light_profile.state}}'
          transition: '{{ transition }}'

########################################################
######            Light Hallway Off               ######
########################################################
light_hallway_off:

  alias: Hallway lights Off
  icon: mdi:lightbulb-off
  mode: single
  sequence:

    # Activate when light is off
    - condition: state
      entity_id: light.hallway
      state: 'on'

    # Hallway ligths on
    - service: light.turn_off
      entity_id: light.hallway
      data_template:
          transition: '{{ transition }}'

Then I can call for this script when I need to turn on, update or turn off the lights.

This script will use the “input_select.current_light_profile” value for the “profile”. So you need to have the same options there as your profiles in your light_profiles.csv.

So if the current light profile variable changes, I’ll run my light update script inn all rooms. If the lights are off, nothing happens. If the lights are on, they’ll slowly update to the new value.

So you also need to send over the transition time you want to use when calling the script.

I do it like this:

      - service: script.turn_on
        entity_id: script.light_hallway_update
        data:
          variables:
          # Passing "transition" data to script
            transition: 50

I hope this helps.

1 Like