Mqtt publish input_select

HI,
@NotoriousBDG, please let me ask in a dedicated thread here, continuing the Double trigger? home, not_home discussion we had

I have this input_select on my main system:

  - alias: 'Activity selection'
    id: 'Activity selection'
#    hide_entity: True
    initial_state: 'on'
    trigger:
      platform: state
      entity_id: input_select.activity
    condition:
      condition: template
      value_template: >
        {{ states('input_select.activity') in 
            ['Opstaan','Aan de slag','Home theater',
             'Gym', 'Gaming', 'Selamat makan', 'Naar bed'] }}
    action:
      - service_template: >
            {{ 'script.' + states('input_select.activity') | lower | replace(' ','_') }}

I have a second Homeassistant setup which needs to read the selection made on this main system. It has the exact same input select for now.
Which of course is cause for trouble, since they don’t see each other, but do act on the same hardware…

Would it be possible to do that by publishing this activity selection over mqtt vice-versa?

If so, what would be the correct syntax for that?
I figured something like this:

  service: mqtt.publish
  data_template:
    topic: "activity_selection"
    retain: true
    payload: >-
      {
        "activity": "{{ states('input_select.activity') }}"
      }

of course I’d need to read that on the second system, which up to now uses

device_tracker:
  - platform: mqtt_json
    devices:
      activity_selection: activity_selection

Maybe I am over complicating things here.
Starting over:

2 systems having an identical input select, should be both master and slave. A choice made on either side should result in state change on both sides.

Hope you could have a look please, thx!

Since it’s a single value that you need, just publish that value to a topic. Then create a sensor on either system to show the state of that topic. You can make both input_selects update when the state changes for that sensor via an automation.

  service: mqtt.publish
  data_template:
    topic: "activity_selection"
    retain: true
    payload: "{{ states('input_select.activity') }}"
sensor:
  - platform: mqtt
    state_topic: "activity_selection"

thanks!

will test immediately.

this is the original automation, on both sides, working independently, and thus creating confusion:

automation:

  - alias: 'Activity selection'
    id: 'Activity selection'
   #hide_entity: True
    initial_state: 'on'
    trigger:
      platform: state
      entity_id: input_select.activity
    condition:
      condition: template
      value_template: >
        {{ states('input_select.activity') in 
            ['Opstaan','Aan de slag','Home theater',
             'Gym', 'Gaming', 'Selamat makan', 'Naar bed'] }}
    action:
      - service_template: >
            {{ 'script.' + states('input_select.activity') | lower | replace(' ','_') }}
      - delay: 00:00:02
      - service: python_script.summary

How does this change state when on either system a selection is made…? should I replace the trigger with:

trigger:
  platform: state
  entity_id: sensor.activity_selection

would be really cool if that were possible

setting it up as follows now, had to create an intermediary automation I believe, to have the new sensor trigger the automation of the input select. Always a bit mind bending which has to trigger what…

So input_select triggers and creates the mqtt sensor:

  - alias: 'Set Activity selection Mqtt'
    id: 'Set Activity selection'
    #hide_entity: True
    initial_state: 'on'
    trigger:
      platform: state
      entity_id: input_select.activity
    condition:
      condition: template
      value_template: >
        {{ states('input_select.activity') in 
            ['Opstaan','Aan de slag','Home theater',
             'Gym', 'Gaming', 'Selamat makan', 'Naar bed'] }}
    action:
      service: mqtt.publish
      data_template:
        topic: "activity_selection"
        retain: true
        payload: >-
          {
            "activity": "{{ states('input_select.activity')}}"
          }

which triggers the actions based on the input_select option originally:

  - alias: 'Activity selection'
    id: 'Activity selection'
   #hide_entity: True
    initial_state: 'on'
    trigger:
      platform: state
      entity_id: sensor.activity_selection
    condition:
      condition: template
      value_template: >
        {{ states('sensor.activity_selection') in 
            ['Opstaan','Aan de slag','Home theater',
             'Gym', 'Gaming', 'Selamat makan', 'Naar bed'] }}
    action:
      - service_template: >
            {{ 'script.' + states('sensor.activity_selection') | lower | replace(' ','_') }}
      - delay: 00:00:02
      - service: python_script.summary

like that? Ive got a feeling Im missing something, since the state of the input_select isnt set somehow now? hmm

also, the sensor isn’t created. Shouldn’t i have a name? found it!

ill rename it quickly, Ive got a feeling more mqtt sensors are in the way :wink:
Do need to know how to filter out the activity in a template…or can I create the sensor in a way it only hs the activity as its state, which would be very nice.

Don’t replace your trigger. Just add a new automation with this trigger then in the action, call input_select.select_option service to update your input select from the sensor’s state.

Unless you’re planning to send more than a string, change your action to this to remove the json encoding. It will be simpler to deal with.

    action:
      service: mqtt.publish
      data_template:
        topic: "activity_selection"
        retain: true
        payload: "{{ states('input_select.activity') }}"

If you’re planning to send attributes at the same time, then leave it as json and update your sensor config to parse the json.

sensor:
  - platform: mqtt
    state_topic: "activity_selection"
    value_template: "{{ value_json.activity }}"
1 Like

thanks!
will do this for now, and keep your second option in memory…
had already started like this: {{states('sensor.mqtt_sensor')[17:-1]|replace('"', '')}}…

not sure if I follow correctly:

I have the new automation set the mqtt sensor based on the input_select:

  - alias: 'Set Activity selection Mqtt'
    id: 'Set Activity selection'
    #hide_entity: True
    initial_state: 'on'
    trigger:
      platform: state
      entity_id: input_select.activity
    condition:
      condition: template
      value_template: >
        {{ states('input_select.activity') in 
            ['Opstaan','Aan de slag','Home theater',
             'Gym', 'Gaming', 'Selamat makan', 'Naar bed'] }}
    action:
      service: mqtt.publish
      data_template:
        topic: "activity_selection"
        retain: true
        payload:   "{{ states('input_select.activity') }}"

Why shouldn’t I change the trigger? If I don’t do that, the second system wont use it for input on the input select? I mean selecting on system 1 should lead to action on system 1 and have system 2 follow the option in the inout select. And vice versa. Isnt the mqtt sensor necessary as trigger for that?

nevermind the above.I think this is what you meant:

automation:

  - alias: 'Activity selection'
    id: 'Activity selection'
    #    hide_entity: True
    initial_state: 'on'
    trigger:
      platform: state
      entity_id: input_select.activity
    condition:
      condition: template
      value_template: >
        {{ states('input_select.activity') in 
            ['Opstaan','Aan de slag','Home theater',
             'Gym', 'Gaming', 'Selamat makan', 'Naar bed'] }}
    action:
      - service_template: >
            {{ 'script.' + states('input_select.activity') | lower | replace(' ','_') }}
      - delay: 00:00:02
      - service: mqtt.publish
        data_template:
          topic: "activity_selection"
          retain: true
          payload: "{{ states('input_select.activity') }}"
      - service: python_script.summary

  - alias: 'Set Activity selection mqtt sensor'
    id: 'Set Activity selection mqtt sensor'
    #    hide_entity: True
    #    initial_state: 'on'
    trigger:
      platform: state
      entity_id: sensor.activity_selection
    condition:
      condition: template
      value_template: >
        {{ states('sensor.activity_selection') in 
            ['Opstaan','Aan de slag','Home theater',
             'Gym', 'Gaming', 'Selamat makan', 'Naar bed'] }}
    action:
      service: input_select.select_option
      data_template:
        entity_id: input_select.activity
        option: >
          {{ states('sensor.activity_selection')}}
2 Likes