Enable / disable 'switch' in HASS?

Is it possible to have an ESPHome device read an ‘enable’ switch set in HASS and then run fans etc. only if the enable switch in HASS is on?

I have a drying unit using a dehumidifier, controlled by an ESPHome unit that switches fans and heaters on at various temperatures. What I want to do is have a ‘switch’ on a HASS Lovelace dashboard such that the dehumidifier is only enabled when the switch in HASS is on - and disabled when not.

I’m sure this is achievable but I’m going around in circles trying to get it to work so any help would be greatly appreciated.

Thanks.

Input_boolean?

More particularly this Home Assistant Binary Sensor — ESPHome

Yes.

As mentioned, you use an input boolean helper in HA to control your primary setting. Then you add them as a binary sensor to the ESP device config.

When the device connects with the api to HA, it will read the state of the helpers, so if you want to prevent unintended situations, you should wait for the api connection, then set the relay state to the value of the helper if it is on, assuming the fan switch is off at boot:

switch:
- platform: gpio
  name: "${friendly_name} Fan Relay Internal"
  id: fan_relay_internal
  restore_state: false
  internal: true

binary_sensor:
  - platform: homeassistant
    id: api_fan_setting
    name: "${friendly_name} Fan Setting"
    entity_id: input_boolean.esp_device_fan_setting
    on_press:
      then:
        - switch.turn_on: fan_relay_internal
    on_release:
      then:
        - switch.turn_off: fan_relay_internal
    filters:
      - delayed_on_off: 15000ms

esphome:
  on_boot:
    priority: -10
    then:
    # set your pre-api actions
    - wait_until:
        api.connected:
    - logger.log: API is connected!
    # set your post-api actions
    - if:
        condition:
          binary_sensor.is_on: api_fan_setting
        then:
          - switch.turn_on: fan_relay_internal

Using it that way prevents the disconnection of the device from making the control switch in HA be unavailable. That was an example where the control is in HA, and an automation reading the temp and humidity would control the fan or other device.

You can also have the setting “detached” and simply have it disable the automation in the ESP config:

switch:
- platform: template
  name: "${friendly_name} Fan Relay"
  id: fan_relay
  restore_state: false
  on_turn_on:
    then:
      - if:
        condition:
          binary_sensor.is_on: api_fan_setting
        then:
          - switch.turn_on: fan_relay_internal
  on_turn_off:
    then:
      - switch.turn_off: fan_relay_internal

Then you have temp and humidity automations on the ESP control fan_relay, and the actual relay only turns on if the the input helper is on

The code above was written by hand, I do not know if it compiles or if the indentation is correct, it is just an example, I am using something nearly identical for LED control so something based on it should work correctly, more code changes are required for the detached code to work correctly however, and that depends on the specific setup you have.

Thanks, all. @richieframe, that is particularly helpful as I was getting myself tied up in knots on this. I shall certainly be experimenting with this as I can see many potential uses for it.

Richard