UndefinedError: 'trigger' is undefined : what did i miss

I have this input_select

  kettle_set:
    name: Set Kettle
    options:
      - 'Keepwarm Off :102,0'
      - 'Keepwarm 40c :102,1'
      - 'Keepwarm 50c :102,2'
      - 'Keepwarm 60c :102,3'
      - 'Keepwarm 80c :102,4'
      - 'Keepwarm 90c :102,5'

I have this automation

- id: 'Set the kettle Auto'
  alias: 'Set the kettle Auto'
  description: ''
  trigger:
  - entity_id: input_select.kettle_set
    platform: state
  condition: []
  action:
  - data:
      payload_template: '{% set dta = trigger.to_state.state %}{{dta.split(":")[1]}}'
      topic: bla/bla
    service: mqtt.publish

I want it to run when the state of the input_select is updated

2020-01-28 17:48:57 ERROR (MainThread) [homeassistant.components.automation] Error while executing automation automation.set_the_kettle_auto. Error rendering template for call_service at pos 1: UndefinedError: 'trigger' is undefined

What manual did i miss read ??

Should it be flipped?

trigger:
  - platform: state
    entity_id: input_select.kettle_set

thanks but
no go
Thats what got me stump

Im Missing something

I have done this

  trigger:
  - entity_id: light.garage
    platform: state

and this works

does this mean I have to have the

to:

with the input_select

I don’t believe you need ‘to:’ since the docs show an example without. I’ve always used/seen ‘platform’ being listed before the ‘entity_id’ but I suppose that should not really make any difference if the HA parsers worked independently of that order.

good we reading the same manual LOL

Is initial_state: true needed?

edit: i know its enabled by default but just wondering if it ever got disabled at some point and the recovery state of the automation is then out of whack.

Yes thats so it get turn on each time HA start

my understanding is if you turn off a automation next it wont start when HA restart.

but have

initial_state: true

will start it everytime

Tried data_template ?

1 Like

As @planetawylie mentioned, you need to change it to data_template try this:

 action:
  - data_template:
      payload: >
        {% set dta = trigger.to_state.state %}
        {{ dta.split('.')[1] }}
      topic: bla/bla
    service: mqtt.publish
1 Like

ammmmmmmmmm NO

the errror state this

2020-01-28 17:48:57 ERROR (MainThread) [homeassistant.components.automation] Error while executing automation automation.set_the_kettle_auto. Error rendering template for call_service at pos 1: UndefinedError: 'trigger' is undefined

it to do with the trigger:

not the action:

It’s complaining about the trigger in the action aI think, not about the teigger itself. Can you please show the full automation again?

wife does not understand "why dont you just walk up to kettle and turn … on by hand

2 Likes

here bro

- id: 'Set the kettle Auto'
  alias: 'Set the kettle Auto'
  description: ''
  trigger:
  - platform: state
    entity_id: input_select.kettle_set
  condition: []
  action:
  - data:
      payload_template: '{% set dta = trigger.to_state.state %}{{dta.split(":")[1]}}'
      topic: bla/bla
    service: mqtt.publish

its the trigger bit that HA not happy with when EXE it

Yeah you have ‘trigger’ inside your template and I’m almost sure that it is this that he complains about. Can’t test right now as I’m not home. Your example can’t work, did you try exactly the code I suggested?
Your template is wrong and you need to change data to data_template and payload_template to payload (as far as I know payload_template is not valid for automations.

When you execute an automation manually the trigger and condition part are skipped and it goes straight to the action part. And your action part can’t work when you trigger it manually, because you don’t have a trigger then and your template will fail. To ensure that the trigger part is correct, change the action part to something easy like turn on a switch, then change the input select and see if the switched turned on.

Edit: I just saw that your error message shows that it is the trigger in the template for the service call that is causing the issues and not the trigger part.

Error rendering template for call_service at pos 1: UndefinedError: 'trigger' is undefined

Edit 2: Just tested this in my test environment and can confirm it works as intended:

- id: '1578278123313'
  alias: 'Set the kettle Auto'
  description: ''
  trigger:
  - platform: state
    entity_id: input_select.kettle_set
  action:
  - service: mqtt.publish
    data_template:
      payload: "{{ trigger.to_state.state.split(':')[1] }}"
      topic: bla/bla
4 Likes

Yep, what he said :smile:

Thanks bro :beers:

I have used “payload_template:” else where everything worked

think I need to reread the manual again

1 Like