When to use input_select vs. template & variables

Some years back, I created the below Automation. Ultimately what I want to know… Is there a more effective, or better HA way to set this up? Is using input_select ‘appropriate’ in this case, or should I be using template & variables to accomplish this?

The Q really popped up when I wanted to generate and see on my dashboard, the status of my computer, ie. locked or unlocked. Adding this (see attached) simply creates a dropdown box. I understand this is appropriate for the type of Helper it is, making me wonder, should I not be using input_select in this manner?

alias: Presence - Home
description: ""
trigger:
  - entity_id: group.people_family
    platform: state
    to: home
condition: []
action:
  - data:
      entity_id: input_select.home
      option: Home
    service: input_select.select_option
  - data: {}
    entity_id: automation.random_away_lights
    service: automation.turn_off
  - data: {}
    entity_id: automation.turn_off_all_lights
    service: automation.turn_off
  - service: light.turn_on
    data:
      brightness_pct: 60
    target:
      device_id: 61ce75c8c194fc5bf517be3cc7e0a6aa
  - service: script.living_room_set_scene
    data: {}
mode: single

The time to use any Input helper is when you want/need to have user input directly alter the state of the entity. If the state of the entity is really being decided by non-user events or states of other entities and you do not require the ability to manually override it*, then a template sensor seems more appropriate.

*With trigger-based template sensors manual override is actually still possible, just a little more complicated to set up.

1 Like

The reason I went this direction initially was then I could use result in other automations, scripts, etc. Can I do the same with templates, or do I need to create this as a Condition in every one?

I would like to use variables of sort, ie. set ‘home’ as a conditional variable in a scene… If home then execute, otherwise ignore.

Can you clarify what you mean by “conditional variable” in a scene? I am unaware of any type of global variable or conditional logic available in scenes.

Since you have not posted an example, I’m assuming in scripts and automations you are currently using something like:

  - condition: state
    entity_id: input_select.home
    state: "home"

A template sensor would be used the same way

  - condition: state
    entity_id: sensor.home
    state: "home"

However, if you have person entities defined for all your people, you don’t actually need any supplementary entities (including the group) for triggers or conditions… you can just use the state of zone.home since the state of a zone is the number of person entities in that zone based on their assigned device trackers.

  - condition: numeric_state
    entity_id: zone.home
    above: 0

Wow, thank you. I never really have understood some of this. With regards to the variable in a scene, I was simply referring to how you presented the state/condition. Again, thanks!

I’m coming back around to this question… What would a template sensor look like if I wanted the ability to manually set it?

You use a trigger-based template sensor. As a basic example, the following would create a sensor that tracks whether anyone is home based on the zone integration, but allows manual override by raising a custom event.

template:
  - trigger:
      - platform: state
        entity_id: zone.home
        not_to:
          - unknown
          - unavailable
      - platform: event
        event_type: custom_home_occupancy
    binary_sensor:
      - name: "House Occupied"
        state: >
          {% if trigger.platform == 'event' %}
            {{ trigger.event.data.state }}
          {% else %}
            {{ trigger.to_state.state | int | bool }}
          {% endif %}

Okay. If I then wanted to use it as a Condition in another Automation, what would that be?

- condition: state
    entity_id: binary_sensor.? 
    state: "?"

The entity_id is set by the sensor’s name, so the one in the example would be binary_sensor.house_occupied; and a binary sensor’s state is on or off.

Thanks again!

So now I’m trying to set this up via the Template Editor and can’t get it right. Maybe I don’t need to do this?

{% set trigger =
  {'platform':'state',
   'entity_id':'zone.home',
   'not_to':
    - unknown
    - unavailable
  }
  {'platform':'event',
   'entity_type':'custom_home_occupancy',
  } %}

    binary_sensor:
      - name: "House Occupied"
        state: >
          {% if trigger.platform == 'event' %}
            {{ trigger.event.data.state }}
          {% else %}
            {{ trigger.to_state.state | int | bool }}
          {% endif %}

The template editor tool is only for testing templates and does not work with trigger-based templates, since there is no trigger for it to listen for. To create the sensor, you need to place the sensor configuration in your configuration.yaml file then reload the configuration.

When I add this to an Automation, why is it that binary_sensor.home_occupied is not listed as a State to select in a Condition? Do I need to separately create the actual sensor?

It is not an automation… why are you adding it to an automation?

Seems I’m confused then. I guess the template is it’s own thing. I just add it to my config?

1 Like

Got it sorted out. Again, thanks for all the input!