Publish MQTT From an Automation

I have a RF RGB LED controller that I am trying to control via a openmqttgateway. I have a input selector with the effects and a automation for sending the effect out over MQTT. The input selector works and it triggers the automation but never sends anything out. Any help to shed some light on this would be appreciated.

input_select:
  rgb_effect:
    name: Pond RGB Effect
    options:
     - Red
     - Green
     - Jump 7
     - Fade 3
     - Fade 7
    initial: Fade 7


- id: PondRGBEffectSend
#  hide_entity: true
  trigger:
    platform: state
    entity_id: input_select.rgb_effect
  action:
  - service: mqtt.publish
    data_template:
      entity_id: >
        {% if is_state("input_select.rgb_effect", "Red") %}
          topic: 'home/commands/PLSL_400/433_1'
          payload: "6121737"
        {% elif is_state("input_select.rgb_effect", "Green") %}
          topic: 'home/commands/PLSL_400/433_1'
          payload: "6121738"
        {% endif %}
1 Like

try this,

action:
- service: mqtt.publish
  data_template:
    {% if is_state("input_select.rgb_effect", "Red") %}
      topic: 'home/commands/PLSL_400/433_1'
      payload: "6121737"
    {% elif is_state("input_select.rgb_effect", "Green") %}
      topic: 'home/commands/PLSL_400/433_1'
      payload: "6121738"
    {% endif %}

no need for entity_id. Also make sure your topics and payloads are correct, “6121738” is not the same as 6121738

I just tried removing the entity_id, and the Configuration validation throw an error:

Testing configuration at /home/ha/.homeassistant 2018-07-07 11:13:46 ERROR (Thread-2) [homeassistant.util.yaml] while scanning for the next token found character '%' that cannot start any token in "/home/ha/.homeassistant/automations.yaml", line 111, column 8

line 111 is where the {% if is_state starts.

looks like I messed up on the indentation. service should be indented.

action:
  service: mqtt.publish
  data_template: >
    {% if is_state("input_select.rgb_effect", "Red") %}
      topic: 'home/commands/PLSL_400/433_1'
      payload: "6121737"
    {% elif is_state("input_select.rgb_effect", "Green") %}
      topic: 'home/commands/PLSL_400/433_1'
      payload: "6121738"
    {% endif %}

I indented like you said, and the first error went away but now the whole file automation file is getting an error.

- id: PondRGBEffectSend
#  hide_entity: true
  trigger:
    platform: state
    entity_id: input_select.rgb_effect
  action:
    service: mqtt.publish
    data_template: >
      {% if is_state("input_select.rgb_effect", "Red") %}
        topic: 'home/commands/PLSL_400/433_1'
        payload: "6121737"
      {% elif is_state("input_select.rgb_effect", "Green") %}
        topic: 'home/commands/PLSL_400/433_1'
        payload: "6121738"
      {% endif %}

Testing configuration at /home/ha/.homeassistant 2018-07-07 13:08:35 INFO (MainThread) [homeassistant.setup] Setting up mqtt 2018-07-07 13:08:35 INFO (MainThread) [homeassistant.setup] Setting up http 2018-07-07 13:08:35 INFO (MainThread) [homeassistant.setup] Setting up recorder 2018-07-07 13:08:35 INFO (MainThread) [homeassistant.setup] Setup of domain mqtt took 0.0 seconds. 2018-07-07 13:08:35 INFO (MainThread) [homeassistant.setup] Setup of domain http took 0.0 seconds. 2018-07-07 13:08:35 INFO (MainThread) [homeassistant.setup] Setup of domain recorder took 0.0 seconds. 2018-07-07 13:08:35 INFO (MainThread) [homeassistant.setup] Setting up websocket_api 2018-07-07 13:08:35 INFO (MainThread) [homeassistant.setup] Setting up system_log 2018-07-07 13:08:35 INFO (MainThread) [homeassistant.setup] Setting up api 2018-07-07 13:08:35 INFO (MainThread) [homeassistant.setup] Setting up history 2018-07-07 13:08:35 INFO (MainThread) [homeassistant.setup] Setup of domain websocket_api took 0.0 seconds. 2018-07-07 13:08:35 INFO (MainThread) [homeassistant.setup] Setup of domain system_log took 0.0 seconds. 2018-07-07 13:08:35 INFO (MainThread) [homeassistant.setup] Setup of domain api took 0.0 seconds. 2018-07-07 13:08:35 INFO (MainThread) [homeassistant.setup] Setup of domain history took 0.0 seconds. 2018-07-07 13:08:35 INFO (MainThread) [homeassistant.setup] Setting up frontend 2018-07-07 13:08:35 INFO (MainThread) [homeassistant.setup] Setup of domain frontend took 0.0 seconds. 2018-07-07 13:08:35 INFO (MainThread) [homeassistant.setup] Setting up updater 2018-07-07 13:08:35 INFO (MainThread) [homeassistant.setup] Setting up group 2018-07-07 13:08:35 INFO (MainThread) [homeassistant.setup] Setting up sensor 2018-07-07 13:08:35 INFO (MainThread) [homeassistant.setup] Setting up map 2018-07-07 13:08:35 ERROR (MainThread) [homeassistant.config] Invalid config for [automation]: expected a dictionary for dictionary value @ data['action'][0]['data_template']. Got None. (See /home/ha/.homeassistant/configuration.yaml, line 98). Please check the docs at https://home-assistant.io/components/automation/ 2018-07-07 13:08:35 INFO (MainThread) [homeassistant.setup] Setting up sun 2018-07-07 13:08:35 INFO (MainThread) [homeassistant.setup] Setting up input_select 2018-07-07 13:08:35 INFO (MainThread) [homeassistant.setup] Setup of domain updater took 0.2 seconds. 2018-07-07 13:08:35 INFO (MainThread) [homeassistant.setup] Setup of domain group took 0.1 seconds. 2018-07-07 13:08:35 INFO (MainThread) [homeassistant.setup] Setup of domain sensor took 0.1 seconds. 2018-07-07 13:08:35 INFO (MainThread) [homeassistant.setup] Setup of domain map took 0.1 seconds. 2018-07-07 13:08:35 INFO (MainThread) [homeassistant.setup] Setup of domain sun took 0.1 seconds. 2018-07-07 13:08:35 INFO (MainThread) [homeassistant.setup] Setup of domain input_select took 0.0 seconds. 2018-07-07 13:08:35 INFO (MainThread) [homeassistant.setup] Setting up tts 2018-07-07 13:08:35 INFO (MainThread) [homeassistant.setup] Setting up config 2018-07-07 13:08:35 INFO (MainThread) [homeassistant.setup] Setting up logbook 2018-07-07 13:08:35 INFO (MainThread) [homeassistant.setup] Setting up conversation 2018-07-07 13:08:35 INFO (MainThread) [homeassistant.setup] Setup of domain tts took 0.0 seconds. 2018-07-07 13:08:35 INFO (MainThread) [homeassistant.setup] Setting up script 2018-07-07 13:08:35 INFO (MainThread) [homeassistant.setup] Setting up light 2018-07-07 13:08:35 INFO (MainThread) [homeassistant.setup] Setting up switch 2018-07-07 13:08:35 INFO (MainThread) [homeassistant.setup] Setting up automation 2018-07-07 13:08:35 INFO (MainThread) [homeassistant.setup] Setup of domain config took 0.0 seconds. 2018-07-07 13:08:35 INFO (MainThread) [homeassistant.setup] Setup of domain logbook took 0.0 seconds. 2018-07-07 13:08:35 INFO (MainThread) [homeassistant.setup] Setup of domain conversation took 0.0 seconds. 2018-07-07 13:08:35 INFO (MainThread) [homeassistant.setup] Setup of domain script took 0.0 seconds. 2018-07-07 13:08:35 INFO (MainThread) [homeassistant.setup] Setup of domain light took 0.0 seconds. 2018-07-07 13:08:35 INFO (MainThread) [homeassistant.setup] Setup of domain switch took 0.0 seconds. 2018-07-07 13:08:35 INFO (MainThread) [homeassistant.setup] Setup of domain automation took 0.0 seconds. Failed config automation:

It then goes on to list the whole file contents.

Ok the only other thing I can think of is to break it up into topic and payload. You’ll need to do an if-then-else for each.

I have an automation that cleans up device tracker status that does essentially the same thing as you but i have topic and payload separate.

That’s about all I can think of. .

here’s what mine looks like for reference… this runs at every (re)start of HA. My Topic is singular but there’s no reason to think I couldn’t change it dependent on trigger.

  action:
    service: mqtt.publish
    data_template:
      topic: >
        ha/{{ trigger.entity_id | replace('_home_status','') | replace('sensor.', '') }}_home_status
      payload: >
        {% if states( 'device_tracker.ha_' ~ ( trigger.entity_id | replace('_home_status','') | replace('sensor.', ''))) == 'home' %}
          home
        {% elif states( 'device_tracker.ha_' ~ ( trigger.entity_id | replace('_home_status','') | replace('sensor.', ''))) == 'MMS' %}
          school
        {% elif states( 'device_tracker.ha_' ~ ( trigger.entity_id | replace('_home_status','') | replace('sensor.', ''))) == 'NES' %}
          school
        {% else %}
          away
        {% endif %}
      retain: false
2 Likes

It worked splitting up the topic and payload! You are the Best! I have include my code in case anyone else runs into the same problem.

- id: pondrgbeffectsend
  hide_entity: true
  trigger:
    platform: state
    entity_id: input_select.rgb_effect
  action:
    service: mqtt.publish
    data_template:
      topic: 'home/commands/PLSL_400/433_1'
      payload: >
        {% if is_state("input_select.rgb_effect", "Red") %}
          6121737
        {% elif is_state("input_select.rgb_effect", "Green") %}
          6121738
        {% endif %}
3 Likes

Glad you got it working… :thumbsup: