Input Boolean Target determined by template

I’m made a dog food tracking page using input booleans.

What i’m attempting to do is have the IOS action turn on a boolean according to day and time.
It’s easy enough to get the boolean name using a template, but I’m unable to use it in the target field for the input_boolean.turnon function.

{% set sensor_names = [ 'dogmon', 'dogtues', 'dogwed', 'dogthurs', 'dogfri', 'dogsat', 'dogsun' ] %}
{% set today_name = sensor_names[now().weekday()] %}

{% if now().strftime("%H") | float < 13 -%}
  {% set entity_name =  'input_booleon.'+today_name+'am' %}
{%- else -%}
  {% set entity_name =  'input_booleon.'+today_name+'pm' %}
{%- endif %}

I can get it usable in a data_template by creating a template sensor, but that doesn’t work for input booleans.

I want to be able to do this:

service: input_boolean.turn_on
target:
  entity_id: {{ entity_name }}

Has anyone else tried this?

Besides the fact that {{ entity_name }} should be in-between quotes, It should work. What’s the issue?
Please show the full automation you’ve implemented and the errors.

EDIT: Oh, and 'input_booleon.'+today_name.. should be input_boolean... :wink:

Where in the automation are you defining entity_name?

If you define it in variables it will be accessible to all templates within the automation’s action.

action:
  - variables:
      entity_name: >
        {% set names = [ 'dogmon', 'dogtues', 'dogwed', 'dogthurs', 'dogfri', 'dogsat', 'dogsun' ] %}
        input_boolean.{{ names[now().weekday()] ~ 'am' if now().hour < 13 else 'pm' }}
  - service: input_boolean.turn_on
    target:
      entity_id: '{{ entity_name }}'
 ... etc ...
 ... variable entity_name accessible to other service calls ...

If only one service call needs it then defining it as a variable is completely optional because it can all be done within the target’s entity_id.

action:
  - service: input_boolean.turn_on
    target:
      entity_id: >
        {% set names = [ 'dogmon', 'dogtues', 'dogwed', 'dogthurs', 'dogfri', 'dogsat', 'dogsun' ] %}
        input_boolean.{{ names[now().weekday()] ~ 'am' if now().hour < 13 else 'pm' }}

BTW, this is incorrect because the template lacks outer quotes:

target:
  entity_id: {{ entity_name }}

Taras, thank you so much! I was trying to get the variable set in the script and it kept ejecting it, so i defined it in the sensors as a template and it didn’t like that either. Oh, and it didn’t’ like the variable name with or without the quotes.

Just to get it working, I went with your simple option of just a toggle and it worked when I tested it! Now that this is working, I can add a TTS to my speaker or maybe a dinner triangle sound and work on the logging.

My wife will also like this because I have it notify both of us in the morning and now she can just tell it that she fed the dog from the notification either on the phone or the watch.

Yes, I had caught the misspelling after I submitted this and fixed that part. I was not able to get it to work with or without quotes.

I can tell you for certain that without the outer quotes, it is a syntax error. Here’s the result of Check Configuration, complaining about the template on line 11:

Screenshot from 2021-07-28 07-50-03

for this simple example where the template, on line 11, is not delimited by quotes:

Screenshot from 2021-07-28 07-51-49

Anyway, glad to hear it all worked out in the end.