#Valid to use template for `entity_id` in a condition?

41 messages · Page 1 of 1 (latest)

quick spear
#

Are you allowed to use a template for the entity_id field?

condition: numeric_state
entity_id: "{{ <template that evaluates to an entity_id> }}"
below: 10
topaz hedge
#

No, you can't use a template there

teal ridge
#

What are you trying to accomplish? There may be a different way.

For example, use of fields or blueprints.

gloomy swift
#

You can use such template in action, but I believe not in condition. However, you can use template condition like this:

condition: template
template_value: "{{ states(trigger.entity_id)|int < 10 }}"
#

Where trigger.entity_id is example of your entity.

wooden juniper
#

Not sure if I'm meant to open a new thread or use this, as it's basically the same question I have. Let me know if a new thread is preferred.

#

In an automation, I am trying to trigger a script where the name of the script to trigger is the one selected in an input_helper.

I saw in the docs :

actions:
  - action: light.turn_off
    target:
      entity_id: "{{ trigger.entity_id }}"

(from 'automation 3' in the examples on https://www.home-assistant.io/docs/automation/templating/ )

I've tried to add in my automation:

entity_id: "script.{{ states('input_select.livingroom_ac_morning') }}"

But that does not work.

#

Although the top of that page it says that 'the Home Assistant templates extensions to scripts are available for automations', I'm guessing this does not apply to every field.

#

So I'm wondering ... what is my best approach here?

  1. Somehow make this possible through variables or something?
  2. Trigger a script that figures out what's in the input_select, and have that then trigger that script?
#

Would welcome any thoughts on this / experience in the matter.

trim prism
#

Where exactly are you using this entity_id field?

gloomy swift
wooden juniper
# trim prism Where exactly are you using this entity_id field?
- id: livingroom_ac_daymode
  alias: Turn on living room a/c when waking up

  trigger:
    platform: state
    entity_id: input_select.home_mode
    to: 'day'

  condition:
    condition: state
    entity_id: binary_sensor.home_occupied
    state: 'on'

  action:
    - service: script.turn_on
      # entity_id: "script.{{ states('input_select.livingroom_ac_morning') }}"
      entity_id: script.livingroom_airconditioner_auto26_focusedright
wooden juniper
# gloomy swift Yeah, give the full action code, where you're using this. Also would be nice to ...
livingroom_ac_morning:
    name: Livingroom a/c morning setting
    options:
      - livingroom_airconditioner_auto20
      - livingroom_airconditioner_auto21
      - livingroom_airconditioner_auto22
      - livingroom_airconditioner_auto23
      - livingroom_airconditioner_auto24
      - livingroom_airconditioner_auto25
      - livingroom_airconditioner_auto26
      - livingroom_airconditioner_auto27
      - livingroom_airconditioner_auto28
      - livingroom_airconditioner_auto29
      - livingroom_airconditioner_auto20_focusedright
      - livingroom_airconditioner_auto21_focusedright
      - livingroom_airconditioner_auto22_focusedright
      - livingroom_airconditioner_auto23_focusedright
      - livingroom_airconditioner_auto24_focusedright
      - livingroom_airconditioner_auto25_focusedright
      - livingroom_airconditioner_auto26_focusedright
      - livingroom_airconditioner_auto27_focusedright
      - livingroom_airconditioner_auto28_focusedright
      - livingroom_airconditioner_auto29_focusedright
    initial: livingroom_airconditioner_auto25_focusedright
#

Apologies, I should have included these in the first place. I just wasn't sure if I should post here or open a new thread.

#

Thanks for your help!

gloomy swift
#

Do you have trace for this automation?

#

What doesn't work?

#

Seems ok to me

wooden juniper
#

To further add, the scripts mentioned all look like this:

livingroom_airconditioner_auto25_focusedright:
  sequence:
    - service: remote.send_command
      data:
        entity_id: remote.kitchen_broadlink
        device: livingroom_airconditioner
        command: power_on_temp_25_fan_auto_direction_focusedright

I know this is a bit silly and I could just use that command directly, but this is how I built it close to a decade ago and I call these scripts from a bunch of automations so I've just not gotten around to rejigging that all.

#

I mean, obviously right now it works. I actually want to use the commented line 🙂

gloomy swift
#

It's solid stuff.

#

With uncommented line - what does it say?

wooden juniper
#

But if I uncomment that line, and comment the other ... HA says "Nope." and disables it. Let me try to reload to get you an exact error message. Give me 1 minute.

gloomy swift
#

Hmm

wooden juniper
#

The automation "Turn on living room a/c when waking up" (automation.set_living_room_air_conditioning_for_day_mode) is not active because the configuration has errors.

Error:not a valid value for dictionary value @ data['actions'][0]['entity_id']. Got "script.{{ states('input_select.livingroom_ac_morning') }}".

gloomy swift
#

I don't think it should restrict entity id templating, but maybe for script run it does...

wooden juniper
#

I've tried to find clarity in the documentation, but couldn't find consistent "does or does not support templating" for different fields.

gloomy swift
#

Yeah I definitely have templates for entity IDs in several places. But I guess it differs action by action

wooden juniper
#

Hmm, it feels a bit arbitrary that it wouldn't do that for script.turn_on. But I guess it's possible.

gloomy swift
#

I just quickly did this in script:

action: script.turn_on
target:
  entity_id: script.{{ "adjust_humidifiers_for_area" }}

It allows me.

#

You forgot target:

wooden juniper
#

Ugh.

#

I'm so sorry.

#

That's a dumbass mistake.

topaz hedge
#

You can't use a template unless it's under data: or target:

wooden juniper
#

Yep. That fixed it.

#

I'm really sorry! That's a mistake I shouldn't have bugged you guys with.

gloomy swift
#

Hah no problem, i also didn't realize it right away

#

Glad it worked

wooden juniper
#

it does indeed 🙂