Trigger.to_state to variable?

Hi everybody,

I am trying to convert multiple automations into one, using trigger.to_state to determine what is supposed to happen.

alias: Triggertest
description: ""
triggers:
  - trigger: time
    at: input_datetime.triggertest_az_00
  - trigger: time
    at: input_datetime.triggertest_az_01
  - trigger: time
    at: input_datetime.triggertest_wz_00
  - trigger: time
    at: input_datetime.triggertest_wz_01
conditions: []
actions:
  - variables: {}
mode: single

How do I go from here? I would like to use input_number.set_value, where the pattern is input_number.triggertest_az_00, input_number.triggertest_az_01, input_number.triggertest_wz_00, input_number.triggertest_wz_01, depending on the entity_id of the input_datetime, which triggers this automation.

Concrete example:

if triggered by input_datetime.triggertest_az_00, then input_number.set_value the entity input_number.triggertest_az_current to the value of input_number.triggertest_az_00. Then, of course:

if triggered by input_datetime.triggertest_wz_01, then input_number.set_value the entity input_number.triggertest_wz_current to the value of input_number.triggertest_wz_01.

So always set xyz_current (where xyz is the abbreviation for the room) to the input_numbers state, which it gets from the input_datetimes entity id.

Basically, what’s going on here is that at certain I want climate entities to be set to specific values. There is always (...)_00 up to (...)_04 per room. Currently, these are all separate automations - and they are using trigger ids.

However, this seems like a case for trigger.to_(...), right? I just can’t figure out how to do it.

Hopefully I have explained this in a way people can understand. If not, sorry, please ask and I’ll try my best to clearify.

Thank you in advance for your help :slight_smile:

These are the variables available from a time trigger: Automation trigger variables - Home Assistant (no entity id). You could use trigger ids to identity which trigger occurred.

triggers:
  - id: az_00
    trigger: time
    at: input_datetime.triggertest_az_00
  - id: az_01
    trigger: time
    at: input_datetime.triggertest_az_01
  - id: wz_00
    trigger: time
    at: input_datetime.triggertest_wz_00
  - id: wz_01
    trigger: time
    at: input_datetime.triggertest_wz_01
actions:
  - action: input_number.set_time
    target:
      entity_id: "{{'input_number.triggertest_' ~ trigger.id ~ '_current' }}"
    data:
     time: "{{ states('input_number.triggertest_' ~ trigger.id }} 

Thank you.

I got this

actions:
  - action: input_number.set_value
    target:
      entity_id:  "{{'input_number.triggertest_' ~ trigger.id.split('.')[1].split('_')[1] ~ '_current' }}"
    data:
      value:

When I use this in templates, I get that

{% set tausch = "input_datetime.triggertest_az_00" %}
 "{{'input_number.triggertest_' ~ tausch.split('.')[1].split('_')[1] ~ '_current' }}"

==> "input_number.triggertest_az_current"

However, I am not sure what to put as value; This needs to become the value of input_number.triggertest_az_00 if the trigger equals input_datetime.triggertest_az_00.

So here we need az (or whatever room abbr.) and 00 (or whatever number there is), but we need the actual state of this, not the name. This is pretty confusing to me.

as for the string splitting of those entities, you can use

removeprefix('input_datetime.triggertest_') to get to the important az_00 bit

1 Like

Use the states() function to get the value. Like I did. Not sure why you are over complicating it. Did you try what I showed above?

I tried your solution, but it did not work. Below is the expected output; maybe it is clumsy, but seems to work. Your solution created something like (not exactly, but I cannot reproduce it at the moment) input_number.triggertest_input_number.triggertest_az_00). Like, it wouldn’t replace the part it was supposed to, but simply add it to the string.

{% set trigger = "input_datetime.triggertest_az_00" %}
{% set name = (trigger.removeprefix("input_datetime.triggertest")) %}
{% set name2 =  name.replace("00", "") %}
-----
trigger is "{{ trigger }}"
name is "{{ name }}"
name2 is "{{ name2 }}"
-----
"{{"input_number" ~ name2 ~ 'current' }}" 
"{{'input_number.triggertest' ~ name ~ '' }}"

Then you copied it incorrectly. Or set the ids incorrectly.

Typo in tirgger.id.

1 Like

Thanks for the extra set of eyes Troon.

Fixed in the original post.

Thank you Troon and tom_l; but that was not it. If I were to use tirgger.id, I would get no output at all, but rather an error like this. I will try to reproduce what I got when first trying this, then report back :slight_smile:

EDIT:
I changed it to this

{% set trigger = "input_datetime.triggertest_00" %}
time: "{{ states('input_number.triggertest_' ~ trigger) }}"

tried this as well time: "{{ states('input_number.triggertest_' ~ trigger.id) }}" (with the first line as in the codeblock).

EDIT2: as you can see, I am testing this in the template editor; That’s why {% set trigger......, because there is no decent way to test this within an automation (as least I couldn’t find one). Might that be the issue? But then, how would I decently test this? Since the trigger is based on an input_datetime, I could manually change this datetime, wait 1s-59s (depending on current time), see what happens, then repeat - that is very impractical, that’s why I am using the editor…

Are you trying to test this in the template editor?

The trigger id is set here in the automation:

The template editor knows nothing about that.

Just try the automation as posted above.

I see what’s the issue here.

I am not talking about this:

automation:
  triggers:
    - trigger: state
      entity_id: device_tracker.paulus
      id: paulus_device

I meant what they are describing in this thread.

Not the trigger id, am I particularly trying to avoid that. What I am trying to do, is use n different triggers, all of which will trigger the same action, but this action ought to change based on the trigger.to_state that triggered it. Not the trigger id.

If I were to use trigger id, I could leave my automation as is. I am trying to get rid of trigger id so that any input_datetime I chose as trigger will automatically trigger the same action, but change parameters within that action due to trigger.to_state (or similar).

Sorry for the confusion!

trigger.to_state will give the entir state object the entity changed to. Use trigger.to_state.state for the new state of the entity

Except they are using time triggers with input_datetimes and want to recover the trigger input datetime entity id. Which is not possible. The only way I can think to do it is to reconstruct the entity id using a trigger id but they don’t want to do this.