MQTT Topic from input select

Hi! I am trying to control IOT Link on my pc through mqtt with input selects. There are multiple serious issues with this, namely that I am terrible at programming, not familiar with the syntax of jinja or yaml just impatient in general for any kind of debugging. I have tried to basically copy-paste code i have found on the site but of course it does not work.
This is what it looks like:

- id: computer_state
  alias: Power
  trigger:
  - platform: state
    entity_id: input_select.pc_power
  condition: []
  action:
  - service: mqtt.publish
    data_template:
      entity_id: input_select.pc_power
      option: >-
        {% set mydict = { 
          'logoff': 'Log Out'
          'lock': 'Lock',
          'reboot': 'Reset', } %}
        {{ mydict.get(action.topic) }}
      topic: "iotlink/workgroup/odin/commands/{{action.topic}}"
      payload: ''

And this is the error it throws:

Invalid config for [automation]: invalid template (TemplateSyntaxError: expected token ‘,’, got ‘:’) for dictionary value @ data[‘action’][0][‘data_template’][‘option’]. Got None. (See /config/configuration.yaml, line 48).
Invalid config for [automation]: Entity ID input_select. is an invalid entity id for dictionary value @ data[‘trigger’][0][‘entity_id’]. Got None
expected str for dictionary value @ data[‘id’]. Got None
string value is None for dictionary value @ data[‘alias’]. Got None. (See /config/configuration.yaml, line 48).

Line 48 is an enter, with nothing else, line 49 is:

automation: !include automations.yaml

Very helpful.

Thank you in advance for your help

Please format your code. In its current unformatted form, it’s difficult to spot errors.

There are two methods to format code (use either one):

  • Select the code and click the </> icon.
  • Enter three consecutive back-quotes ``` on a separate line before your code and another three on a separate line after your code.

Here’s one error that stands out, there’s a missing comma after 'logoff': 'Log Out'

When you use action.topic where is action defined? Maybe you want to use trigger as in trigger.topic ?

MQTT

Template variable Data
trigger.platform Hardcoded: mqtt .
trigger.topic Topic that received payload.
trigger.payload Payload.
trigger.payload_json Dictonary of the JSON parsed payload.
trigger.qos QOS of payload.

EDIT
Now that I look more closely, you’re using a state trigger so trigger.topic won’t work. What exactly are you trying to do in the template? It seems to me you may need to use trigger.to_state.state.

1 Like

Fixed both issues

- id: computer_state
  alias: Power
  trigger:
  - platform: state
    entity_id: input_select.pc_power
  condition: []
  action:
  - service: mqtt.publish
    data_template:
      entity_id: input_select.pc_power
      option: >-
        {% set mydict = { 
          'logoff': 'Log Out',
          'lock': 'Lock',
          'reboot': 'Reset', } %}
        {{ mydict.get(trigger.topic) }}
      topic: "iotlink/workgroup/odin/commands/{{trigger.topic}}"
      payload: ''

Unfortunately the error message didn’t change much.

Invalid config for [automation]: Entity ID input_select. is an invalid entity id for dictionary value @ data[‘trigger’][0][‘entity_id’]. Got None
expected str for dictionary value @ data[‘id’]. Got None
string value is None for dictionary value @ data[‘alias’]. Got None. (See /config/configuration.yaml, line 48).

Can it be the input select?

  pc_power:
    name: PC Power
    options:
      - "Log Out"
      - "Lock"
      - "Reset"

Use trigger.to_state.state.

To clarify, I originally didn’t notice that the trigger is using the State platform. Therefore if you want the input_select's new state, it will be reported by trigger.to_state.state

State

Template variable Data
trigger.platform Hardcoded: state
trigger.entity_id Entity ID that we observe.
trigger.from_state The previous state object of the entity.
trigger.to_state The new state object that triggered trigger.
trigger.for Timedelta object how long state has been to state, if any.

I don’t see how this will work at all. Topic is seemly pulled out of the air. If it’s coming from the input select then this is what you want:

I’m going to assume your pc power inmput_select is formatted like this:

input_select:
  pc_power:
    options: 
    - Log Out
    - Lock
    - Reset

With that being said, your automation should look like this:

- id: computer_state
  alias: Power
  trigger:
  - platform: state
    entity_id: input_select.pc_power
  action:
  - service: mqtt.publish
    data_template:
      topic: >
        {% set mydict = {
          'Log Out': 'logoff',
          'Lock': 'lock',
          'Reset': 'reboot',
          } %}
        iotlink/workgroup/odin/commands/{{ mydict[trigger.to_state.state] }}
      payload: ''

But something needs to publish to the payload.

1 Like

Just in the topic but you need to restructure it like in petro’s example.

I’m not familiar with IotLink but your example is odd because it doesn’t supply a payload and, instead, uses something called option. What is that?

There’s no such parameter in mqtt.publish

99 % sure he’s combining an MQTT publish service and a input_select.select_option service and he doesn’t understand that you can’t do that.

1 Like

My intent with it is to use a drop down menu to lock, reboot or log out of my pc. In IOT Link the mqtt topics change instead of their payload. And of course it looks like it was pulled out of thin air. Because I have no idea about how it should look like. As I said, I was just trying to copy parts of code i simply don’t understand. I don’t even know what i don’t know. My bad, I did not mean to upset anyone.

Just checked the docs for IotLink. Apparently leaving payload blank is valid. If left blank it applies to all users logged into the PC. Otherwise, you specify the user’s account name.

no ones upset, just trying to help you. The automation I provided should do what you want, assuming your input_select is build the way I assumed. Can you post your input select?

  pc_power:
    name: PC Power
    options:
      - "Log Out"
      - "Lock"
      - "Reset"

in the input_select.yaml file. This was working before, with

- id: pc_log_out
  alias: Log Out
  description: ''
  trigger:
  - entity_id: input_select.pc_power
    platform: state
    to: Log Out
  condition: []
  action:
  - service: script.my_computer_logoff

- id: pc_lock
  alias: Lock PC
  description: ''
  trigger:
  - entity_id: input_select.pc_power
    platform: state
    to: Lock
  condition: []
  action:
  - service: script.my_computer_lock

- id: pc_reset
  alias: Reset PC
  description: ''
  trigger:
  - entity_id: input_select.pc_power
    platform: state
    to: Reset
  condition: []
  action:
  - service: script.my_computer_reboot

and

  my_computer_reboot:
    alias: "Reboot"
    sequence:
      - service: mqtt.publish
        data:
          topic: "iotlink/workgroup/odin/commands/reboot"
          payload: ""

  my_computer_logoff:
    alias: "Logoff"
    sequence:
      - service: mqtt.publish
        data:
          topic: "iotlink/workgroup/odin/commands/logoff"
          payload: ""

  my_computer_lock:
    alias: "Lock"
    sequence:
      - service: mqtt.publish
        data:
          topic: "iotlink/workgroup/odin/commands/lock"
          payload: ""

Yup, not upset, just puzzled by the knot you created. :slight_smile:

I believe it’s been untangled and petro’s example should do the trick.

Invalid config for [automation]: Entity ID input_select. is an invalid entity id for dictionary value @ data[‘trigger’][0][‘entity_id’]. Got None
expected str for dictionary value @ data[‘id’]. Got None
string value is None for dictionary value @ data[‘alias’]. Got None. (See /config/configuration.yaml, line 48).

Did you copy my example exactly? That error is saying that input_select. is not valid. If you copy and paste the code exactly, it will work.

I copied the wrong error message actually. The current one says

Error loading /config/configuration.yaml: mapping values are not allowed here
in “/config/automations.yaml”, line 273, column 9

Which is the colon after alias.

can you post a screenshot of your config

Played around with the indentation.

- id: computer_state
  alias: Power
  trigger:
  - platform: state
    entity_id: input_select.pc_power
  action:
  - service: mqtt.publish
    data_template:
     topic: >-
      {% set mydict = {
         'Log Out': 'logoff',
         'Lock': 'lock',
         'Reset': 'reboot',
         } %}
      iotlink/workgroup/odin/commands/{{ mydict[trigger.to_state.state] }}
     payload: ''

New error message is

Invalid config for [automation]: Entity ID input_select. is an invalid entity id for dictionary value @ data[‘trigger’][0][‘entity_id’]. Got None
expected str for dictionary value @ data[‘id’]. Got None
string value is None for dictionary value @ data[‘alias’]. Got None. (See /config/configuration.yaml, line 48).

Which part of it would you like to see?

Just a bit of tidying and proper indenting. This passes Config Check on my system:

- id: computer_state
  alias: Power
  trigger:
    platform: state
    entity_id: input_select.pc_power
  action:
    service: mqtt.publish
    data_template:
      payload: ''
      topic: >-
        {% set mydict = {
          'Log Out': 'logoff',
          'Lock': 'lock',
          'Reset': 'reboot' } %}
        iotlink/workgroup/odin/commands/{{ mydict[trigger.to_state.state] }}

BTW, if your input_select's options were defined like this:

  pc_power:
    name: PC Power
    options:
      - "Logoff"
      - "Lock"
      - "Reboot"

then the automation’s topic template can be reduced to this:

      topic: "iotlink/workgroup/odin/commands/{{ trigger.to_state.state | lower }}"
1 Like

Petro’s solution worked well for me as well, after i cleaned out a few lines of leftover code :smile: