Using input_selection to publish to multiple mqtt topics when selection is made

I’d like to create an input_selection that has three states: start, stop, auto start/stop

I need to publish to 2 mqtt topics when any of these selections are selected. I have two sensors that return the state. The same topic path is used or publishing as well

  - platform: mqtt
    name: "auto_start_stop"
    scan_interval: 1
    state_topic: "N/abc123/settings/0/Settings/Generator0/AutoStartEnabled"
    value_template: '{{value_json.value}}'
  - platform: mqtt
    name: "generator_manual_start"
    scan_interval: 1
    state_topic: "N/abc123/settings/0/Settings/Generator0/AutoStartEnabled"
    value_template: '{{value_json.value}}'

When start is selected:
auto_start_stop should publish 0
generator_manual_start should publish 1

When stop is selected:
auto_start_stop should publish 0
generator_manual_start should publish 0

When Auto start/stop is selected:
auto_start_stop should publish 1
generator_manual_start should publish 0

I have followed the docs on input_select but it’s not clear how I can implement the above? Any pointers?

Write an automation that triggers on a change of the input select and then in the action part publish to these topics based om what the input select shows.

1 Like

@Burningstone thank you. How would I keep the state of which option is selected in sync? For instance I would want to select the button, but then also verify and update it’s state. This is because there are other ways the state can be chanted outside of HA so I would want to update when those changes happen.

If the generator is running, I would want the Start/On button to be highlighted like below. I can surmise this by checking the two MQTT topics I wrote to

Ok, with this additional information, an input_select is not the right choice for this task in my opinion. I’d create one template switch for each of the three options.

@Burningstone this is so far what I have, any feedback on this or if there’s a bettery way

I have a sensor that combines the data from two sensors to give me what the current value is on, off, auto start/stop

- platform: template
  sensors:
    generator_start_stop:
      friendly_name: Generate Start/Stop State
      value_template: >
        {% if states.sensor.generator_auto_start_stop and states.sensor.generator_manual_start %}
          {% if is_state('sensor.generator_auto_start_stop', '1') and is_state('sensor.generator_manual_start', '0') %}
            Auto Start/Stop
          {% elif is_state('sensor.generator_auto_start_stop', '0') and is_state('sensor.generator_manual_start', '0') %}
            Off
          {% elif is_state('sensor.generator_auto_start_stop', '0') and is_state('sensor.generator_manual_start', '1') %}
            On
          {% elif is_state('sensor.generator_auto_start_stop', '1') and is_state('sensor.generator_manual_start', '1') %}
            On
          {% else %}
            N/A
          {% endif %}
        {% else %}
          N/A
        {% endif %}

Then I have an input select with corresponding values:

input_select:
  generator_start_stop:
    name: Generator
    options:
      - "On"
      - "Off"
      - "Auto Start/Stop"
    icon: mdi:target

Then I have an automation which syncs the input_select to the sensor value:

- alias: 'Update Generator Input'
  trigger:
  - platform: state
    entity_id: sensor.generator_start_stop
  action:
    service: input_select.select_option
    data_template:
      entity_id: input_select.generator_start_stop
      option: "{{ states('sensor.generator_start_stop') }}"

Now if I want to use the input select to change the state, I have a automation that fires a script for each state:

- alias: Generator Input Select Off
  trigger:
  - platform: state
    entity_id: input_select.generator_start_stop
    to: 'Off'
  action:
    - service: script.turn_on
      data:
        entity_id: script.generator_off

- alias: Generator Input Select On
  trigger:
  - platform: state
    entity_id: input_select.generator_start_stop
    to: 'On'
  action:
    - service: script.turn_on
      data:
        entity_id: script.generator_on

- alias: Generator Input Select Auto
  trigger:
  - platform: state
    entity_id: input_select.generator_start_stop
    to: 'Auto Start/Stop'
  action:
    - service: script.turn_on
      data:
        entity_id: script.generator_auto

And one of the scirpts looks like this:

generator_off:
  alias: 'Generator Off Script'
  sequence:
    - service: mqtt.publish
      data:
        topic: "N/b827eb57c4de/settings/0/Settings/Generator0/AutoStartEnabled"
        payload: '{ "value": 0}'
    - service: mqtt.publish
      data:
        topic: "N/b827eb57c4de/generator/0/Generator0/ManualStart"
        payload: '{ "value": 0}'

It seems to work, but I’m of course wondering if there is a cleaner/simpler way to implement. Any feedback appreciated