Trigger MQTT publish script via input_boolean Template Switch

I have a pair of scripts that are working - I can manually trigger them and see the results in the “Listen to a topic” screen:

mqtt_in_a_meeting_on:
  alias: 'Publish In a Meeting notification'
  sequence:
    - service: mqtt.publish
      data:
        topic: "home-assistant/message-board/1/meeting"
        payload: '{ "message": "In a Meeting", "action": "on" }'
        
mqtt_in_a_meeting_off:
  alias: 'Publish Not In a Meeting notification'
  sequence:
    - service: mqtt.publish
      data:
        topic: "home-assistant/message-board/1/meeting"
        payload: '{ "message": "Not In a Meeting", "action": "off" }'

I also have a Template Switch associated with an input_boolean which is currently displayed on the UI and responds to the Entity toggle:

input_boolean:
  in_a_meeting:
    name: In a Meeting

switch:
  - platform: template
    switches:
      in_a_meeting:
        value_template: "{{ is_state('input_boolean.in_a_meeting', 'on') }}"
        turn_on:
          service: script.mqtt_in_a_meeting_on
        turn_off:
          service: script.mqtt_in_a_meeting_off

But the toggle is not triggering the MQTT publish. Help?

Figured it out. I needed a little more information in the switch config:

switch:
  - platform: template
    switches:
      in_a_meeting:
        friendly_name: In a Meeting
        value_template: "{{ is_state('input_boolean.in_a_meeting', 'on') }}"
        turn_on:
          - service: input_boolean.turn_on
            entity_id: input_boolean.in_a_meeting
          - service: script.mqtt_in_a_meeting_on
        turn_off:
          - service: input_boolean.turn_off
            entity_id: input_boolean.in_a_meeting
          - service: script.mqtt_in_a_meeting_off

And then in the UI, I needed to create the card based on switch.in_a_meeting instead of the input_boolean.in_a_meeting.

Why not put the two input_boolean service calls into the scripts?

For that matter, why not use a single script that accepts a parameter indicating whether to turn on or off?

mqtt_meeting:
  alias: 'Publish Meeting notification'
  sequence:
    - service: mqtt.publish
      data:
        topic: "home-assistant/message-board/1/meeting"
        payload: >
          { "message": "{{'Not ' if mode == 'off' else ''}}In a Meeting", "action": "{{mode}}" }
    - service: 'input_boolean.turn_{{mode}}'
      target:
        entity_id: input_boolean.in_a_meeting
switch:
  - platform: template
    switches:
      in_a_meeting:
        friendly_name: In a Meeting
        value_template: "{{ is_state('input_boolean.in_a_meeting', 'on') }}"
        turn_on:
          - service: script.mqtt_meeting
            data:
              mode: 'on'
        turn_off:
          - service: script.mqtt_meeting
            data:
              mode: 'off'

Why not? Because I’m very new to HA and didn’t know you could do that! :smile:

Thanks for the tips! It’s working as-is, but I may utilize some of your improvements. I’m not entirely happy with the “In” being capitalized when mode == ‘off’, but could easily overcome that. Also don’t like the hard-coded reference to the input_boolean id in the script.

Maybe I could make the ‘publish’ script more generic and put data variables in for the topic and potentially the appropriate input_boolean as well (if I end up having more than one, which is entirely possible).

Wondering if this would work (not yet deployed/tested):

mqtt_input_boolean_notification:
  alias: 'Publish MQTT Notification from Input Boolean'
  sequence:
    - service: mqtt.publish
      data:
        topic: "home-assistant/{{generic.parent_topic}}/{{generic.board_number}}/{{generic.sub_topic}}"
        payload: >
          { "message": "{{message}}", "action": "{{mode}}" }
    - service: 'input_boolean.turn_{{mode}}'
      target:
        entity_id: input_boolean.{{generic.input_id}}

Not sure about the “generic data” definition - theoretically this should work. The YAML passes validation (Best YAML Validator Online)

meetings_board_generic_data:
  parent_topic: 'message-board'
  board_number: 1
  sub_topic: 'meetings'
  input_id: 'in_a_meeting'

switch:
  - platform: template
    switches:
      in_a_meeting:
        friendly_name: In a Meeting
        value_template: "{{ is_state('input_boolean.in_a_meeting', 'on') }}"
        turn_on:
          - service: script.mqtt_input_boolean_notification
            data:
              generic: meetings_board_generic_data
              mode: 'on'
              message: 'In a meeting'
        turn_off:
          - service: script.mqtt_input_boolean_notification
            data:
              generic: meetings_board_generic_data
              mode: 'off'
              message: 'Not in a meeting'

The end-goal is to have the message text displayed on an AdaFruit 64x32 LED Matrix that subscribes to that feed. I’ve been working from home since March of 2020 and am in and out of virtual meetings and calls all day. I close my door when I’m on the call, but then when I’m available I don’t always open my door immediately. Having the board outside my home office will help my family know not to disturb me, or if I’m available (as long as I remember to toggle the switch :upside_down_face:). Stretch goal is to hook it up to my MS Teams account so it can automatically publish the notifications based on my status, but I don’t know if my job will allow me access to the API.

I have the hardware set up, just need to write the script to process the notification payload. I want to make that script as generic as possible, too, which is why I have it delineated by parent_topic, board_number, and sub_topic so if I create a second board with the same setup but different configuration it can be hooked up to a different template switch, input boolean, and topic (say, if my daughter wanted to have one on her bedroom door).