Input_text as input for input_select

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:

I hear your cry…
Tbh, templating isn’t the best documented bit in HA, especially not what/which to use when, let alone how…Different types of entities, device_trackers, sensors, being template or not, and needing them to be in browser or server side.
these could have been done differently, I know, its just I had this working syntax at hand and copied it…

When in doubt I always go back to @andrey’s primer on this: home-assistant-custom-ui/docs/templates.md at master · andrey-git/home-assistant-custom-ui · GitHub, and, as a nice and valuable addition to that, use the ‘Tiles’ templating by @eddi89 : https://github.com/c727/home-assistant-tiles

trial, hit and error come hand in hand though, as shown in the input_text/select in my travel package…
cheers,
Marius

1 Like

i think this page explains that pretty good

but thats the serverside part. which was all we had untill the custom UI came along.
i didnt even know there were templating possibilities on client side. :wink:

i guess you would normally first use the server side possibilities. and then if you run out of options you get the clientside possibilities.

the difference is something like:

variabele = some_sensor_state * 2
print (variable)

or

print (some_sensor_state * 2)

in the first case you can reuse the variable in the second case not.
and the first case depends on the speed from the server and the second on the speed from the device showing it.

1 Like

shouldn’t have left that out, sorry

As far as I understand it, it is advisable to try to use client side templating as much as possible, since it relieves the processor.

exactly, and since mine is a Pi3, i am trying to be as lightweight as possible on it :wink:

did you ever look at the load on the RPI?
HA does nothing to it :wink:
with just HA i had not even a cpu usage from 8%

the more you use clientside, the longer it takes to load your pages.
i would go with the serverside, but thats a choice.

I didn’t even know there was such a thing as custom UI! :slight_smile:

Thanks @Mariusthvdb & @ReneTode!

1 Like

only Hassio on the Pi.

will have to rebuild a lot, and then some (in the packages…)

23

anyways,
thanks for thinking along!

1 Like

You should try to use server-side whenever possible.

Server-side templates are jinja2
Client-side is JavaScript.