Input_text as input for input_select

HI,

Trying to have an input_select also read the input_text, to create further flexibility next to the preset options, Im having trouble finding the correct syntax.

Its part of a bigger package Text to Speech Notification to Leave for Appointment

but heres the input bit:

input_select:
  travel_destination:
    icon: mdi:login
    name: Travel destination
    options:
      - Select
      - Paris, France
      - Amsterdam, Netherlands
      - Eindhoven, Netherlands
      - Utrecht, Netherlands
      - Brussels, Belgium
      - Berlin, Germany
      - Bayreuth, Germany
      - zone.home
      - device.tracker.iphone
      - device.tracker.telefoon
      - sensor.select_text_travel_destination
    initial: Select
#      - '{{states.input_text.travel_destination.state}}'
#      - sensor.select_text_travel_destination

and the input_text:

input_text:
  travel_destination:
    icon: mdi:login
    name: Travel destination
    pattern: [City, Country]

Ive also made a sensor reading the input_text, that won’t work yet either…

  select_text_travel_destination:
    value_template: '{{states.input_text.travel_destination.state}}'
  select_text_travel_origin:
    value_template: '{{states.input_text.travel_origin.state}}'

Could this be done? If so,please help me how…

Thanks,
Marius

So I think what you’re trying to do is to change one of the options in input_select.travel_destination to be the value of input_text.travel_destination. Is that correct?

If so, I believe this is possible. There may be a better way, but I would think that would involve an automation that would use the input_select.set_options service. I.e., whenever input_text.travel_destination changes (and probably when HA starts), the automation would trigger and the action would be to set the options of input_select.travel_destination. But I don’t think you can change just one of the options - you have to set all the options. So the automation might look something like this:

automation:
  - alias Update Travel Destination options
    trigger:
      - platform: state
        entity_id: input_text.travel_destination
      - platform: homeassistant
        event: start
    action:
      - service: input_select.set_options
        data_template:
          entity_id: input_select.travel_destination
          options:
            - Paris, France
            ...
            - {{ states("input_text.travel_destination") }}

I haven’t actually tried this, so not sure if it will work exactly as is, but hopefully you get the idea.

Now, having said that, if it were me, I’d just leave the last option as something like “as specified in text input”, and then have whatever uses input_select.travel_destination use input_text.travel_destination when the former is set to this last option. Does that make sense?

yes indeed. i follow your thinking in the automation (which i will try soon) but don’t understand why I wouldn’t just have it like that in the definition of the input_select in the first place. If you can set the options through the automation, that must be a possibility from starters too?
Won’t a template work in the options directly?

about your suggestion making sense: i do think that might be so :wink: Not sure exactly how this would be implemented though.

Another thing about the input_text: can you help me formatting the pattern…

input_text:
  travel_destination:
    icon: mdi:login
    name: Travel destination
    pattern: [City, Country]

the options from an input select are set in 2 ways: 1) in the config 2) through a service
and you cant use templates there.
in an automation you can use templates, thats why that would work.

edit: and pattern needs to be a string, so use quotes.

2 Likes

Yep, sorry, meant to comment about that, too. So the pattern, as I understand it, needs to be a “regular expression.” If you’re not familiar with regular expressions you can specify a pattern like “lower and upper case letters or numbers, followed by a comma and an optional space, followed by more lower or upper case letters or numbers”. That would look like this:

pattern: '[a-zA-Z0-9]+, ?[a-zA-Z0-9]+'

Again, if you’re not familiar with regular expressions, just Google! :slight_smile:

BTW, I haven’t tested that regular expression on HA. There are a zillion definitions for how regular expressions work and I’m not that familiar yet with how HA implements them.

1 Like

you mean like [‘City’, ‘Country’] ?

‘[City, Country]’, or ‘[City] , [Country]’ …
btw this is what Yamllint suggests…

--- 
input_text: 
  travel_destination: 
    icon: "mdi:login"
    name: "Travel destination"
    pattern: 
      - City
      - Country
  travel_origin: 
    icon: "mdi:logout"
    name: "Travel origin"
    pattern: 
      - City
      - Country

back for some testing ;=)
thanks @ReneTode and @pnbruckner, appreciated!

no @pnbruckner did explain it more clear.

‘[a-zA-Z0-9]+, ?[a-zA-Z0-9]+’

is a pattern.
but you could also use an initial value

initial: "City, Country"

but the WHOLE pattern must be a string, not a list, so the outside characters must be " " and not []

1 Like

that seems even better, thanks.

1 Like

about the automation: If the options are set in the automation, how do i declare the input_select.travel_destination itself. Right now i have the options in that declaration, but since the automation would set them that would not be correct any longer?

The options array is required when you define it in your configuration. But since it will (almost immediately) get overwritten by the automation, you can just include enough to make it happy, e.g.:

input_select:
  travel_destination:
    options: ['not yet set']
    ...
1 Like

Just so you understand, initial and pattern do two different things. initial controls what the value will be when HA starts. pattern, on the other hand, determines whether what is typed in will be accepted.

E.g., you could just use initial as a hint for what should be typed in. Or you could just use pattern to make sure what is typed in meets some particular format (but that has to be defined in terms of a regular expression.) Or you could even do both.

Also note, if you don’t use initial, then if HA restarts it will retain whatever is in the input_text across the restart, whereas if you do use initial it will overwrite that after the restart.

Anyway, just wanted to be complete. :smile:

1 Like

ok will try.

since the original travel time sensors work with zone.home and device_tracker.iphone as origin and destinations, and the input_select doesn’t, could I add these to the automation to maybe?

which is appreciated very much, thank you indeed!

btw:

Configuration invalidCHECK CONFIG
Error loading /config/configuration.yaml: invalid key: "OrderedDict([('states("input_text.travel_destination")', None)])"
  in "/config/packages/package_travel_destination.yaml", line 138, column 0

which is about:

automation:
  - alias: Update Travel Destination options
    trigger:
      - platform: state
        entity_id: input_text.travel_destination
      - platform: homeassistant
        event: start
    action:
      - service: input_select.set_options
        data_template:
          entity_id: input_select.travel_destination
          options:
            - Select
            - Roosendaal, Netherlands
            - Amsterdam, Netherlands
            - Eindhoven, Netherlands
            - Utrecht, Netherlands
            - Antwerp, Belgium
            - Brussels, Belgium
            - Berlin, Germany
            - Bayreuth, Germany
            - Paris, France
            - {{ states("input_text.travel_destination") }}  

  - alias: Update Travel Destination options
    trigger:
      - platform: state
        entity_id: input_text.travel_origin
      - platform: homeassistant
        event: start
    action:
      - service: input_select.set_options
        data_template:
          entity_id: input_select.travel_origin
          options:
            - Select
            - Roosendaal, Netherlands
            - Amsterdam, Netherlands
            - Eindhoven, Netherlands
            - Utrecht, Netherlands
            - Antwerp, Belgium
            - Brussels, Belgium
            - Berlin, Germany
            - Bayreuth, Germany
            - Paris, France
            - {{ states("input_text.travel_origin") }}

Hmm, yes, templates can be tricky, and sometimes down right difficult. :slight_smile: Try putting single quotes around {{ ... }}. So:

  - Paris, France
  - '{{ states("input_text.travel_destination") }}'

When I tried it on the Templates page I thought the single quotes would become part of the resulting string. But I think they might actually be necessary here.

Bingo!!:

using this:

input_text:
  travel_destination:
    icon: mdi:login
    name: Travel destination
    pattern:
      - City
      - Country
    initial: "City, Country"

  travel_origin:
    icon: mdi:logout
    name: Travel origin
    pattern: "[City, Country]"
    initial: "City, Country"

input_select:
  travel_destination:
    icon: mdi:login
    name: Travel destination
    options:
      - Select
      - Not yet set
    initial: Select

and:

automation:
  - alias: Update Travel Destination options
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: input_text.travel_destination
      - platform: homeassistant
        event: start
    action:
      - service: input_select.set_options
        data_template:
          entity_id: input_select.travel_destination
          options:
            - Select
            - Roosendaal, Netherlands
            - Amsterdam, Netherlands
            - Eindhoven, Netherlands
            - Utrecht, Netherlands
            - Antwerp, Belgium
            - Brussels, Belgium
            - Berlin, Germany
            - Bayreuth, Germany
            - Paris, France
            - '{{ states("input_text.travel_destination") }}'

Now only need to find out how to get the travel-mode working… The sensors seems to always use Driving .

2 Likes

BTW, don’t claim to understand this all, by any means, but I’m not sure how what you have for pattern works. Anyway, glad you’re making progress! :smile:

using the 2 techniques you both suggested, and Yamllint checked, seems to be fine both ways. The initial state is there as an example, and ‘pattern’ checks if one fills it put correctly :wink:

full package fyi:

##########################################################################################
# Package Google Traveltime
# only feed api with calls while necessary/desired to mitigate api call restrictions
# MtHvdB 07072018
##########################################################################################

homeassistant:
  customize:
    sensor.travel_destination:
      templates:
        icon: >
          if (entities['input_select.travel_mode'].state === 'bicycling') return 'mdi:bike';
          if (entities['input_select.travel_mode'].state === 'driving') return 'mdi:car';
          if (entities['input_select.travel_mode'].state === 'walking') return 'mdi:transit-transfer';
          if (entities['input_select.travel_mode'].state === 'transit') return 'mdi:train';
          return 'mdi:bus';
    sensor.select_travel_destination:
      icon: mdi:login
    sensor.select_travel_origin:
      icon: mdi:logout
    sensor.select_travel_mode:
      icon: mdi:transit-transfer
    input_select.travel_mode:
      templates:
        icon: >
          if (state === 'bicycling') return 'mdi:bike';
          if (state === 'driving') return 'mdi:car';
          if (state === 'walking') return 'mdi:transit-transfer';
          if (state === 'transit') return 'mdi:train';
          return 'mdi:bus';
    sensor.distance_selected_destination:
      icon: mdi:map-marker-distance
      friendly_name: Distance
    sensor.duration_selected_destination:
      icon: mdi:timer
      friendly_name: Duration
    sensor.duration_in_traffic_selected_destination:
      icon: mdi:traffic-light
      friendly_name: In traffic
##########################################################################################
# Sensors
##########################################################################################

sensor:
  - platform: google_travel_time
    api_key: !secret google_travel
    mode: sensor.select_travel_mode
    departure_time: now
    name: Travel destination
    origin: sensor.select_travel_origin
    destination: sensor.select_travel_destination

  - platform: template
    sensors:
      select_travel_destination:
        value_template: '{{states.input_select.travel_destination.state}}'
      select_travel_origin:
        value_template: '{{states.input_select.travel_origin.state}}'
      select_travel_mode:
        value_template: '{{states.input_select.travel_mode.state}}'

      distance_selected_destination:
        value_template: '{{states.sensor.travel_destination.attributes.distance}}'
      duration_selected_destination:
        value_template: '{{states.sensor.travel_destination.attributes.duration}}'
      duration_in_traffic_selected_destination:
        value_template: '{{states.sensor.travel_destination.attributes.duration_in_traffic}}'

      input_travel_origin:
        value_template: '{{states.input_text.travel_origin.state}}'
      input_travel_destination:
        value_template: '{{states.input_text.travel_destination.state}}'

##########################################################################################
# Inputs
##########################################################################################

input_text:
  travel_destination:
    icon: mdi:login
    name: Travel destination
    pattern:
      - City
      - Country
    initial: "City, Country"

  travel_origin:
    icon: mdi:logout
    name: Travel origin
    pattern: "[City, Country]"
    initial: "City, Country"

input_select:
  travel_destination:
    icon: mdi:login
    name: Travel destination
    options:
      - Select
      - Not yet set
    initial: Select

#      - zone.home
#      - device.tracker.iphone
#      - device.tracker.telefoon

  travel_origin:
    icon: mdi:logout
    name: Travel origin
    options:
      - Select
      - Not yet set
    initial: Select

#      - zone.home
#      - device_tracker.iphone
#      - device_tracker.telefoon

  travel_mode:
    icon: mdi:bike
    name: Travel mode
    options:
      - driving
      - bicycling
      - walking
      - transit
    initial: bicycling

##########################################################################################
# Automation to set Input_select options with template
##########################################################################################

automation:
  - alias: Update Travel Destination options
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: input_text.travel_destination
      - platform: homeassistant
        event: start
    action:
      - service: input_select.set_options
        data_template:
          entity_id: input_select.travel_destination
          options:
            - Select
            - Roosendaal, Netherlands
            - Amsterdam, Netherlands
            - Eindhoven, Netherlands
            - Utrecht, Netherlands
            - Antwerp, Belgium
            - Brussels, Belgium
            - Berlin, Germany
            - Bayreuth, Germany
            - Paris, France
            - '{{ states("input_text.travel_destination") }}'

  - alias: Update Travel Destination options
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: input_text.travel_origin
      - platform: homeassistant
        event: start
    action:
      - service: input_select.set_options
        data_template:
          entity_id: input_select.travel_origin
          options:
            - Select
            - Roosendaal, Netherlands
            - Amsterdam, Netherlands
            - Eindhoven, Netherlands
            - Utrecht, Netherlands
            - Antwerp, Belgium
            - Brussels, Belgium
            - Berlin, Germany
            - Bayreuth, Germany
            - Paris, France
            - '{{ states("input_text.travel_origin") }}'

##########################################################################################
# Frontend Group
##########################################################################################

group:
  travel_destination:
    name: Travel destination
    icon: mdi:transit-transfer
    entities:
      - input_select.travel_origin
      - input_text.travel_origin
      - input_select.travel_destination
      - input_text.travel_destination
      - input_select.travel_mode
      - sensor.travel_destination
      - sensor.distance_selected_destination
      - sensor.duration_selected_destination
      - sensor.duration_in_traffic_selected_destination

Yes, they’re syntactically correct, but I don’t think they’re semantically correct. Are you sure you’re not getting any warnings or errors in the HA log regarding these?

Which do you want me to check specifically? Havent seen anything popup yet, but maybe the logs reveal deeper issues. Both seem to work as expected.

As long as it’s working for you I guess that’s the main thing. I just don’t understand how it is. Besides the pattern for the input_text items, I also see other things in your configuration that I don’t understand. E.g., under customize, you have “templates” under sensor.travel_destination, and the actual templates under “icon” do not appear to be jinja. So either the documentation is woefully inadequate, or I’m completely missing something.

But if you’re happy, I’m happy! :slight_smile: