Template use to set entity_id

Hi all,

I try to set some input_boolean to “on” when certain input_select option is activated, and I want the previously active input_boolean to be turned of.

I wrote the following code and it parses fine, but no input_booleans get turned on.

help appreciated!


- alias: some input_booleans - external update
  trigger:
    - platform: state
      entity_id: input_select.remote_activities
  condition:
    condition: template
    value_template: "{{ trigger.to_state.state != 'unknown' }}"
  action:
    - delay: '00:00:20'
    - service: input_boolean.turn_on
      data_template:
        entity_id: >
          {% if is_state(trigger.to_state), 'act 1' %}
            input_boolean.act1
          {% elif is_state(trigger.to_state), 'act 2' %}
            input_boolean.act2
          {% else %}
            input_boolean.dummy
          {% endif %}
    - service: input_boolean.turn_off
      data_template:
        entity_id: >   
          {% if is_state(trigger.from_state), 'act 1' %}
            input_boolean.act1
          {% elif is_state(trigger.from_state), 'act 2' %}
            input_boolean.act2
          {% else %}
            input_boolean.dummy
          {% endif %}

Use:

  • trigger.to_state.state and not trigger.to_state
  • trigger.from_state.state and not trigger.from_state

like this?

{% if trigger.from_state.state == 'act 1' %}

or like this?

{% if is_state(trigger.from_state.state), 'act 1' %}

Like this:

{% if trigger.from_state.state == 'act 1' %}

or like this:

{% if is_state(trigger.from_state.state, ‘act 1’) %}


EDIT
Corrected as per petro’s explanation below.

Looks like this now, but still does not turn on the booleans…

 action:
    - delay: '00:00:20'
    - service: input_boolean.turn_on
      data_template:
        entity_id: >
          {% if is_state(trigger.to_state.state, 'act 1') %}
            input_boolean.act1
          {% elif is_state(trigger.to_state.state, 'act 2') %}
            input_boolean.act2
          {% else %}
            input_boolean.dummy
          {% endif %}

not even input_boolean.dummy?

Your automation’s action is designed to turn on the input_boolean and then immediately turn it off.

Based on that behavior (on then off), how are you checking if the input_boolean was ever turned on? It won’t show in the States page so are you checking the Logbook?

is_state only accepts entity_id’s unless that’s changed recently. So if you use is_state, then the code would be:

 action:
    - delay: '00:00:20'
    - service: input_boolean.turn_on
      data_template:
        entity_id: >
          {% if is_state(trigger.to_state.entity_id, 'act 1') %}
            input_boolean.act1
          {% elif is_state(trigger.to_state.entity_id, 'act 2') %}
            input_boolean.act2
          {% else %}
            input_boolean.dummy
          {% endif %}
1 Like

I need my morning coffee …

1 Like

This should work:

- alias: some input_booleans - external update
  trigger:
    - platform: state
      entity_id: input_select.remote_activities
  condition:
    condition: template
    value_template: "{{ trigger.to_state.state != 'unknown' }}"
  action:
    - delay: '00:00:20'
    - service: input_boolean.turn_on
      data_template:
        entity_id: >
          {% if trigger.to_state.state | regex_search('act [1-2]')  %}
            input_boolean.act{{trigger.to_state.state[-1]}}
          {% else %}
            input_boolean.dummy
          {% endif %}
    - service: input_boolean.turn_off
      data_template:
        entity_id: >   
          {% if trigger.from_state.state | regex_search('act [1-2]')  %}
            input_boolean.act{{trigger.from_state.state[-1]}}
          {% else %}
            input_boolean.dummy
          {% endif %}
1 Like

Thanks for all the support!
The last solutions looks great if the state name and the input_boolean name are identical! In my case this is not so because the state names contain spaces.

So I went for

{% if trigger.from_state.state == 'act 1' %}

Actually, it is designed to meet your situation where the two are different (i.e. state’s value contains a space but the input_boolean’s name does not).

Look carefully at this line:

 input_boolean.act{{trigger.to_state.state[-1]}}

It takes the last character in

trigger.to_state.state

and appends it to the string

input_boolean.act

So if the value is

act 2

the string will be

input_boolean.act2

I see. But this is not my situation. I am rather talking about something like:
input_boolean.bla_blibbblubb
input_select.dork state: “Blibb to Blubb”

Sure if I rename the input_booleans (blibbblubb -> blibbtoblubb) a little bit it can be done with some string operations (to lower case, remove spaces).

If you have the string ops from the top of your head I will happily go ahead and implement it like that. (It is running now for me, but the overall code is rather lengthy and repetitive).

Also the other direction takes a lot of repetitive code, but I thought it is not possible to use a template for the trigger itself, right?

So I have a number of auch repetitive code blocks for the other “direction”:

- alias: Set input_select from input_boolean - Blibb Blubb
  trigger:
    - platform: state
      entity_id: input_boolean.something_blibbblubb
  condition:
    condition: template
    value_template: "{{ trigger.to_state.state == 'on' }}"
  action:
    - service: input_select.select_option
      data:
        entity_id: input_select.harmony_hub_living_room_activities
        option: "Blibb Blubb"

It would be great to tighten this and get rid of the repetitive blocks…

The example I offered is based on what you originally posted, where it’s simply ‘act 1’ and ‘act1’. We can only help you with what you share with us, so no one could’ve guessed your situation is more like the ‘blibbblubb’ example. So let’s see what can be done to help you with that.

I suggest you make the input_boolean’s name match an option in the input_select.

For example:

input_boolean:
  blibb_blubb:
    name: Blibb Blubb
  flipp_flopp:
    name: Flipp Flopp
  flimm_flamm:
    name: Flimm Flamm

input_select:
  harmony_hub_living_room_activities:
    options:
      - Blibb Blubb
      - Flipp Flopp
      - Flimm Flamm

This simplifies using them in an automation. In the following example, trigger.to_state.name will contain the input_boolean’s name (Blibb Blubb or Flipp Flopp or Flimm Flamm).

- alias: Set input_select from any input_boolean
  trigger:
    - platform: state
      entity_id: input_boolean.blibb_blubb
    - platform: state
      entity_id: input_boolean.flipp_flopp
    - platform: state
      entity_id: input_boolean.flimm_flamm
  condition:
    condition: template
    value_template: "{{ trigger.to_state.state == 'on' }}"
  action:
    - service: input_select.select_option
      data_template:
        entity_id: input_select.harmony_hub_living_room_activities
        option: "{{trigger.to_state.name}}

Looks interesting. Obviously some characters need to be automatically replaced by HW when putting the state into trigger-to_state.name

Will “+” and “/” be translated, and if so, into what character? Is it “_” ?

Actually, it’s not obvious because no one else but you knows how you’ve named your entities and options.

To help us help you, please post the configuration of your input_select and input_booleans so we can make informed suggestions on how best to handle them.

with all due respect and thankfulness for your help - the config is not at he core of the question.
the core of the question actually is:

if a state contains characters that are not allowed for entity_id’s, will trigger_to.state.name automatically translate such characters into a syntactically allowed replacement character, and which is that? “_” ?

when I know the replacement character I know how to rename my input boolean entity_id’s so that they will match the translated state names.

The example I offered doesn’t use entity_id it uses the name (same as friendly_name) which has fewer naming restrictions.

Oh I misunderstood that part. Sorry. That means that I have to choose the input_select option strings as friendly name for the according input booleans?

and would something like this also work for the other direction?

- service: input_boolean.turn_on
      data_template:
        entity_id: input_boolean.blabla_"{{ trigger.to_state.name }}"

where trigger.to_state.name of the input_select options which triggered actually matches the latter part of the input_boolean name.

Yes, that’s how I designed the example I posted above. If you do that, it simplifies the automation’s template.

trigger.to_state.name represents the friendly_name of whichever input_boolean triggered the automation. If it was input_boolean.flipp_flopp then option is assigned Flipp Flopp.