{{trigger.topic.split('/')[2]}} not working for MQTT subscribing

I am finding it difficult to resolve this issue and I would be grateful for any help. I have two raspberry pi 3’s running home assistant. Hassbian1 is the primary controller and hassbian2 is in MQTT communication with hassbian1.

To subscribe to the times for a pump to start from hassbian2 to hassbian1 I have the following automation in hassbian1:

- alias: MQTT subscribe input_datetime command
  trigger:
    - platform: mqtt
      topic: 'hassbian2/input_datetime/pool_start_time/state'
    - platform: mqtt
      topic: 'hassbian2/input_datetime/pool_stop_time/state'
  action:
    - service_template: "input_datetime.set_datetime"
      entity_id: "input_datetime.{{trigger.topic.split('/')[2]}}"
      data_template:
        time: "{{trigger.payload}}"

This comes out as “Valid YAML” in yamllint.com. But it produces the following error:

hassbian1 hass[2556]: 2019-10-03 22:14:12 ERROR (MainThread) [homeassistant.config] Invalid config for [automation]: not a valid value for dictionary value @ data['action'][0]['entity_id']. Got "input_datetime.{{trigger.topic.split('/')[2]}}". (See /home/homeassistant/.homeassistant/automations.yaml, line 125). Please check the docs at https://home-assistant.io/components/automation/

If I change the line:
entity_id: "input_datetime.{{trigger.topic.split('/')[2]}}"
to:
entity_id: "input_datetime.pool_start_time"
this works fine, but of course only updates the pool_start_time, not the pool_end_time.

I have used {{trigger.topic.split(’/’)[2]}} in a different context in other automations:

- alias: MQTT subscribe input_boolean command
  initial_state: 'true'
  trigger:
    - platform: mqtt
      topic: 'hassbian2/input_boolean/automated_pool_pump/set'
    - platform: mqtt
      topic: 'hassbian2/input_boolean/turn_on_pool_for_specified_duration/set'
    - platform: mqtt
      topic: 'hassbian2/input_boolean/start_pool_set_times/set'
  action:
    service_template: "input_boolean.turn_{{trigger.payload | lower}}"
    data_template:
      entity_id: "input_boolean.{{trigger.topic.split('/')[2]}}"

and this works fine.

All advice gratefully received.

Move the line containing entity_id below the line containing data_template.

Many thanks @123 for an amazingly quick reply. I changed the service action to:

  action:
    - service_template: "input_datetime.set_datetime"
      data_template:
        entity_id: "input_datetime.{{trigger.topic.split('/')[2]}}"
        time: "{{trigger.payload}}"

and it is working just fine!
Thanks again, much appreciated.

1 Like

FWIW, the custom of this forum is to mark the original post offering the functional solution with ‘Solution’. Marking your own post containing a copy of the offered solution is misleading.

However, it you feel more comfortable marking your own post as the Solution, so be it.

Very sorry @123 I had presumed marking “Solution” signified the end of the thread. I have moved the Solution tick to your post. Thank you for your advice and the for providing the solution.

1 Like