Value Template/syntax help, Using trigger_to as part of entity in condition?

I’m trying to cut down the number of automations from 4 to 1 by using trigger_to but can’t seem to figure out the proper code for this. Essentially an input_number is changed, I take that input_number and get its friendly name and use that in a state check, but I can’t get the state check to work. Here’s what I have. Any help would be greatly appreciated.

  condition:
    condition: template
    value_template: "{{ states.light."{{ states.trigger_to.state.attributes.friendly_name.replace( ' ', ')' ) | lower }}".state == 'on' }}"

the light entity and input_number entity have the same friendly_name so it’s a simple string replace to check state but my solution just isn’t working.

You cannot use nested jinja. example {{}} inside {{}}. or {{}} inside {%%}.

You should use the entity_id from the variable trigger.

Trigger sytnax is documented here.

This will work with any trigger:

  condition:
    condition: template
    value_template: "{{ is_state(trigger.entity_id, 'on') }}"

This will work with state triggers only:

  condition:
    condition: template
    value_template: "{{ trigger.to_state.state == 'on' }}"

that only gives the input_number entity though correct? if so that won’t work.

Do you mean the input_number and light entities have the same object_id, or the same friendly_name attribute, or both?

If I understand correctly, the automation trigger is based on the input_number, but in the condition you want to check the state of the associated light. Is that right?

Assuming they have the same object_id, then this should do what you want:

  condition:
    condition: template
    value_template: "{{ is_state('light.' ~ trigger.to_state.state.object_id, 'on') }}"

If they only have the same friendly_name, it’s still possible, but a bit more involved. Let me know if the above works for you. If not, and it needs to be based on the friendly_name, then I can probably come up with something that will work for that, too.

EDIT: Out of curiosity, why do you need a template? Can’t you just use the entity_id of the light? Or are there more than one input_number/light pairs associated with this automation? (That’s why it’s usually better to provide more context - e.g., the full automation - in these topics.)

Do you mean the input_number and light entities have the same object_id, or the same friendly_name attribute, or both?

friendly_name

If I understand correctly, the automation trigger is based on the input_number, but in the condition you want to check the state of the associated light. Is that right?

yes

If they only have the same friendly_name, it’s still possible, but a bit more involved. Let me know if the above works for you. If not, and it needs to be based on the friendly_name, then I can probably come up with something that will work for that, too.

that’s what I’m working out

EDIT: Out of curiosity, why do you need a template? Can’t you just use the entity_id of the light? Or are there more than one input_number/light pairs associated with this automation? (That’s why it’s usually better to provide more context - e.g., the full automation - in these topics.)

if I had only 1 light i wouldn’t need a template, but i have a lot and they are all based on the same naming structure… ie light is light.a_day_light, light.b_day_light… .and so on… the corresponding input_number is the same input_number.a_day_light, input_number.b_day_light.

with this it’s just a matter of stripping out friendly_name to use to turn on/off the associated light as well as the mqtt topics since I can take the friendly name and use the split function to parse out my topic.

Those are object_id’s, not friendly_name’s. entity_id’s are made up of domain and object_id separated by a dot. friendly_name is an attribute. E.g., states.domain.object_id.attributes.friendly_name.

So the solution I provided should work for you.

it won’t load… here’s the entire automation

 - alias: Change LED Bed Lights
   trigger:
     - platform: state
       entity_id: input_number.a_day_lights, input_number.a_bed_lights, input_number.b_bed_lights, input_number.b_day_lights
   condition:
     condition: template
     value_template: "{{ is_state('light.' ~ trigger.to_state.state.object_id, 'on') }}"
   action:
     service: mqtt.publish
     data_template:
       topic: 'ha/{{ states.trigger_to.attributes.friendly_name.split()[0] | lower}}/lights/leds/{{ states.trigger_to.attributes.friendly_name.split()[1] | lower}}/cmd'
       payload: '{{ states.trigger_to.state }}'
       retain: false

It’s because the of problems in your action, not the condition. There is no such thing as states.trigger_to.

I may be wrong, but I am under the impression that the trigger data is only available in the action, and not in conditions. Not sure what you are trying to accomplish, but you should be able to expand the condition logic without using trigger data and still achieve the desired functionality.

It is available in both.

If you can provide an example of what the mqtt topic and payload should look like, I can probably tell you what your templates for those should be.

Thanks, good to know! I will try that sometime!

1 Like

the topic should be ha/(1st part of entity)/lights/leds/(2nd part of entity)/cmd
payload should be the input_number(trigger) value itself

i tried to cobble this thing together last night after about 40 hours of non sleep and just wasn’t paying attention to everything i had written.

When you say (1st part of entity), do you mean the input_number’s or the light’s entity? The first part of the entity_id is, of course, the domain. So for input_number.a_day_light that would be input_number, and for light.a_day_light that would be light. So do you mean ha/input_number/lights/leds/... or ha/light/lights/leds/..., or something else?

How about, let’s be specific. If the input_number that changed was input_number.a_day_light, what should the topic be exactly?

The payload part is easy. That should just be:

       payload: '{{ trigger.to_state.state }}'

BTW, you might want to read Trigger State Object and Available Trigger Data which follows.

ha/a/lights/led/day/cmd

i have read the trigger state object, I’ve used it other automations i just can’t figure this one out.

1 Like

Ah! Ok, you mean the 1st and 2nd parts of the object_id. (Oh, and BTW, now that I look back at it, I did make a mistake on the condition. :blush: ) So, the complete automation should be:

 - alias: Change LED Bed Lights
   trigger:
     platform: state
     entity_id: input_number.a_day_lights, input_number.a_bed_lights, input_number.b_bed_lights, input_number.b_day_lights
   condition:
     condition: template
     value_template: "{{ is_state('light.' ~ trigger.to_state.object_id, 'on') }}"
   action:
     service: mqtt.publish
     data_template:
       topic: "ha/{{ trigger.to_state.object_id.split('_')[0] }}/lights/leds/{{ trigger.to_state.object_id.split('_')[1] }}/cmd"
       payload: "{{ trigger.to_state.state }}"

EDIT: I just noticed earlier your topic included ‘leds’ but now you said ‘led’. So adjust if it is indeed led vs leds.

come back from a meeting and the thread took off! this is the correct solution @forsquirel

1 Like

Garage door guy came and had to deal with that.

For some reason the condition is still not being met. I’m not seeing the MQTT message being published.

the payload is incorrect…

What is the payload supposed to look like? I assumed you wanted it to be the state of the input_number.

I got it working. for some reason it doesn’t like double quotes. Everything is working as should with a single quote

Thanks for the help. This looks so much cleaner and better than having 4 separate automations!

mta: everything is working as is with double quotes, syntax error on my part. I’ve marked your post as the solution. Thanks again.

1 Like